一篇文档txt里,有很多行全是空格,试了半天实在不知道怎么去掉用replaceAll("^\\s+", "")为什么也不行呢?空白字符开头的行全替换成空?
No.--Ref.:29
1379/10
Notifying country:UNITED KINGDOMProduct(Click on the photo to enlarge):Category: Hobby/sports equipment Product: Tent - 2 Man Pop-up TentBrand: YellowstoneType/number of model: Item number: CA1072. Barcode: 5026396533176. No batch codeDescription: Green single skin pop up tent. 210 x 135 x 120 cm. Sold in circular flat storage bag.Country of origin: China
Danger:SuffocationThe product poses a risk of suffocation because there are no ventilation holes in the tent and the material from which it is made is non-breathable.The product does not comply with the relevant European standard EN 5912. Measures adopted by notifying country:Voluntary recall from consumers by the importer. Withdrawal from the et ordered by the authorities. Products were found and measures were taken also in: Ireland 就是中间那段空白,代码
public void indb(String mulu){
String s = "";
FileReader fr = null;
String tem = "";
try {
fr = new FileReader(mulu);
BufferedReader br = new BufferedReader(fr);; while((s = br.readLine()) != null){
tem = tem + s + "\n";
}
tem = tem.replaceAll("^\\n", "").replaceAll("\\t", "");
System.out.println(tem);
fr.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
No.--Ref.:29
1379/10
Notifying country:UNITED KINGDOMProduct(Click on the photo to enlarge):Category: Hobby/sports equipment Product: Tent - 2 Man Pop-up TentBrand: YellowstoneType/number of model: Item number: CA1072. Barcode: 5026396533176. No batch codeDescription: Green single skin pop up tent. 210 x 135 x 120 cm. Sold in circular flat storage bag.Country of origin: China
Danger:SuffocationThe product poses a risk of suffocation because there are no ventilation holes in the tent and the material from which it is made is non-breathable.The product does not comply with the relevant European standard EN 5912. Measures adopted by notifying country:Voluntary recall from consumers by the importer. Withdrawal from the et ordered by the authorities. Products were found and measures were taken also in: Ireland 就是中间那段空白,代码
public void indb(String mulu){
String s = "";
FileReader fr = null;
String tem = "";
try {
fr = new FileReader(mulu);
BufferedReader br = new BufferedReader(fr);; while((s = br.readLine()) != null){
tem = tem + s + "\n";
}
tem = tem.replaceAll("^\\n", "").replaceAll("\\t", "");
System.out.println(tem);
fr.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
加上条件过滤掉
import java.io.File;
import java.io.FileReader;
import java.io.IOException;public class TrimSpace {
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new FileReader(new File("./src/foundations/string/test.txt")));
StringBuilder sb = new StringBuilder();
String s = "";
while ((s = br.readLine()) != null) {
sb.append(s);
sb.append('\n');
}
String text = sb.toString();
System.out.println("text: " + text);
System.out.println("*****************************");
System.out.println(text.replaceAll("(?m)^\\s+$", ""));
}}
------------------------------------------------------------------------------------------------
也可以在读取每行时判断是否为空行, 下面会将所有空行都去掉
public static void trimSpace2() throws IOException
{
BufferedReader br = new BufferedReader(new FileReader(new File("./src/foundations/string/test.txt")));
StringBuilder sb = new StringBuilder();
String s = "";
while ((s = br.readLine()) != null) {
if (s.matches("^\\s*$")) {
continue;
}
sb.append(s);
sb.append('\n'); }
String text = sb.toString();
System.out.println("text: " + text);
}
String s = "";
FileReader fr = null;
String tem = "";
try {
fr = new FileReader(mulu);
BufferedReader br = new BufferedReader(fr);
while ((s = br.readLine()) != null) {
if (!"".equals(s.trim()))
tem = tem + s + "\n";
}
System.out.println(tem);
fr.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}不用replace 直接在读取的时候加个判断就好了
String s = "";
String mulu = "d:\\123.txt";
FileReader fr = null;
String tem = "";
try {
fr = new FileReader(mulu);
BufferedReader br = new BufferedReader(fr);
while ((s = br.readLine()) != null) {
if (!"".equals(s.trim()))
tem = tem + s + "\n";
}
System.out.println(tem);
fr.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
红色的字居然出不来?这样行不行
MULTILINE
public static final int MULTILINE
启用多行模式。
在多行模式中,表达式 ^ 和 $ 仅分别在行结束符前后匹配,或者在输入序列的结尾处匹配。默认情况下,这些表达式仅在整个输入序列的开头和结尾处匹配。
通过嵌入式标志表达式 (?m) 也可以启用多行模式。.replaceAll("(?m)^\\s+", "");