利用文件输入⼀输出流编写一个实现文件复制的java应用程序,源文件名和目标文件名

2025-01-30 13:53:08
推荐回答(1个)
回答1:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class mytxt {

public static void main(String[] args) throws IOException {

String infile = args[1].toString(); // 输入文件
String fileout = "result.txt"; // 输出路径

try {
// 输出结果
File f = new File(fileout);
FileOutputStream fout = new FileOutputStream(f);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fout));

// 输入文件
BufferedReader br = null;
File file = new File(infile);
if (file.exists()) {
FileInputStream fin = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(fin));
}

String str = br.readLine();
int n = 0;
while (str != null && !str.isEmpty()) {
n++;
str = br.readLine();
bw.write(str + "\n");
}
bw.flush();
bw.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

[还望自行修改,感觉Swing 很鸡肋 。]