gpt4 book ai didi

Java:将文件保存到两个不同的文件中 - 请解释一下解决方案

转载 作者:行者123 更新时间:2023-12-01 16:54:35 25 4
gpt4 key购买 nike

我在执行其中一项任务时遇到问题:从控制台读取 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

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

最佳答案

这是解释。第一个程序创建一个缓冲区读取器,其源是键盘输入。然后程序逐行读取并将结果保存在适当的字符串中。然后程序创建用于在文件中读写的流(所谓的文件流)。之后,读取输入文件中的所有字节并将其放入缓冲区字节数组中。第一的我们需要检查文件中的字节数是偶数还是奇数,然后执行将字节复制到数组中的适当操作。请记住,在复制缓冲区数组时,范围在结束时是独占的,在开始时是包含的,例如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();

}

关于Java:将文件保存到两个不同的文件中 - 请解释一下解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61612992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com