2017-04-04 77 views
1

我想找到一个文件编号的模式(这是我成功的做到了),将其转换成我想要的数字格式通过使用类的另一种方法(这也是我成功地做了)...替换字符串文本文件的某些部分

我的问题是,我想,以取代以前的数字格式的新数字格式(我不知道怎么做),并将其写入到一个新文件。

String INPUT = "D:\\input.txt"; 
    String OUTPUT = "D:\\output.txt"; 
    String all = ""; 
    Pattern regexp = Pattern.compile("\"(\\d{14}.{2}\\d{4})\""); 
    try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(INPUT), "UTF-8"))) { 
     String CurrentLine; 
     while ((CurrentLine = in.readLine()) != null) { 
      Matcher matcher = regexp.matcher(CurrentLine); 
       if (matcher.find()) { 
        String number1 = CurrentLine.substring(10,20); 
        String number2 = CurrentLine.substring(30,40); 
        number1 = convertor(number1); 
        number2 = convertor(number2); 
       } 
      all = all + sCurrentLine + "\r\n"; \\create output to use for BufferedWriter 
     } 

顺便说一句,上面的代码是(因为它很明显)只是我的整个代码的一部分。

+0

目前还不清楚转换器的作用是什么... – freedev

+0

假设数字格式是这样的:23 + 3转换器方法将数字相加并返回26 + 0。输入是20个字符,转换器输出也是20个字符。 – aminspy12

回答

0

要保存你的输出就在环的末端执行此。

Files.write(Paths.get(OUTPUT), all.getBytes(), StandardOpenOption.CREATE); 

matcher对象甚至返回其中组(\d{14}.{2}\d{4})已经相匹配的位置。我建议仔细检查这部分代码以帮助自己。

使用matcher.start(1)有以前的匹配操作期间,由该组捕获的子序列的初始索引。

+0

我确切地知道在那些行(有数字)中找到字符串的位置。这matcher.start(1)帮助我替换转换后的模式? – aminspy12

+0

这只是一个建议......如果你已经知道如何做到这么好 – freedev

+0

谢谢,但我怎样才能用原始数字替换转换后的数字? – aminspy12

相关问题