zoukankan      html  css  js  c++  java
  • java替换txt文本中的字符串

    public class TestDemo {

      public static String read(File src) {
        StringBuffer res = new StringBuffer();
        String line = null;
      try {
        BufferedReader reader = new BufferedReader(new FileReader(src));
        while ((line = reader.readLine()) != null) {
        res.append(line + " ");
      }
        reader.close();
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
        return res.toString();
      }

      public static boolean write(String cont, File dist) {
        try {
          BufferedWriter writer = new BufferedWriter(new FileWriter(dist));
          writer.write(cont);
          writer.flush();
          writer.close();
          return true;
        } catch (IOException e) {
          e.printStackTrace();
          return false;
        }
      }

      public TestDemo() {
      }

      public static void main(String[] args) {
        File src = new File("D:\rili.log");
        String cont = TestDemo.read(src);
        System.out.println(cont);
        // 对得到的内容进行处理
        cont = cont.replaceAll("asd", "asdf");
        System.out.println(cont);
        // 更新源文件
        System.out.println(TestDemo.write(cont, src));
      }
    }

  • 相关阅读:
    第一个SWT程序
    稀疏数组
    算法与数据结构
    《Java核心技术》学习笔记 第1-3章
    算术运算符
    5.11 rw zip file
    5.10 gob序列化
    5.9 piping between writer and reader
    5.7 io.MultiWriter(buf, f)
    5.7 读写 二进制数据
  • 原文地址:https://www.cnblogs.com/love-you-girl/p/3824006.html
Copyright © 2011-2022 走看看