2014-04-29 26 views
0

我想創建一個jar文件,它分析目錄中的所有文件尋找提供的字符串,並用提供的字符串+ .getInstance()替換它,我有這樣的代碼:Java FileIO錯誤沒有分析所有類

public static String toAnalyze; 
    public static String path; 

    public static void main(String[] args) { 
     if (args.length != 2) { 
      System.out.println("Usage: java InstanceFixer <To Analyze> <Path>"); 
      System.exit(1); 
     } 

     toAnalyze = args[0]; 
     path = args[1]; 

     SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); 

     Date resultDate = new Date(System.currentTimeMillis()); 
     LoggingUtils.log("Instance Fixer","Started InstanceFixer on analysis " + toAnalyze + " at " + sdf.format(resultDate)); 

     startIterating(); 
    } 

    public static void startIterating() { 
     File dir = new File(path); 
     System.out.println(dir); 
     File[] directoryListing = dir.listFiles(); 
     if (directoryListing != null) { 
      for (File child : directoryListing) { 
        filterFile(child); 
      } 
      LoggingUtils.log("Iterator","Analyzed " + directoryListing.length + " files."); 
     } else { 
      LoggingUtils.log("Iterator", path + " is not valid \n won't be able to Analyze files (if any)."); 
     } 
    } 

    public static void filterFile(File file) { 
     try { 
      BufferedReader in = new BufferedReader(new FileReader(file)); 
      BufferedWriter out = new BufferedWriter(new FileWriter(file, true)); 
      String read = in.readLine(); 
       if (read.contains(toAnalyze + "Manager")) { 
        out.write(read.replace(toAnalyze + "Manager", toAnalyze + "Manager.getInstance()")); 
        LoggingUtils.log("Filter","Analyzed file " + file.getName() + ", found and fixed instance."); 
        out.close(); 
       } else { 
        LoggingUtils.log("Filter","Analyzed file " + file.getName() + ", didn't find anything to rename, skipped."); 
       } 
      in.close(); 
     } catch (IOException e) { 
      LoggingUtils.log("Filter","There was a problem: " + e); 
      e.printStackTrace(); 
     } 
    } 

問題是,它只是在第一行或最後一行進行搜索,而不是將其寫入舊文本的文本。我能做些什麼來解決這個問題?

回答

1

而不是替換它並將其寫入文本舊

的一側在new FileWriter(file, true)參數true被設置append選項意味着作家將在文件的末尾添加新的內容。將其設置爲false,另一方面意味着文件將被清理和作家將開始從開始寫它(這可能不是你想要的,如果它包含一個以上的線)。

但無論如何你不應該讀取和寫入您在同一時間分析相同的文件。
相反

  • 創建新的臨時文件,
  • 迭代在你的原始文件的所有行,
  • 寫入線到臨時文件 - 與你想的變化。
  • 刪除原始文件
  • 重命名臨時文件具有相同的名稱爲原來的。

例如:可以說,我們要行號添加新線列所以其實我們要編輯的每一行,並在其開始像xxx:地方添加x代表數量(或空間,如果數量沒有三位數字)。所以我們可以像

try { 
    File original = new File("input.txt"); 
    File tmp = new File("tmp.txt"); 
    BufferedReader in = new BufferedReader(new FileReader(original)); 
    PrintWriter out = new PrintWriter(new BufferedWriter(
      new FileWriter(tmp))); 
    String line = null; 
    int i = 1; 
    while ((line = in.readLine()) != null) { 
     out.format("%3d: %s%n", i++, line);// %d digit, %s string, %n 
              // line separator 
    } 
    in.close(); 
    out.close(); 

    // lets swap files 
    if (original.delete()) { 
     System.out.println("original file removed"); 
     if (tmp.renameTo(original)) { 
      System.out 
        .println("temporary file renamed to original file"); 
     } else { 
      System.out.println("temporary file couldn't be renamed"); 
     } 
    } else { 
     System.out.println("original file couldn't be removed"); 
    } 

} catch (IOException e) { 
    e.printStackTrace(); 
} 
+0

請問您能提供一些代碼嗎? – ItsErikR

+0

@ user3241491新增示例。這是不完全一樣的事情,你想完成,但應該給你基本的想法。 – Pshemo

+0

@ user3241491實際上最後部分(與包含數據的新版本的臨時替換一個原始文件)可以使用'Files.copy被縮短(tmp.toPath(),original.toPath(),StandardCopyOption.REPLACE_EXISTING);' – Pshemo