回答
怎麼是這樣的:
FileReader fr = new FileReader("infile.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
if (!line.equals("")) // don't write out blank lines
{
fw.write(line, 0, line.length());
}
}
fr.close();
fw.close();
注意 - 沒有經過測試,可能不是完美的語法,但給你一個想法/方法要遵循。
請參見下面的JavaDoc以供參考: http://download.oracle.com/javase/7/docs/api/java/io/FileReader.html http://download.oracle.com/javase/7/docs/api/java/io/FileWriter.html
這將刪除文件中的所有換行符。 –
看一看TRIM()函數
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#trim()
此外,一些代碼將是有益的......
這是在String對象中已經被emded的唯一有效的解決方案。絕對不需要做任何事情。這應該被設置爲答案 – Omri
'返回字符串的副本,省略了前導和尾部空白.'它不會刪除文本文件中的空白,只會導致&尾隨。這不是OP要求的。 OP想要刪除所有空格和製表符。 –
...
Scanner scanner = new Scanner(new File("infile.txt"));
PrintStream out = new PrintStream(new File("outfile.txt"));
while(scanner.hasNextLine()){
String line = scanner.nextLine();
line = line.trim();
if(line.length() > 0)
out.println(line);
}
...
的每一行中的空格,不考慮空和空行:
String line = buffer.readLine();
while (line != null) {
line = removeSpaces(line);
//ignore empty lines
if (line.isEmpty()) return;
....code....
line = buffer.readLine();
}
public String removeSpaces (String arg)
{
Pattern whitespace = Pattern.compile("\\s");
Matcher matcher = whitespace.matcher(arg);
String result = "";
if (matcher.find()) {
result = matcher.replaceAll("");
}
return result;
}
用於去除空行在同一個文件中。
public static void RemoveEmptyLines(String FilePath) throws IOException
{
File inputFile = new File(FilePath);
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
String inputFileReader;
ArrayList <String> DataArray = new ArrayList<String>();
while((inputFileReader=reader.readLine())!=null)
{
if(inputFileReader.length()==0)
{
continue;
}
DataArray.add(inputFileReader);
}
reader.close();
BufferedWriter bw = new BufferedWriter(new FileWriter(FilePath));
for(int i=0;i<DataArray.size();i++)
{
bw.write(DataArray.get(i));
bw.newLine();
bw.flush();
}
bw.close();
}
package com.home.interview;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class RemoveInReadFile {
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(new File("Readme.txt"));
while(scanner.hasNext())
{
String line = scanner.next();
String lineAfterTrim = line.trim();
System.out.print(lineAfterTrim);
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
在這個程序中,我刪除了所有的空格,並從單行讀取的文件中讀取所有內容。 –
- 1. 刪除所有的空白和所有換行符和字符
- 2. 刪除行尾的所有空格和製表符
- 3. 如何刪除Perl中的所有空格和換行符?
- 4. PyPDF2堅持刪除所有空格
- 5. 刪除所有尾部空格
- 6. MySQL查詢,刪除所有空格
- 7. 序言:刪除所有空格名單
- 8. 刪除所有空白?
- 9. Java replaceAll從空行中刪除空格
- 10. 從網格刪除空行
- 11. 刪除表格的空行
- 12. 刪除空格
- 13. 刪除空格
- 14. 刪除空格
- 15. 刪除空格
- 16. 刪除換行符和空格
- 17. 刪除所有空白行,但留下兩個空行
- 18. 從特定字詞開始的行中刪除所有空格
- 19. 如何刪除字符串中的所有空格和\ n \ r?
- 20. 刪除xml文檔中的所有前導和尾隨空格
- 21. MySQL的刪除所有的連字符和空格
- 22. 如何從字符串中刪除所有符號和空格?
- 23. Windows批處理:如何刪除所有空白(或空)行
- 24. 刪除空行
- 25. 刪除空行
- 26. 空行/刪除
- 27. 刪除空行
- 28. 刪除空格組
- 29. C - 刪除空格
- 30. DOS:刪除空格
向我們展示你的代碼到目前爲止 – JoseK
你有什麼代碼,使遠嗎? –
正在做作業嗎? – Bozho