2016-11-02 65 views
-1

編輯:我有10,000個相同的文件,其中有下面的文本。在10000個文件中搜索並回報一個字符串

TRANSACTION_ID=9093626660000000001,VAULT_REPORT_NAME=VUpldr_QA_1Mb_Report00001,DIMENSION=REGION:Europe;LOB:All LOB;CATEGORY:RO Reporting;CUSTOMER:All Customer;FREQUENCY:Daily;REPORT AUDIENCE:Apple RO Reporting;REPORT SUBSCRIPTION:Apple RO Reporting 

我的要求是在10k文件中替換如下。

  1. TRANSACTION_ID從90936266600000000019093626660000010000。 和VUpldr_QA_1Mb_Report00001
  2. VAULT_REPORT_NAME到 VUpldr_QA_1Mb_Report10000

所以我的輸出文件的內容將是

file 1st:

TRANSACTION_ID=9093626660000000001,VAULT_REPORT_NAME=VUpldr_QA_1Mb_Report00001,DIMENSION=REGION:Europe;LOB:All LOB;CATEGORY:RO Reporting;CUSTOMER:All Customer;FREQUENCY:Daily;REPORT AUDIENCE:Apple RO Reporting;REPORT SUBSCRIPTION:Apple RO Reporting 

file 2nd:

TRANSACTION_ID=9093626660000000002,VAULT_REPORT_NAME=VUpldr_QA_1Mb_Report00002,DIMENSION=REGION:Europe;LOB:All LOB;CATEGORY:RO Reporting;CUSTOMER:All Customer;FREQUENCY:Daily;REPORT AUDIENCE:Apple RO Reporting;REPORT SUBSCRIPTION:Apple RO Reporting 

file 10000th:

TRANSACTION_ID=9093626660000010000,VAULT_REPORT_NAME=VUpldr_QA_1Mb_Report10000,DIMENSION=REGION:Europe;LOB:All LOB;CATEGORY:RO Reporting;CUSTOMER:All Customer;FREQUENCY:Daily;REPORT AUDIENCE:Apple RO Reporting;REPORT SUBSCRIPTION:Apple RO Reporting

我寫了下面的代碼,但不工作:

import java.io.*; 
import java.nio.charset.Charset; 
import java.nio.charset.StandardCharsets; 
import java.nio.file.FileSystems; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 

public class mdScript { 




    public static void main(String[] args) throws IOException { 

     for (int i=1;i<=10000;i++){ 

     StringBuffer pathVarBuf = new StringBuffer(); 
     pathVarBuf.append("/Users/564169/Desktop/Vault_Testing/vUpldr/MD/1MbReport"); 
     pathVarBuf.append(i); 
     pathVarBuf.append(".md"); 
     //System.out.println(pathVarBuf); 
     Path path = Paths.get(pathVarBuf.toString()); 
     Charset charset = StandardCharsets.UTF_8; 

     String content = new String(Files.readAllBytes(path), charset); 
     content = content.replaceAll("VUpldr_QA_1Mb_Report00001", "RVUpldr_QA_1Mb_Report"+i); 
     int id=999900001; // (Transaction id in the org MD file is 90) 
     id = id+i; 
     content = content.replaceAll("999900001", Integer.toString(id)); 
     Files.write(path, content.getBytes(charset)); 
     System.out.println(content); 

     } 

} 

} 

我得到以下錯誤:

Exception in thread "main" java.lang.NullPointerException 
    at sun.nio.fs.UnixFileSystem.getPath(UnixFileSystem.java:267) 
    at java.nio.file.Paths.get(Paths.java:84) 
    at mdScript.main(mdScript.java:22) 
+3

鏈接記事本++ –

+1

我會做的是刪除每個包含該文本的文件,然後用新文本寫入該文件,如果沒有任何內容。 – grooveplex

+2

如果文件是**完全相同**,則替換一個文件中的文本,然後刪除其他9999個文件,並製作第一個文件的9999個副本。 – Andreas

回答

0

有很多方法可以做到這一點。你也可以通過編程來完成。

簡單的解決方案將使用TextPad 這裏是你根本就利用它的任何文本編輯器的查找文件,例如替換命令相同 TextPad find and replace in files

相關問題