目录
题目:*17.12 (组合文件)
编写一个工具程序,使它能够用下面的命令,将文件组合在一起构成一个新文件:
java Exercisel7_12 SourceFlle1 ... SourceFileN TargetFile
这个命令将 SourceFile1, …, SourceFileN 合并为TargetFile
-
代码示例
编程练习题17_12combinedFile.java
package chapter_17;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class 编程练习题17_12combinedFile{
public static void main(String[] args) throws IOException, FileNotFoundException{
int length = args.length;
if(length == 0) {
System.out.println("Usage: java 编程练习题17_12combinedFile <inputFile>... <outputFile>");
System.exit(0);
}
for(int i = 0;i < length-1;i++) {
DataOutputStream output = new DataOutputStream(new FileOutputStream(args[length-1],true));
DataInputStream input = new DataInputStream(new FileInputStream(args[i]));
int bytes = input.read();
while (bytes != -1) {
output.write((char)bytes);
bytes = input.read();
}
output.close();
input.close();
}
System.out.println("Done!");
}
}
-
结果展示