文本操作工具类

来源:互联网 发布:大数据定义和概念 编辑:程序博客网 时间:2024/06/06 01:07
 public static void contentToTxt(String filePath, List<String> content) {  
       String str = new String(); //原有txt内容  
       String s1 = new String();//内容更新  
       try {  
           File f = new File(filePath);  
           if (f.exists()) {  
               System.out.print("文件存在");  
           } else {  
               System.out.print("文件不存在");  
               f.createNewFile();// 不存在则创建  
           }  
           BufferedReader input = new BufferedReader(new FileReader(f));  
 
           while ((str = input.readLine()) != null) {  
               s1 += str + "\n";  
           }  
           System.out.println(s1);  
           input.close();  
           for(int i=0;i<content.size();i++) {
            System.out.println(content.get(i));
            s1 += content.get(i);
           }
           //s1 += content;  
 
           BufferedWriter output = new BufferedWriter(new FileWriter(f));  
           output.write(s1);  
           output.close();  
       } catch (Exception e) {  
           e.printStackTrace();  
 
       }  
   }  
原创粉丝点击