java逐行写入读取文本

来源:互联网 发布:系统更新软件下载 编辑:程序博客网 时间:2024/05/16 07:54

逐行写入文本

public class TestFileWriter {
    public static void main(String[] args) {
        FileWriter fw = null;
        try {
            fw = new FileWriter("d://test.txt",true);
            String c = "abs"+"/r/n";
            fw.write(c);
            fw.close();
        } catch (IOException e1) {
            e1.printStackTrace();
            System.out.println("写入失败");
            System.exit(-1);
        }
    }
}

逐行读取文本

public static final void readF1(String filePath) throws IOException {     
        BufferedReader br = new BufferedReader(new InputStreamReader(
        new FileInputStream(filePath)));
        for (String line = br.readLine(); line != null; line = br.readLine()) {
                           System.out.println(line);               

        }
        br.close();
    }

原创粉丝点击