为什么从inputStream到outputStream的传输无法正常工作?

huangapple go评论81阅读模式
英文:

Why inputStream to outputStream does not work properly?

问题

我创建了3个文件:f1,f2,f3。我将f1文件填充了数字:12345。其他文件(f2,f3)是空的。我的目标是从控制台读取文件名,然后使用FileInputStream从第一个文件读取内容,并将f1的前半部分写入f2,后半部分写入f3。所以,我认为在关闭流之后,f2中将会有数字1、2,而f3中将会有数字345。我编写了以下代码:

public static void main(String[] args) throws IOException {
    BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
    String file1 = rd.readLine();
    String file2 = rd.readLine();
    String file3 = rd.readLine();

    try (
            FileInputStream inputStream1 = new FileInputStream(file1);
            FileOutputStream output2 = new FileOutputStream(file2);
            FileOutputStream output3 = new FileOutputStream(file3);
    ) {

        while (inputStream1.available() > 0) {
            if (inputStream1.available() < inputStream1.getChannel().size() / 2) {
                output2.write(inputStream1.read());
            } else {
                output3.write(inputStream1.read());
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

这段代码可以运行,但是它的功能有问题。运行程序后,f2中只有数字5,而f3中有1234。为什么inputStream1.getChannel().size()的大小是6(尽管f1中只有5个数字)?为什么输出流的操作出现了问题?

英文:

I created 3 files: f1,f2,f3. f1-file I filled with numbers: 12345. Other files (f2,f3) are empty. My objective is to read filenames from console and read from first file using FileInputStream and to write first half of f1 to f2 and second half to f3. So, I suppose that after closing streams, I will get in f2 numbers 1,2 and in f3 numbers 345. I wrote the code:

public static void main(String[] args) throws IOException {
        BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
        String file1 = rd.readLine();
        String file2 = rd.readLine();
        String file3 = rd.readLine();

        try (
                FileInputStream inputStream1 = new FileInputStream(file1);
                FileOutputStream output2 = new FileOutputStream(file2);
                FileOutputStream output3 = new FileOutputStream(file3);
                ) {

            while (inputStream1.available() &gt; 0) {
                if (inputStream1.available() &lt; inputStream1.getChannel().size() / 2) {
                    output2.write(inputStream1.read());
                } else {
                    output3.write(inputStream1.read());
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

It works, but it works incorrectly. After running the program, I get in f2 only number 5, but in f3 get 1234. Size of inputStream1.getChannel().size() is 6 (though I have only 5 numbers in f1) why? And why output streams works wrong?

答案1

得分: 1

你正在错误地使用 available(),应该使用更新的 NIO.2 API,并且你正在每次复制1字节,这非常慢。

请按照以下方式进行:

try (
    InputStream in = Files.newInputStream(Paths.get(file1));
    OutputStream out2 = Files.newOutputStream(Paths.get(file2));
    OutputStream out3 = Files.newOutputStream(Paths.get(file3));
    ) {
    byte[] b = new byte[8192];
    int len;
    
    // 将前一半复制到 file2
    long remain = Files.size(Paths.get(file1)) / 2;
    while (remain > 0 && (len = in.read(b, 0, (int) Math.min(remain, b.length))) > 0) {
        out2.write(b, 0, len);
        remain -= len;
    }
    
    // 将剩余部分复制到 file3
    while ((len = in.read(b)) > 0) {
        out3.write(b, 0, len);
    }
}
英文:

You're misusing available(), you should use the newer NIO.2 API, and you're copying 1 byte at a time, which is very slow.

Do it like this:

try (
		InputStream in = Files.newInputStream(Paths.get(file1));
		OutputStream out2 = Files.newOutputStream(Paths.get(file2));
		OutputStream out3 = Files.newOutputStream(Paths.get(file3));
		) {
	byte[] b = new byte[8192];
	int len;
	
	// Copy first half to file2
	long remain = Files.size(Paths.get(file1)) / 2;
	while (remain &gt; 0 &amp;&amp; (len = in.read(b, 0, (int) Math.min(remain, b.length))) &gt; 0) {
		out2.write(b, 0, len);
		remain -= len;
	}
	
	// Copy the rest to file3
	while ((len = in.read(b)) &gt; 0) {
		out3.write(b, 0, len);
	}
}

huangapple
  • 本文由 发表于 2020年10月24日 16:54:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64511635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定