简体   繁体   English

FileInputStream和FileOutputStream如何在Java中工作?

[英]How FileInputStream and FileOutputStream Works in Java?

I'm reading about all input/output streams in java on Java Tutorials Docs . 我正在阅读Java Tutorials Docs中Java中的所有输入/输出流。 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 File data: xanadu.txt文件数据:

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.

Output to outagain.txt file: 输出到outagain.txt文件:

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. Why do the writers use int c even if we are reading characters? 为什么即使我们正在阅读字符,作者也会使用int c

  2. Why use -1 in while condition? 为什么在条件下使用-1

  3. How out.write(c); 怎么out.write(c); method convert int to again characters? 方法将int转换为再次字符?

1: Now I want to ask why writer use int c? 1:现在我想问为什么作家使用int c? even we are reading characters. 即使我们正在读人物。

FileInputStream.read() returns one byte of data as an int . FileInputStream.read()int返回一个数据字节。 This works because a byte can be represented as an int without loss of precision. 这是有效的,因为一个字节可以表示为int而不会损失精度。 See this answer to understand why int is returned instead of byte . 请参阅此答案以了解为什么返回int而不是byte

2: The second why use -1 in while condition? 2:第二个为什么在条件下使用-1?

When the end of file is reached, -1 is returned. 到达文件末尾时,返回-1。

3: How out.write(c); 3:如何out.write(c); method convert int to again characters? 方法将int转换为再次字符? that provide same output in outagain.txt file 在outagain.txt文件中提供相同的输出

FileOutputStream.write() takes a byte parameter as an int . FileOutputStream.write()将一个byte参数作为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 . 由于int跨越多于一个字节的值,因此忽略给定int的24个高位,使其成为字节兼容的值:Java中的int 总是32位 By removing the 24 high-order bits, you're down to a 8 bits value, ie a byte. 通过删除24个高位,您可以达到8位值,即一个字节。

I suggest you read carefully the Javadocs for each of those method. 我建议你仔细阅读每种方法的Javadocs。 As reference, they answer all of your questions: 作为参考,他们回答了您的所有问题:

read : read

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. 值字节作为int返回,范围为0到255.如果没有字节可用,因为已到达流的末尾,则返回值-1。 This method blocks until input data is available, the end of the stream is detected, or an exception is thrown. 此方法将阻塞,直到输入数据可用,检测到流的末尾或抛出异常。

write : write

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. 要写入的字节是参数b的八个低位。 The 24 high-order bits of b are ignored. b的24个高位被忽略。

Just read the docs. 只需阅读文档。

here is the read method docs http://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html#read() 这里是read方法docs http://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html#read()

public int read() throws IOException Reads a byte of data from this input stream. public int read()throws IOException从此输入流中读取一个数据字节。 This method blocks if no input is yet available. 如果尚未提供输入,此方法将阻止。

Specified by: read in class InputStream 指定者:在类InputStream中读取

Returns: the next byte of data, or -1 if the end of the file is reached. 返回:数据的下一个字节,如果到达文件末尾,则返回-1。

That int is a your next set of bytes data. 那个int是你的下一组字节数据。 Now , here are the answers. 现在,这是答案。

1) When you assign a char to an int, it denotes it's ascii number to the int. 1)当你为一个int赋一个char时,它表示它是int的ascii数。

If you are interested, here us the list of chars and their ascii codes https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html 如果您有兴趣,请在此处列出字符及其ascii代码列表https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html

2)-1 if the end of the file is reached. 2)-1如果到达文件的末尾。 So that's a check to data exists or not. 所以这是对数据的检查存在与否。

3)When you send an ascii code to print writer, it's prints that corresponding char to the file. 3)当您向打印编写器发送ascii代码时,它会将相应的char打印到文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM