小例子:
public class mm{
public static void main(String[] args){
int c=0;
FileInputStream input=new FileInputStream("d:\\test\\intfile.java");//创建一个输入流,读取d目录下的文件内容
FileOutputStream output=new FileOutputStream("E:\\test\\outfile.java"); //创建一个输出流,写入E目录下的文件
while((c=input.read()!=-1)){ //判断是否读取完毕
output.write(c); //将读取的内容写入输出流的文件中
}
}
}
当然上面的代码里面没有写try-catch。当文件中有中文的时候你会发现出现乱码,这就需要用字符流读取内容了。
字符流
public class nn{
public static void main(String [] args){
int c=0;
FileReader reader= new FileReader("d:\\java\\readfile.java"); //创建一个字符输入流对象读取d文件内容
while((c=reader.read()!=-1)){
system.out.print ((char)c); //打印出文件内容
}
}
}