import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;
public class CopyFile {
static final String INPUT="d:/student.txt";
static final String OUTPUT="d:/stunew.txt";
public static void main(String[] args) throws FileNotFoundException{
int iResult;
String str;
RandomAccessFile rdin=new RandomAccessFile(INPUT,"rw");
FileInputStream fisIn=new FileInputStream(INPUT);
FileOutputStream fosOut=new FileOutputStream(OUTPUT);
try{
System.out.println("添加文件内容:");
Scanner sc=new Scanner(System.in);
str=sc.next();
rdin.writeBytes(str);
System.out.println("开始复制文件"+INPUT);
do{
iResult=fisIn.read();
if(iResult!=-1){
fosOut.write(iResult);
System.out.println("...\n");
}
}
while(iResult!=-1);
System.out.println(INPUT+"已成功复制到"+OUTPUT);
fisIn.close();//关闭对象fisIn和对象fosOut
fosOut.close();
}
catch(IOException e){
e.printStackTrace();
}
}
}
运行结果: