Java 案例研究:这是分割文件的有效方法吗?
所以,我在学校的java课程中,一直在努力完成一项任务。为了更好地理解代码应该做什么,我将引用它: “(分割文件)假设您要备份一个大文件(例如,10 GB AVI文件)到CD-R。您可以通过将文件拆分为较小的部分并分别备份这些部分来实现。编写一个实用程序,使用以下命令将较大的文件拆分为较小的文件:javaClassNameJava 案例研究:这是分割文件的有效方法吗?,java,file-io,split,Java,File Io,Split,所以,我在学校的java课程中,一直在努力完成一项任务。为了更好地理解代码应该做什么,我将引用它: “(分割文件)假设您要备份一个大文件(例如,10 GB AVI文件)到CD-R。您可以通过将文件拆分为较小的部分并分别备份这些部分来实现。编写一个实用程序,使用以下命令将较大的文件拆分为较小的文件:javaClassName
package;
导入java.io.*;
导入java.util.*;
公开课考试{
/**
*主要方法
*
*@param args[0]用于源文件
*@param args[1]表示件数
*@抛出异常
*/
公共静态void main(字符串[]args)引发IOException{
//程序需要使用两个参数执行,以便
//工作。这句话检查一下。
如果(参数长度!=2){
System.out.println(“用法:java Copy sourceFile numberOfPieces”);
系统出口(1);
}
//检查源文件是否存在
File sourceFile=新文件(args[0]);
如果(!sourceFile.exists()){
System.out.println(“源文件”+args[0]+“不存在”);
系统出口(2);
}
//需要一个数组来存储应该包含的所有新文件
//原始文件的部分
ArrayList fileArray=新的ArrayList();
//所有新文件都需要它们自己的输出(或者它们是否需要?)
ArrayList outputArray=新的ArrayList();
//在源文件上使用randomAccessFile可以更轻松地读取部分内容
RandomAccessFile inOutSourceFile=新的RandomAccessFile(sourceFile,
“rw”);
//此循环更改新文件的名称,使其与
//附加数字的源文件
对于(int i=0;i当然有必要打开所有输出文件。但您不必一直打开它们。您可以打开第一个文件、写入、关闭、打开第二个文件、写入、关闭等等
例如,文件格式.png有一个必须遵循的结构。它可能有特殊的页眉,也可能有特殊的页脚。这就是为什么当此文件拆分为两个或两个以上时,第一个文件将丢失页脚,中间的文件将丢失页眉和页脚,最后一个文件将丢失页眉。这使得它们无法作为单个文件使用
当然有可能。通过合并所有部分,可以重新构造原始文件填充
嗯,为了合并这个文件,我会创建一个新的文件“final_fine.x”,并将来自所有部分的字节(比如file_part1.originalformat,file_part2.originalformat)等放入其中。是的,但是我想知道这个文件是否会以任何方式被更改。我应该测试一下。如果我能使用一个“FileOutput”,那就太棒了“刚刚更改了它写入的文件。我没有创建新的文件。我在代码中添加了您的建议。它现在在完成后立即关闭输出。但我没有注意到和速度的提高。”。
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.");
}
}