Java:将文件保存到两个不同的文件中 – 请为我解释解决方案。

huangapple 未分类评论50阅读模式
英文:

Java: save file into two different files - please explain me solution

问题

我遇到了一个任务的问题:
从控制台读取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。

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

英文:

I have a problem with one of the tasks:
Read 3 file names from console: file1, file2, file3.
Divide file:
Save half of bites into file2 and second half into file3.
If number of bytes is not even save more bytes into file2.
Close streams.

I was wondering how to solve it and the only solution that works properly is:

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();
    }
}

My question is: why do I have to save from count/2 to count/2? It does not make any sense to me. If I will use numbers, let's assume that file1 has 100 bytes. I save from 0 to count/2 (100/2=50) and from count/2 to count/2 (from 100/2=50 to 100/2=50 or even 50/2=25).
In my opinion it should be from 0 to count/2 and from count/2 to count or buffer.length

Please explain me why my solutions are wrong comparing to correct one.
Thank you.

答案1

得分: 0

这是解释。第一个程序创建一个缓冲读取器,其数据源是键盘输入。然后程序逐行读取并将结果保存在适当的字符串中。接下来,程序创建用于读写文件的流(称为文件流)。然后,从输入文件中读取所有字节并放入缓冲字节数组中。首先,我们需要检查文件中的字节数是奇数还是偶数,然后根据情况执行将字节复制到数组中的适当操作。在复制缓冲区数组时,请注意范围在结尾时是排除在外的,在开头时是包含的,例如 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();

}
英文:

Here is the explanation.First program makes a buffer reader whose source is keyboard entry. Then program reads line by line and saves the results in appropriate strings. Then program makes streams for reading and writing in files (so called file streams). After that all of the bytes from input file are read and put in buffer byte array. First
we need to cheek if byte number in file is even or odd and then do the appropriate actions of copying bytes into arrays.
Have in mind when copying buffer array than range is exclusive at end and inclusive at start e.g. Arrays.copyOfRange(buffer, 0, buffer.length / 2) copies bytes in this interval [0,buffer.length / 2>. Now we only need to write the bytes in respective files. Hope this helps.

       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();

}

huangapple
  • 本文由 发表于 2020年5月5日 20:19:50
  • 转载请务必保留本文链接:https://java.coder-hub.com/61612992.html
匿名

发表评论

匿名网友

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

确定