英文:
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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论