码迷,mamicode.com
首页 > 编程语言 > 详细

java 读写文件

时间:2017-11-26 12:45:18      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:inpu   public   buffer   user   final   handle   output   buffered   pack   

//字节流
import java.io.*;
public class testchaer {
    public static void main(String  args[]) {
    FileReader fr=null;
    FileWriter fw=null;
    try {
        fr=new FileReader("C:/Users/Administrator/Desktop/xuexi/jiankongjiao.py");
        fw=new FileWriter("C:/Users/Administrator/Desktop/xuexi/xuexi11.txt");
        char [] buffer=new char[100];
        while (true) {
            int tem=fr.read(buffer, 0, buffer.length);
            if (tem==-1){
                break;
            }
            fw.write(buffer,0,tem);
        }
    } catch (Exception e) {
        System.out.println(e);
    }
    finally {
        try {
            fr.close();
            fw.close();
        } catch (Exception e2) {
            // TODO: handle exception
        }
    }
}
}
//字符流
import java.io.*;
public class inpur {
   public static void main(String  args[]) {
    FileInputStream fis=null;
    FileOutputStream fos=null;
       try{
           fis=new FileInputStream("C:/Users/Administrator/Desktop/xuexi/jiankongjiao.py");
           fos= new FileOutputStream("C:/Users/Administrator/Desktop/xuexi/xuexi2.txt");
           byte [] buffer=new byte[100];
           while (true){
               int temp=fis.read(buffer,0,buffer.length);
               if(temp==-1){
                   break;
               }
               fos.write(buffer,0,temp);
               String stri=new String(buffer);
               //调用string对象的tim方法,将会将string的收尾的空格和空字符串
               stri=stri.trim();
               System.out.println(stri);
           }  
       }
       catch (Exception e) {
        // TODO: handle exception
           System.out.println(e);
       }
       finally{
            try {
                fis.close();
                fos.close();
            } catch (Exception e) {
                System.out.println(e);
            }
            }
}
}

 

一行一行的读了,

package ifwh;
import java.io.*;
public class teatsjaa {
 public static void main(String agrs[]) {
    FileReader fileReader=null;
    BufferedReader  bufferedWriter=null;
    try {
        fileReader=new FileReader("C:/Users/Administrator/Desktop/xuexi/jiankongjiao.py");
        bufferedWriter =new BufferedReader(fileReader);
        String line=null;
        while (true) {
            line=bufferedWriter.readLine();
            if (line==null){
                break;
            }
            System.out.println(line);
        }
    } catch (Exception e) {
        // TODO: handle exception
        System.out.println(e);
    }finally {
        try {
            fileReader.close();
            bufferedWriter.close();
        } catch (Exception e2) {
            // TODO: handle exception
            System.out.println(e2);
        }
    }
}
}

 

java 读写文件

标签:inpu   public   buffer   user   final   handle   output   buffered   pack   

原文地址:http://www.cnblogs.com/leiziv5/p/7898527.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!