【问题标题】:Java: save file into two different files - please explain me solutionJava:将文件保存到两个不同的文件中-请解释一下解决方案
【发布时间】:2020-05-05 12:19:50
【问题描述】:

我对其中一项任务有疑问: 从控制台读取 3 个文件名:file1、file2、file3。 分割文件: 将一半的内容保存到 file2 中,另一半保存到 file3 中。 如果字节数甚至没有将更多字节保存到file2中。 关闭流。

我想知道如何解决它,唯一有效的解决方案是:

public class main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String a = reader.readLine();
        String b = reader.readLine();
        String c = reader.readLine();

        FileInputStream fileInputStream1 = new FileInputStream(a);
        FileOutputStream fileOutputStream2 = new FileOutputStream(b);
        FileOutputStream fileOutputStream3 = new FileOutputStream(c);

        byte[] buffer = new byte[fileInputStream1.available()];

        if (fileInputStream1.available() % 2 != 0) {
            while (fileInputStream1.available() > 0) {
                int count = fileInputStream1.read(buffer);
                fileOutputStream2.write(buffer, 0, count / 2+1);
                fileOutputStream3.write(buffer, count / 2+1, count/2);
            }
        } else {
            while (fileInputStream1.available() > 0) {
                int count = fileInputStream1.read(buffer);
                fileOutputStream2.write(buffer, 0, count / 2);
                fileOutputStream3.write(buffer, count / 2, count/2);
            }
        }

        fileInputStream1.close();
        fileOutputStream2.close();
        fileOutputStream3.close();
    }
}

我的问题是:为什么我必须从 count/2 保存到 count/2?这对我来说没有任何意义。如果我要使用数字,我们假设 file1 有 100 个字节。我从 0 保存到 count/2(100/2=50),从 count/2 保存到 count/2(从 100/2=50 到 100/2=50 甚至 50/2=25)。 在我看来,它应该是从 0 到 count/2 和从 count/2 到 count 或 buffer.length

请解释为什么我的解决方案与正确的解决方案相比是错误的。 谢谢。

【问题讨论】:

  • OutputStream.write的文档。第三个参数是要写入的数组部分的length,而不是位置。此外,您的解决方案不正确,因为 available() 没有给您文件中的字节数,只有在下一次读取操作中可读取的字节数。这也只是一个估计。阅读文档非常重要。
  • 太棒了,这帮助了我。我会记得经常检查文档!

标签: java file fileinputstream fileoutputstream


【解决方案1】:

这里是解释。第一个程序创建一个缓冲区读取器,其源是键盘输入。然后程序逐行读取并将结果保存在适当的字符串中。然后程序制作用于在文件中读取和写入的流(所谓的文件流)。之后,读取输入文件中的所有字节并将其放入缓冲区字节数组中。第一的 我们需要检查文件中的字节数是偶数还是奇数,然后执行将字节复制到数组中的适当操作。 请记住,在复制缓冲区数组时,范围在结束时是独占的,在开始时是包括在内的,例如Arrays.copyOfRange(buffer, 0, buffer.length / 2) 在此区间 [0,buffer.length / 2> 中复制字节。现在我们只需要在各自的文件中写入字节。希望这会有所帮助。

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

       BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        String inputFile = reader.readLine();
        String outFile2 = reader.readLine();
        String outFile3 = reader.readLine();

        FileInputStream  fileInputStream1  = new FileInputStream(inputFile);
        FileOutputStream fileOutputStream2 = new FileOutputStream(outFile2);
        FileOutputStream fileOutputStream3 = new FileOutputStream(outFile3);

        byte[] buffer = Files.readAllBytes(Paths.get(inputFile));
        byte[] bytesForOut2, bytesForOut3;

        if(buffer.length % 2 == 0) {
            bytesForOut2 = Arrays.copyOfRange(buffer, 0, buffer.length / 2);
            bytesForOut3 = Arrays.copyOfRange(buffer, buffer.length /2, buffer.length);
        }
        else {
            bytesForOut2 = Arrays.copyOfRange(buffer, 0, buffer.length / 2 + 1);
            bytesForOut3 = Arrays.copyOfRange(buffer, buffer.length /2 + 1, buffer.length);
        }

        fileOutputStream2.write(bytesForOut2);
        fileOutputStream3.write(bytesForOut3);

        fileInputStream1.close();
        fileOutputStream2.close();
        fileOutputStream3.close();

}

【讨论】:

  • 这不适用于大于 2GB 的文件,并且取决于堆大小,也可能不适用于较小的文件。并且执行copyOfRange 意味着您实际上将内存需求增加了一倍 - 最初使用write(buf,from,len) 会更节省内存。
  • @RealSkeptic 感谢您的反馈。我很感激。我是初学者,所以经验丰富的程序员的建议总是有用的。我的解决方案还有更多 cmets 吗?
猜你喜欢
  • 1970-01-01
  • 2020-03-26
  • 1970-01-01
  • 1970-01-01
  • 2010-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多