java 为何无法去除自文件读入的字符串中的空格?显示的结果还是有空格的
读入文件的格式9999000099900000099077000000770000007777000077770000777700007777程序importjava.i...
读入文件的格式
9 9 9 9 0 0 0 0
9 9 9 0 0 0 0 0
0 9 9 0 7 7 0 0
0 0 0 0 7 7 0 0
0 0 0 0 7 7 7 7
0 0 0 0 7 7 7 7
0 0 0 0 7 7 7 7
0 0 0 0 7 7 7 7
程序
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class cetu2{
public static void main(String arg[]) throws IOException{
int b,n,k,count,p;
n=0;
k=0;
double m;
m=0;
count = 0;
byte g[] = new byte [60];
int a[] = new int[20];
int shou[] = new int[60];
int jie[] = new int[60];
String str = null;
String st[] = new String[20];
String tr = null;
FileReader f = null;
FileWriter file = null;
try {
f = new FileReader("111OK.txt");
file = new FileWriter("000.txt");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
BufferedReader in = null;
BufferedWriter out = null;
out = new BufferedWriter(file);
in = new BufferedReader(f);
tr = in.readLine();
str = tr.replaceAll(" ","");
System.out.println(str);
}
} 展开
9 9 9 9 0 0 0 0
9 9 9 0 0 0 0 0
0 9 9 0 7 7 0 0
0 0 0 0 7 7 0 0
0 0 0 0 7 7 7 7
0 0 0 0 7 7 7 7
0 0 0 0 7 7 7 7
0 0 0 0 7 7 7 7
程序
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class cetu2{
public static void main(String arg[]) throws IOException{
int b,n,k,count,p;
n=0;
k=0;
double m;
m=0;
count = 0;
byte g[] = new byte [60];
int a[] = new int[20];
int shou[] = new int[60];
int jie[] = new int[60];
String str = null;
String st[] = new String[20];
String tr = null;
FileReader f = null;
FileWriter file = null;
try {
f = new FileReader("111OK.txt");
file = new FileWriter("000.txt");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
BufferedReader in = null;
BufferedWriter out = null;
out = new BufferedWriter(file);
in = new BufferedReader(f);
tr = in.readLine();
str = tr.replaceAll(" ","");
System.out.println(str);
}
} 展开
3个回答
展开全部
因为你的正则表达式写错了,分析文本可以知道,要匹配的是制表符\t 而不是空格
str = tr.replaceAll(" ","");
修改成
str = tr.replaceAll("\t","");
完整的代码修改
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class cetu2 {
public static void main(String arg[]) throws IOException {
int b, n, k, count, p;
n = 0;
k = 0;
double m;
m = 0;
count = 0;
byte g[] = new byte[60];
int a[] = new int[20];
int shou[] = new int[60];
int jie[] = new int[60];
String str = null;
String st[] = new String[20];
String tr = null;
FileReader f = null;
FileWriter file = null;
try {
f = new FileReader("111OK.txt");
file = new FileWriter("000.txt");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
BufferedReader in = null;
BufferedWriter out = null;
out = new BufferedWriter(file);
in = new BufferedReader(f);
String hasRead = null;
while((hasRead = in.readLine())!=null){
tr = hasRead;
str = tr.replaceAll("\t", "");
System.out.println(str);
out.write(str+"\r\n");
}
in.close();
out.close();
}
}
展开全部
首先你要确定那是空格还是制表符tab,如果是空格的话肯定是能去除的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的这个地方中间并不是空格,而是tab键
你可以将str = tr.replaceAll(" ","");改为str = tr.replaceAll("\t","");
你可以将str = tr.replaceAll(" ","");改为str = tr.replaceAll("\t","");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询