从Filereader中删除特定单词

时间:2016-08-03 06:23:32

标签: java filereader filewriter

我有这个代码,我的目标是删除""。所以输出看起来像这样。

  

块引用
  水很湿。男人很生气。床很温暖。

我很难搞清楚要做什么,我尝试过使用replaceALL,但它并不起作用。然后输出将写入文本文件中。任何帮助都可以。

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

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


File THE = new File("a.txt");
    String replace;
    THE.createNewFile();
    FileWriter writer = new FileWriter(THE);
    writer.write("The water is wet.\n");
    writer.write("The man is angry.\n");
    writer.write("The bed is warm.");
    writer.flush();
    writer.close();

    FileReader READ = new FileReader(THE);
    char [] x = new char [70];
    READ.read(x);
        System.out.print(x);
    READ.close();
    }
}

1 个答案:

答案 0 :(得分:2)

问题是x不是String,是char [],所以你必须先将它转换为String

FileReader READ = new FileReader(THE);
char [] x = new char [70];
READ.read(x);
String xAsString = String.valueOf(x).replace("The ", "");
System.out.print(xAsString);
READ.close();