用JAVA语言将两个文本文件(文件A和文件B)中的内容合并,并写到一个新的文件(C)中,用标准的输入和输出流来
2个回答
展开全部
import java.io.*;
public class Test1 {
public static void main(String[] args){
try {
java.io.File file = new java.io.File("123.txt");
java.io.FileOutputStream fos = new java.io.FileOutputStream(file);
FileInputStream fis = new FileInputStream(new File("haha.txt"));
FileInputStream fis1 = new FileInputStream(new File("abc.txt"));
byte[] b = new byte[1];
while((fis.read(b)) != -1){
fos.write(b);
}
while((fis1.read(b)) != -1){
fos.write(b);
}
fos.flush();
System.out.println("success!");
}
catch(Exception e){System.out.println("error: " + e);}
}
}
代码二(与代码一基本相似,只是先合并输入流):
import java.io.*;
public class Test2 {
public static void main(String[] args){
try {
java.io.File file = new java.io.File("123.txt");
java.io.FileOutputStream fos = new java.io.FileOutputStream(file);
FileInputStream fis = new FileInputStream(new File("haha.txt"));
FileInputStream fis1 = new FileInputStream(new File("abc.txt"));
SequenceInputStream sis = new SequenceInputStream(fis,fis1);
byte[] b = new byte[1];
while((sis.read(b)) != -1){
fos.write(b);
}
fos.flush();
System.out.println("success!");
}
catch(Exception e){System.out.println("error: " + e);}
}
}
代码三(使用RandomFile):
import java.io.*;
public class Test3{
public static void main(String[] args){
try {
RandomAccessFile outt= new RandomAccessFile(new java.io.File("123.doc"),"rw");
RandomAccessFile inn= new RandomAccessFile(new File("haha.doc"),"r");
int c;
while((c=inn.read())!=-1)
outt.write(c);
RandomAccessFile inn1= new RandomAccessFile(new File("abc.doc"),"r");
while((c=inn1.read())!=-1)
outt.write(c);
System.out.println("success!");
}
catch(Exception e){System.out.println("error: " + e);}
}
}
public class Test1 {
public static void main(String[] args){
try {
java.io.File file = new java.io.File("123.txt");
java.io.FileOutputStream fos = new java.io.FileOutputStream(file);
FileInputStream fis = new FileInputStream(new File("haha.txt"));
FileInputStream fis1 = new FileInputStream(new File("abc.txt"));
byte[] b = new byte[1];
while((fis.read(b)) != -1){
fos.write(b);
}
while((fis1.read(b)) != -1){
fos.write(b);
}
fos.flush();
System.out.println("success!");
}
catch(Exception e){System.out.println("error: " + e);}
}
}
代码二(与代码一基本相似,只是先合并输入流):
import java.io.*;
public class Test2 {
public static void main(String[] args){
try {
java.io.File file = new java.io.File("123.txt");
java.io.FileOutputStream fos = new java.io.FileOutputStream(file);
FileInputStream fis = new FileInputStream(new File("haha.txt"));
FileInputStream fis1 = new FileInputStream(new File("abc.txt"));
SequenceInputStream sis = new SequenceInputStream(fis,fis1);
byte[] b = new byte[1];
while((sis.read(b)) != -1){
fos.write(b);
}
fos.flush();
System.out.println("success!");
}
catch(Exception e){System.out.println("error: " + e);}
}
}
代码三(使用RandomFile):
import java.io.*;
public class Test3{
public static void main(String[] args){
try {
RandomAccessFile outt= new RandomAccessFile(new java.io.File("123.doc"),"rw");
RandomAccessFile inn= new RandomAccessFile(new File("haha.doc"),"r");
int c;
while((c=inn.read())!=-1)
outt.write(c);
RandomAccessFile inn1= new RandomAccessFile(new File("abc.doc"),"r");
while((c=inn1.read())!=-1)
outt.write(c);
System.out.println("success!");
}
catch(Exception e){System.out.println("error: " + e);}
}
}
参考资料: http://hi.baidu.com/lanruijin/blog/item/6a4f4f1e851088f51bd576fe.html
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询