用词替换的java作业

时间:2015-09-19 03:40:12

标签: java

你好,有人能帮帮我吗?家庭作业的重点是阅读一个文件,然后创建另一个文件,用“is”替换所有单词“is”,我已经完成了所有这些,但我也没有能够替换其中包含“是”的单词例如:“this,isthmus”。

import java.io.*;
import java.util.*;


public class WordChange {
public static void main(String[]args) throws Exception {

    FileReader fr = null;
    FileWriter fw = null;

    try
         {
         Scanner keyboard=new Scanner(System.in);
         System.out.println("Enter the name of the text file: ");
         String fileName=keyboard.nextLine();
         File file = new File(fileName);
         BufferedReader reader = new BufferedReader(new FileReader(file));
         String line = "", oldtext = "";
         while((line = reader.readLine()) != null)
             {

             oldtext += line + "\r\n";

         }
         reader.close();
         String replacedtext=oldtext.replaceAll("is ","was ");
         FileWriter writer = new FileWriter("output.txt");
         writer.write(replacedtext);


         writer.close();

     }
      catch (IOException ioe)
         {
         ioe.printStackTrace();
     }
}

}

1 个答案:

答案 0 :(得分:1)

这里只是猜测而不是

String replacedtext=oldtext.replaceAll("is ","was ");

这会有效吗

String replacedtext=oldtext.replaceAll(" is "," was ");

我只是猜测让我知道它是否有效