【Java】读写文本文件

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();
    }  
}

 

posted @ 2017-09-02 21:11  Dsp Tian  阅读(427)  评论(0编辑  收藏  举报