gpt4 book ai didi

java - 案例研究 : Is this an effective way to split a file?

转载 作者:行者123 更新时间:2023-12-01 13:29:15 25 4
gpt4 key购买 nike

所以,我正在努力完成学校 Java 类(class)中的一项任务。为了更好地理解代码应该做什么,我引用它:

“(分割文件)假设您要将一个大文件(例如,10 GB AVI 文件)备份到 CD-R 上。您可以通过将文件分割成较小的部分并分别备份这些部分来实现. 编写一个实用程序,使用以下命令将大文件拆分为较小的文件:java ClassName SourceFile 件数

该命令创建文件 SourceFile.1SourceFile2...等

现在要澄清一下。这篇文章绝不是试图找到问题的“解决方案”。我已经解决了(用我所知道的)。我只是想对编写代码时想到的一些问题有更多的了解。

  1. 是否有必要为每个文件创建一个新的输出复制到?这是否需要不必要的系统电源?
  2. 第一个被复制的文件(SourceFile 在本例中是 .png文件)可以查看。并显示原始的一小部分图片。 (如果我分成两部分。我可以看到一半的图片。)但是后面的我看不到……为什么?
  3. 是否可以以任何方式重新组合分割的文件?如果我的图片被分成两个文件,我可以将它们重新组合在一起吗查看全图?

代码,如果你想看的话。

欢迎所有反馈,祝你有美好的一天! :)

package oblig2;

import java.io.*;
import java.util.*;

public class Test {

/**
* Main method
*
* @param args[0] for source file
* @param args[1] for number of pieces
* @throws IOException
*/
public static void main(String[] args) throws IOException {

// The program needs to be executed with two parameters in order to
// work. This sentence check for it.
if (args.length != 2) {
System.out.println("Usage: java Copy sourceFile numberOfPieces");
System.exit(1);
}
// Check whether or not the sourcefile exists
File sourceFile = new File(args[0]);
if (!sourceFile.exists()) {
System.out.println("Source file " + args[0] + " does not exist");
System.exit(2);
}
// Need an Array to store all the new files that is supposed to contain
// parts of the original file
ArrayList<File> fileArray = new ArrayList<File>();

// All the new files need their own output(or do they?)
ArrayList<BufferedOutputStream> outputArray = new ArrayList<BufferedOutputStream>();

// Using randomAccessFile on the sourcefile to easier read parts of it
RandomAccessFile inOutSourceFile = new RandomAccessFile(sourceFile,
"rw");

// This loop changes the name for the new files, so they match the
// sourcefile with an appended digit
for (int i = 0; i < Integer.parseInt(args[1]); i++) {
String nameAppender = String.valueOf(i);
String nameBuilder;
int suffix = args[0].indexOf(".");
nameBuilder = args[0].substring(0, suffix);
fileArray.add((new File(nameBuilder + nameAppender + ".dat")));
}

// Here i create the output needed for all the new files
for (int i = 0; i < Integer.parseInt(args[1]); i++) {
outputArray.add(new BufferedOutputStream(new FileOutputStream(
new File(fileArray.get(i).getAbsolutePath()))));
}

// Now i determine in how many parts the sourcefile needs to be split,
// and the size of each.
float size = inOutSourceFile.length();
double parts = Integer.parseInt(args[1]);
double partSize = size / parts;
int r, numberOfBytesCopied = 0;


// This loop actually does the job of copying the parts into the new
// files
for (int i = 1; i <= parts; i++) {
while (inOutSourceFile.getFilePointer() < partSize * i) {
r = inOutSourceFile.readByte();
outputArray.get(i - 1).write((byte) r);
numberOfBytesCopied++;
}

}
// Here i close the input and outputs
inOutSourceFile.close();
for (int i = 0; i < parts; i++) {
outputArray.get(i).close();
}

// Display the operations
System.out.println(args[0] + " Has been split into " + args[1]
+ " pieces. " + "\n" + "Each file containig " + partSize
+ " Bytes each.");

}

}

最佳答案

  1. 当然需要打开所有输出文件。但您不必始终打开它们。您可以打开第一个文件,对其进行写入,关闭它,打开第二个文件,对其进行写入,关闭它,等等。
  2. 文件格式(例如 .png)具有必须遵循的结构。它可能有特殊的页眉,也可能有特殊的页脚。这就是为什么当这个文件分成两个或多个时,第一个文件将失去页脚,中间的文件将失去页眉和页脚,最后一个文件将失去页眉。这使得它们无法作为单独的文件使用。
  3. 当然有可能。通过组合回所有部分,原始文件将被重构。

关于java - 案例研究 : Is this an effective way to split a file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21660061/

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