java实现字符串追加写入文本,一行一行读取文本

java实现字符串追加写入文本,一行一行读取文本

  • 一、字符串追加写入文本
  • 二、一行一行读取文本字符串

一、字符串追加写入文本


 public static void fileWrite() {
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter("D://bbb.txt", true));
            bw.write("你好");
            // bw.write("\r\n"); //只支持windows系统
            bw.newLine(); // 跨平台的,支持linux和windows
            bw.write("世界");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                bw.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }

二、一行一行读取文本字符串

 public static List<String> fileRead() {
        BufferedReader bufferReader = null;
        List<String> result = new ArrayList<>();
        try {
            bufferReader = new BufferedReader(new FileReader("D://bbb.txt"));
            String tempString;
            while ((tempString = bufferReader.readLine()) != null) {
                result.add(tempString);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                bufferReader.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return result;
    }

你可能感兴趣的:(研究,java,开发语言)