VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Java教程 >
  • java 读写文件

java 读写文件

 
复制代码
//字节流
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);
        }
    }
}
}
复制代码

 

 
出  处:https://www.cnblogs.com/leiziv5/p/7898527.html


相关教程