【发布时间】:2014-08-16 06:38:02
【问题描述】:
我有 2 个文本文件:
File1 - 这个文件的格式是user_id tweet_id tweet_text
文件 1
60730027 6298443824 thank you echo park. you've changed A LOT, but as long as I'm getting paid to make you move, I'm still with it! 2009-12-03 02:54:10
60730027 6297282530 fat Albert Einstein goin in right now over here!!! 2009-12-03 01:35:22
文件2
这个文件的格式是genome_id name ascii_name
4045417 Southwest Indent Southwest Indent
4045418 Southeast Point Southeast Point
下面是sn-p读取文件1的代码:
public void readfromFile() throws FileNotFoundException {
Scanner inputStream;
String source=null;
FileInputStream file = new FileInputStream("file1.txt");
String regex = "/[a-zA-Z ]+/";
Scanner fileScan = new Scanner(file);
while(fileScan.hasNextLine()){
word = fileScan.nextLine();
word = word.replaceAll(regex, "").toLowerCase();
PrintWriter outputStreamName = new PrintWriter(new FileOutputStream("temp.txt"));
outputStreamName.printf("%s",word);
}
我的意图首先是用空值替换 user_id、tweet_id、genome_id 中存在的数据。然后将大写值转换为小写。但是,现在每当此代码处理 file1 时,文本文件都不会发生任何变化。我也想知道发生了什么。当我将它输出到控制台时,我得到了输出。
预期输出:
thank you echo park youve changed a lot but as long as im getting paid to make you move im still with it
fat albert einstein goin in right now over here
【问题讨论】:
-
如果你想改变你应该写的文件,使用 PrintWriter 打开文件并写入你的数据!
-
这个
I'm和you've改成Im和youve怎么样?被接受了吗? -
@user3218114 - 我的错误。我已经从输出中删除了这些值。
-
@WajdyEssam - 我在循环中添加了以下代码。 PrintWriter outputStreamName = new PrintWriter(new FileOutputStream("temp.txt")); outputStreamName.printf("%s",word);
-
别担心,看看我的回答,如果你想要别的东西,请告诉我。
标签: java regex string replaceall