java FileInputStream和FileOutputStream
用字节流FileInputStream和FileOutputStream将文件a.txt和b.txt逐个字节读出来复制到c.txt中首先对文件a.txt和b.txt建立F...
用字节流FileInputStream和FileOutputStream将文件a.txt和b.txt逐个字节读出来复制到c.txt中
首先对文件a.txt和b.txt建立FileInputStream文件流fin1和fin2,然后调用read函数,对文件c.txt建立FileOutputStream文件流fout,分别将fin1和fin2调用read方法读出来的数据write到fout中。
2、修改题目1的代码,以字符流的方式实现文件的合并。
使用BufferedReader和BufferedWriter。 展开
首先对文件a.txt和b.txt建立FileInputStream文件流fin1和fin2,然后调用read函数,对文件c.txt建立FileOutputStream文件流fout,分别将fin1和fin2调用read方法读出来的数据write到fout中。
2、修改题目1的代码,以字符流的方式实现文件的合并。
使用BufferedReader和BufferedWriter。 展开
1个回答
展开全部
public static void main(String[] args) throws IOException {
File a = new File("a.txt");
FileInputStream fin1 = new FileInputStream(a);
byte[] a_cont = new byte[1024];
int a_len = fin1.read(a_cont);
File b = new File("b.txt");
FileInputStream fin2 = new FileInputStream(a);
byte[] b_cont = new byte[1024];
int b_len = fin2.read(b_cont);
File c = new File("c.txt");
FileOutputStream fout = new FileOutputStream(c);
if (a_len != -1) {
fout.write(a_cont);
}
if (b_len != -1) {
fout.write(b_cont);
}
fout.flush();
fout.close();
fin1.close();
fin2.close();
}
public static void main(String[] args) throws IOException {
File a = new File("d:/a.txt");
FileInputStream fin1 = new FileInputStream(a);
BufferedReader br1 = new BufferedReader(new InputStreamReader(fin1));
File c = new File("d:/c.txt");
FileOutputStream fout = new FileOutputStream(c);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fout));
String a_cont = "";
while ((a_cont = br1.readLine()) != null)
{
bw.write(a_cont);
}
bw.append("\r\n");
File b = new File("d:/b.txt");
FileInputStream fin2 = new FileInputStream(b);
BufferedReader br2 = new BufferedReader(new InputStreamReader(fin2));
String b_cont = "";
while ((b_cont = br2.readLine()) != null)
{
bw.append(b_cont);
}
br1.close();
br2.close();
bw.close();
fout.close();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询