从文件中读取文本并更正它(逗号和点)[Java]

时间:2014-12-09 15:08:36

标签: java filereader

我必须更正文件中的文字。
什么时候是逗号或点我必须改变到正确的位置,例如
"这是一些文字,请更正。本文。 " to"这是一些文字,请更正。这个文字。"
我注意到我的代码无法正常工作。对于点,他根本不起作用,逗号之前加上逗号制作空格。
你有任何提示吗?

FileReader fr = null;
 String line = "";
 String result="";
 String []array;
 String []array2;
 String result2="";
   // open the file
   try {
     fr = new FileReader("file.txt");
   } catch (FileNotFoundException e) {
       System.out.println("Can not open the file!");
       System.exit(1);
   }

   BufferedReader bfr = new BufferedReader(fr);
   // read the lines:
   try {
     while((line = bfr.readLine()) != null){

         array=line.split(",");
         for(int i=0;i<array.length;i++){
           //if i not equal to end(at the end has to be period)
             if(i!=array.length-1){
             array[i]+=",";
             }
            result+=array[i];
         }
      //  System.out.println(result);

        array2=result.split("\\.");
         for(int i=0;i<array2.length;i++){
             System.out.println(array2[i]);
             array[i]+="\\.";
             result2+=array2[i]; 
         }
          System.out.println(result2);
     }
    } catch (IOException e) {
        System.out.println("Can not read the file!");
        System.exit(2);
   }

   // close the file
   try {
     fr.close();
    } catch (IOException e) {
         System.out.println("error can not close the file");
         System.exit(3);
        }

1 个答案:

答案 0 :(得分:0)

我们首先假设您可以使用正则表达式。这是一个简单的方法来做你想要的:

import java.io.*;

class CorrectFile 
{
  public static void main(String[] args) 
  {
    FileReader fr = null;
    String line = "";
    String result="";
    // open the file
    try {
      fr = new FileReader("file.txt");
    } catch (FileNotFoundException e) {
        System.out.println("Can not open the file!");
        System.exit(1);
    }

    BufferedReader bfr = new BufferedReader(fr);
    // read the lines:
    try {
         while((line = bfr.readLine()) != null){
            line = line.trim().replaceAll("\\s*([,,.])\\s*", "$1 ");
            System.out.println(line);
        }
    } catch (IOException e) {
        System.out.println("Can not read the file!");
        System.exit(2);
   }

   // close the file
   try {
     fr.close();
   } catch (IOException e) {
     System.out.println("error can not close the file");
     System.exit(3);
   }
  }
}

最重要的是这一行:line = line.trim().replaceAll("\\s*([,,.])\\s*", "$1 ");。首先,您阅读的每一行可能在两端都包含空格。如果是这样,String.trim()将删除它们。接下来,取下字符串(两端都删除了空格),我们想用“逗号+空格”替换“多个空格+逗号+多个空格”之类的内容“dot”。“\ s”是空格的正则表达式,“\ s *”是“零或任意数量的空格”的正则表达式。“[]”表示字符组,“[,,。]”表示“,” “或者”。“和中间的逗号只是一个分隔符。这里我们需要为字符串转义”\“,所以现在我们有”\ s *([,,。])\ s *“这意味着让我们替换一些任意的空格的数量后跟一个“,”或“。”,后跟任意数量的空格,后面跟着一个空格或“。”后跟一个空格。这里的括号是元素在它里面是一个捕获组,用于“保存”找到的匹配(这里是“,”或“。”),我们稍后在我们的例子中使用它作为“$ 1”。所以我们将能够替换我们找到的匹配是“,”或“。”无论匹配是什么。因为你需要逗号或点后面的空格,我们添加一个空格,使其成为“1美元”。

现在,让我们看看你原来的东西有什么问题,为什么我说String.split()可能不是一个好主意。

除了你正在创建大量新的String对象之外,最明显的问题是你(可能不在拼写错误中)使用array[i]+=".";而不是array2[i]+=".";。但是最明显的问题是来自String.split()方法,它实际上包含了分割数组中String段的空格。最后一个数组元素甚至只包含一个空格。

相关问题