The Java.io.InputStream class is the superclass of all classes representing an input stream of bytes. How is it reading a file with characters?

huangapple go评论69阅读模式
英文:

The Java.io.InputStream class is the superclass of all classes representing an input stream of bytes. How is it reading a file with characters?

问题

以下是您要翻译的内容:

我有一个名为 mark_method.json 的文件,其中包含 ABCDE,我正在使用 InputStream 类读取此文件。

根据定义,InputStream 类读取字节输入流。这是如何工作的?文件中没有字节,只有字符?

我正在尝试理解一个读取字节的流如何从文件中读取字符?

public class MarkDemo {
    public static void main(String args[]) throws Exception {
        InputStream is = null;
    
        try {
            is = new FileInputStream("C:\\Users\\s\\Documents\\EB\\EB_02_09_2020_with_page_number_and_quote_number\\Old_images\\mark_method.json");
        }
        catch(Exception e) {
            e.printStackTrace();
        } finally {
            if(is != null) {
                is.close();
            }
        }
    }
}
英文:

I have a file named mark_method.json containing ABCDE in it and I am reading this file using the InputStream class.

By definition, the InputStream class reads an input stream of bytes. How does this work? I don't have bytes in the file, but characters?

I am trying to understand how a stream reading bytes is reading characters from the file?

public class MarkDemo {
    public static void main(String args[]) throws Exception {
	    InputStream is = null;
	
	    try {
		    is = new FileInputStream("C:\\Users\\s\\Documents\\EB\\EB_02_09_2020_with_page_number_and_quote_number\\Old_images\\mark_method.json");
	    }
	    catch(Exception e) {
		    e.printStackTrace();
	    } finally {
		    if(is != null) {
			    is.close();
		    }
	    }
    }
}

答案1

得分: 3

计算机上的每个数据都以位和字节存储。这里文件的内容也是以字节存储的。

我们有程序,将这些字节转换为人类可读的形式,因此我们看到 mark_method.json 文件包含字符而不是字节。

英文:

Every data on the computer is stored in bits and bytes. Here the content of the files is also stored in bytes.

We have programs which convert these bytes into human-readable forms thus we see the mark_method.json file containing characters and not bytes.

答案2

得分: 1

一个字符就是一个字节(至少在ASCII码中是这样)。
从0到127的每个字节都有一个字符值。例如,0代表空字符(Null-character),0xa代表换行符\n,0xd代表回车符\r,0x41代表'A'等等。

这个实现只知道字节。它不知道字符0x2709代表着✉。它只把它看作两个字节:0x27和0x09。
只有文本编辑器解释这些字节,并显示出匹配的符号/字母。

英文:

An character is a byte. (At least in ASCII).
Each byte from 0 to 127 has a character value. For example 0 is the Null-character, 0xa is \n, 0xd is \r, 0x41 is 'A' and so on.

The implementation only knows bytes. It doesn't know, that the char 0x2709 is ✉. It only sees it as two bytes: 0x27 and 0x09.
Only the texteditor interprets the bytes and show the matching symbol/letter

答案3

得分: 0

我认为你实际上在这里问的是如何将使用FileInputStream从文件中读取的字节转换为可以打印和操作的Java String对象。

FileInputStream没有任何直接生成String对象的读取方法,所以如果你想要的是这个,你需要进一步操作你得到的输入。

第一种选项是使用Scanner类:

Scanner scanner = new Scanner(is);
String word = scanner.next();

另一个选项是读取字节并使用String类的构造函数来处理字节数组:

byte[] bytes = new byte[10];
is.read(bytes);
String text = new String(bytes);

请注意,出于简单起见,我假设你可以从文件中读取10个有效字节。在实际代码中,你需要一些逻辑来确保你读取了正确数量的字节。

另外,如果你的文件不是使用系统默认的字符集存储的,你需要将字符集指定为String构造函数的参数。

最后,你可以使用另一个包装类,BufferedReader,它具有readLine函数,可以处理从文件中读取表示文本行的所有所需逻辑,并将它们作为String返回。

BufferedReader in = new BufferedReader(new FileReader("foo.in"));
String line = in.readLine();
英文:

I think what you are actually asking here is how to convert the bytes you read from file using FileInputStream in to a Java String object you can print and manipulate.

FileInputStream does not have any read methods for directly producing a String object so if that is what you want, you need to further manipulate the input you get.

Option one is to use the Scanner class:

Scanner scanner = new Scanner(is);
String word = scanner.next();

Another option is to read the bytes and use the constructor of the String class that works with byte array:

byte [] bytes = new byte[10];
is.read(bytes);
String text = new String(bytes);

Note that for simplicity I just assumed you can read 10 valid bytes from your file.
In real code you would need some logic to make sure you are reading correct number of bytes.

Also, if your file is not stored using your system default character set, you will need to specify the character set as a parameter to the String constructor.

Finally, you can use another wrapper class, BufferedReader that has a readLine function which takes care of all the logic needed to read bytes representing a line of text from a file and return them in a String.

BufferedReader in = new BufferedReader(new FileReader("foo.in"));
String line = in.readLine();

huangapple
  • 本文由 发表于 2020年9月12日 17:34:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63858883.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定