请帮我理解下面的java代码

发布于 2025-01-02 11:42:33 字数 1041 浏览 3 评论 0原文

我正在从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゃ懵逼小萝莉 2025-01-09 11:42:33

应该是:

if(f.read(b) != n)

这是一个从文件中读取字节到缓冲区的方法调用。来自javadoc:

从输入流中读取一定数量的字节并将其存储到
缓冲区数组 b.

此行:

new String(b,0,n);

从缓冲区 b 内的字节创建一个新的 String,从索引 0 开始并获取接下来的 n 个字节。来自javadoc:

通过解码指定的字节子数组构造一个新的 String
使用平台的默认字符集。

最后:

System.err

返回对程序标准错误流的引用。

Should be:

if(f.read(b) != n)

It's a method call to read bytes from the file into the buffer. From javadoc:

Reads some number of bytes from the input stream and stores them into
the buffer array b.

This line:

new String(b,0,n);

creates a new String from the bytes inside the buffer b, starting from index 0 and taking the next n bytes. From javadoc:

Constructs a new String by decoding the specified subarray of bytes
using the platform's default charset.

And finally this:

System.err

returns a reference to the program standard error stream.

白芷 2025-01-09 11:42:33

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 like System.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文