JAVA IO流的简单示例

来源:互联网 发布:购买火车票的软件 编辑:程序博客网 时间:2024/04/29 19:39
import java.io.*;class Test{public static void main(String args[]){FileInputStream fis=null;FileOutputStream fos=null;try{fis=new FileInputStream("d:/src/from.txt");fos=new FileOutputStream("d:/src/to.txt");byte[] buffer=new byte[1024];while(true){int temp=fis.read(buffer,0,buffer.length);if(temp==-1){break;}fos.write(buffer,0,temp);}}catch(Exception e){System.out.println(e);}finally{try{fis.close();fos.close();}catch(Exception e){System.out.println(e);}}}}

以上为字节流




import java.io.*;class TestChar{public static void main(String args[]){FileReader fr=null;FileWriter fw=null;try{fr=new FileReader("d:/src/md5.txt");fw=new FileWriter("d:/src/to.txt");char[] buffer=new char[1024];while(true){int temp=fr.read(buffer,0,buffer.length);if(temp==-1){break;}fw.write(buffer,0,temp);}}catch(Exception e){System.out.println(e);}finally{try{fr.close();fw.close();}catch(Exception e){System.out.println(e);}}}}
以上为字符流


结果是将from.txt中的字母复制到to.txt中
原创粉丝点击