请帮我理解下面的java代码
我正在从 Java : Complete Reference 学习 java。 我目前正在研究本章中的示例:输入/输出:探索 java.io 我不明白下面示例中的几行代码。 谁能帮我这个例子。
import java.io.*;
class FileInputStreamDemo
{
public static void main(String args[]) throws IOException
{
InputStream f = new FileInputStream("E://SomeRandomTextFile.txt");
System.out.println("Total available bytes : " + size = f.available());
int n = size/40;
System.out.println("First" + n + " bytes of file one read() at a time");
for(int i=0; i<n; i++)
{
System.out.println((char) f.read());
}
System.out.println("\n Still available: "+ f.available());
System.out.println("Reading the text " + n + " with one read(b[])");
byte b[] = new byte[n];
if(f.read(b) != n)
{
System.err.println("coudn't read" + n + "bytes.");
}
System.out.println(new String(b,0,n));
}
在上面的代码中,我不明白最后五行代码。
的结果是什么
f.read(b)
What is
System.err
以及
What is the results of
new String(b,0,n);
I am learning java from Java : Complete Reference.
I am currently working on the examples in the chapter : Input/Output : Exploring java.io
I dint understand few lines of code from the example below.
Can anyone help me with this example.
import java.io.*;
class FileInputStreamDemo
{
public static void main(String args[]) throws IOException
{
InputStream f = new FileInputStream("E://SomeRandomTextFile.txt");
System.out.println("Total available bytes : " + size = f.available());
int n = size/40;
System.out.println("First" + n + " bytes of file one read() at a time");
for(int i=0; i<n; i++)
{
System.out.println((char) f.read());
}
System.out.println("\n Still available: "+ f.available());
System.out.println("Reading the text " + n + " with one read(b[])");
byte b[] = new byte[n];
if(f.read(b) != n)
{
System.err.println("coudn't read" + n + "bytes.");
}
System.out.println(new String(b,0,n));
}
In the above code, I dint understand the last five lines of code.
What is the outcome of
f.read(b)
What is
System.err
and
What is the outcome of
new String(b,0,n);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该是:
这是一个从文件中读取字节到缓冲区的方法调用。来自javadoc:
此行:
从缓冲区
b
内的字节创建一个新的String
,从索引 0 开始并获取接下来的 n 个字节。来自javadoc:最后:
返回对程序标准错误流的引用。
Should be:
It's a method call to read bytes from the file into the buffer. From javadoc:
This line:
creates a new
String
from the bytes inside the bufferb
, starting from index 0 and taking the next n bytes. From javadoc:And finally this:
returns a reference to the program standard error stream.
f.read(b)
结果是一个长度为 1 的整数,或者一个字节。System.err
定位错误窗口,然后在其中放置一条消息,就像System.out
定位控制台窗口然后在其中放置一条消息一样。new String(b,0,n)
将使用 String(byte[] bytes, int offset, int length) 构造函数使用字节数组 b 创建一个字符串,从偏移量 0 开始,并具有长度为n。f.read(b)
results in an integer of length 1, or a single byte.System.err
locates the error window and then places a message there, much likeSystem.out
locates the console window and then places a message there.new String(b,0,n)
will use the String(byte[] bytes, int offset, int length) constructor to make a string with the byte array b, starting at offset 0, and having a length of n.