【Java】讀寫文字檔案

Dsp Tian發表於2017-09-02
package rw;
 
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class test {
    public static void main(String[] agrc) throws IOException
    {
        //寫入檔案
        FileOutputStream out = new FileOutputStream("test.txt");       
        for(int i=0;i<100;i++)
        {
            String s = "this line is " + i+"\n";       
            out.write(s.getBytes());       
        }
        out.close();
             
        //讀取檔案
        FileInputStream in = new FileInputStream("test.txt"); // 讀取檔案路徑 
        BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
        for(int i=0;i<100;i++)
        {
            String line = br.readLine();
            System.out.println(line);      
        }  
        br.close();
        in.close();
    }  
}

 

相關文章