java:I/O 一行一行读取和写入

BufferedReader逐行读取

java:I/O 一行一行读取和写入
import java.io.*;
class Test
{
    public static void main(String args []){
        FileReader reader =null;
        BufferedReader bufferedReader =null;
        try{
        reader = new FileReader("e://d/from.txt");    
        bufferedReader = new BufferedReader(reader);
        String line =null;
        while(true){
            line =bufferedReader.readLine();
            if(line ==null){
                break;
            }
            System.out.println(line);
        }
            

        
        }catch(Exception e){
            System.out.println(e);
        }
        finally{
            try{
                bufferedReader.close();
                reader.close();

            }catch(Exception e){
                System.out.println(e);
            }
        }
    }
}
java:I/O 一行一行读取和写入

BufferedWriter逐行写入

java:I/O 一行一行读取和写入
import java.io.*;
class Test
{
    public static void main(String [] args){
        FileWriter fw =null;
        BufferedWriter bw =null;
        String [] arrs = {
            "zhang,33",
            "li,44"
            };
            try{
                fw = new FileWriter("e://d/from.txt");
                bw = new BufferedWriter(fw);
                
                for(String arr:arrs){
                     bw.write(arr+"\t\n");
                }
                /*等效于
                for(int i=0;i<arrs.length;i++){
                    bw.write(arrs[i]+"\t\n");
                }
                */

            }catch(Exception e){
                System.out.println(e);
            }
            finally{
                try{
                    bw.close();
                    fw.close();
                    
                }catch(Exception e){
                    System.out.println(e);
                }
            }
    }
}
java:I/O 一行一行读取和写入

java:I/O 一行一行读取和写入,布布扣,bubuko.com

java:I/O 一行一行读取和写入

上一篇:Load resources from classpath in Java--reference


下一篇:javascript的事件处理