2
我有以下格式的文本文件號:從文本中刪除多餘的空格文件
196903274115371008 @266093898
Prince George takes his first public steps with his mom, Catherine, Duchess of
Cambridge.
我想刪除所有多餘的空間,同時新+行字符以外的第一個新行字符。所以我想上面是這樣的:
[email protected]
Prince George takes his first public steps with his mom, Catherine, Duchess of Cambridge.
我寫了下面的代碼:
package remove_white_space222;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Remove_white_space222 {
public static void main(String[] args) throws FileNotFoundException, IOException {
FileReader fr = new FileReader("input.txt");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("outfile.txt");
String line;
while((line = br.readLine()) != null)
{
line = line.trim(); // remove leading and trailing whitespace
line=line.replaceAll("\\s+", " ");
fw.write(line);
}
fr.close();
fw.close();
}
}
在此先感謝您的幫助,,,,
+1爲乾淨的代碼和你想要的解釋..很少見過SOF。 – TheLostMind