package dan;
import java.io.*;
import java.util.*;
public class kus {
static final String input="d:/student.txt";//定义输入文件路径
static final String output="d:/student.txt";//定义输出文件路径
public static void main(String[] args) throws FileNotFoundException{
// TODO 自动生成的方法存根
int ir;
String str;
RandomAccessFile rd=new RandomAccessFile(input,"rw");//创建具有读/写功能RandomAccessFile对象rd
FileInputStream fis=new FileInputStream(input);//创建 文件读入流对象fis
FileOutputStream fos=new FileOutputStream(output);//创建文件写出流对象fos
try{
System.out.println("添加文件内容");//从键盘上输入信息
Scanner sc=new Scanner(System.in);
str=sc.next();
rd.writeBytes(str);//将读入的字符串信息写入RandomAccessFile对象rd
System.out.println("开始复制文件"+input);
do{//将对象fis中的内容写入对象fos,即实现文件内容复制的功能,到文件结尾ir=-1为止
ir=fis.read();
if(ir!=-1){
fos.write(ir);
System.out.println("...\n");
}
}while(ir!=-1);
System.out.println(input+"已成功复制到"+output);
fis.close();//关闭对象fis和对象fos
fos.close();
}
catch(IOException e){
e.printStackTrace();
}
}
}
运行结果:
知识点:
1.字节流:字节流分为输入和输出.输入流是指能够读出一系列字节的对象. 输出流是指能够写入一系列的对象.
2.Input Stream类:字节流的抽象类
3.Output Stream 类:字节流的抽象类
4.File Input Stream 类: 如果要将数据源文件读入程序,则需要.File Input Stream实现.
5.File Output Stream 类: 如果要将程序内部的数据源文件输出程序,则需要.File Input Stream实现