FileInputStream和FileOutputStream如何在Java中工作? [英] How FileInputStream and FileOutputStream Works in Java?

查看:156
本文介绍了FileInputStream和FileOutputStream如何在Java中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Java上阅读java中的所有输入/输出流教程文档。教程编写者使用此示例:

I'm reading about all input/output streams in java on Java Tutorials Docs. Tutorials writer use this example:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyBytes {
    public static void main(String[] args) throws IOException {

        FileInputStream in = null;
        FileOutputStream out = null;

        try {
            in = new FileInputStream("xanadu.txt");
            out = new FileOutputStream("outagain.txt");
            int c;

            while ((c = in.read()) != -1) {
                out.write(c);
            }
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }
}

xanadu .txt文件数据:

xanadu.txt File data:

In Xanadu did Kubla Khan
A stately pleasure-dome decree:
Where Alph, the sacred river, ran
Through caverns measureless to man
Down to a sunless sea.

输出到outagain.txt文件:

Output to outagain.txt file:

In Xanadu did Kubla Khan
A stately pleasure-dome decree:
Where Alph, the sacred river, ran
Through caverns measureless to man
Down to a sunless sea.




  1. 为什么作家使用 int c 即使我们正在阅读字符?

  1. Why do the writers use int c even if we are reading characters?

为什么要使用 -1 在有条件的情况下?

Why use -1 in while condition?

如何 out.write(c); 方法转换 int 再来一次字符?

How out.write(c); method convert int to again characters?


推荐答案


1:现在我想问为什么作家使用int c?即使我们正在读字。

1: Now I want to ask why writer use int c? even we are reading characters.

FileInputStream.read() 的形式返回一个字节的数据INT 。这是有效的,因为一个字节可以表示为 int 而不会损失精度。请参阅此答案,了解为何返回 int 而不是 byte

FileInputStream.read() returns one byte of data as an int. This works because a byte can be represented as an int without loss of precision. See this answer to understand why int is returned instead of byte.


2:第二个为什么在条件下使用-1?

2: The second why use -1 in while condition?

当到达文件末尾时,返回-1。

When the end of file is reached, -1 is returned.


3:如何out.write(c);方法将int转换为再次字符?在outagain.txt文件中提供相同的输出

3: How out.write(c); method convert int to again characters? that provide same output in outagain.txt file

FileOutputStream.write() 需要一个字节参数为 int 。由于 int 跨越多于一个字节的值,因此忽略给定int的24个高位,使其成为字节兼容值:<$ c $ Java中的c> int 是总是32位。通过删除24个高位,你可以达到8位值,即一个字节。

FileOutputStream.write() takes a byte parameter as an int. Since an int spans over more values than a byte, the 24 high-order bits of the given int are ignored, making it a byte-compatible value: an int in Java is always 32 bits. By removing the 24 high-order bits, you're down to a 8 bits value, i.e. a byte.

我建议你仔细阅读每个方法的Javadocs 。作为参考,他们回答了您的所有问题:

I suggest you read carefully the Javadocs for each of those method. As reference, they answer all of your questions:

读取


从输入流中读取下一个数据字节。值字节作为int返回,范围为0到255.如果没有字节可用,因为已到达流的末尾,则返回值-1。此方法将阻塞,直到输入数据可用,检测到流的末尾或抛出异常。

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.


将指定的字节写入此输出流。写入的一般合同是将一个字节写入输出流。要写入的字节是参数b的八个低位。 b的24个高位被忽略。

Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

这篇关于FileInputStream和FileOutputStream如何在Java中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆