美文网首页
Java读写文件

Java读写文件

作者: 一只菜鸟的笔记 | 来源:发表于2020-12-31 10:31 被阅读0次
          public void test() {
            try {
                // 写文件
                File file = new File("C:\\Users\\test");
    //          FileWriter writer = new FileWriter(file);//不追加
                FileWriter writer = new FileWriter(file, true);//追加
                BufferedWriter bw = new BufferedWriter(writer);
                bw.write("test?");
                bw.newLine();
                bw.close();
                writer.close();
                
                // 读文件
                FileReader reader = new FileReader("C:\\Users\\test");
                BufferedReader br = new BufferedReader(reader);
                String templine = null;
                while ((templine = br.readLine()) != null) {
                    System.out.println(templine);
                }
                br.close();
                reader.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

    相关文章

      网友评论

          本文标题:Java读写文件

          本文链接:https://www.haomeiwen.com/subject/eusloktx.html