Java 读取一个文本文件并写入多个文本文件以进行筛选/提取
我正在寻找一种方法来过滤我的文本文件。我有许多文件夹名称,其中包含许多文本文件,文本文件有几个无工作人员,每个工作人员有10个集群/组(我在这里只显示了3个)。但是每个组/簇可能包含几个基元(我在这里显示了1和2) 这是我的adam文本文件,page1_d.txt:Java 读取一个文本文件并写入多个文本文件以进行筛选/提取,java,loops,text,io,count,Java,Loops,Text,Io,Count,我正在寻找一种方法来过滤我的文本文件。我有许多文件夹名称,其中包含许多文本文件,文本文件有几个无工作人员,每个工作人员有10个集群/组(我在这里只显示了3个)。但是每个组/簇可能包含几个基元(我在这里显示了1和2) 这是我的adam文本文件,page1_d.txt: # staff No. 0 //no of staff * 0 0 1 //no of cluster 1 1 1 1 1 1 //one primitive here actually p1 in my Array
# staff No. 0 //no of staff
* 0 0 1 //no of cluster
1 1 1 1 1 1 //one primitive here actually p1 in my Array
* 0 1 1
1 1 1 1 1 1
* 0 2 1
1 1 1 1 1 1
* 0 3 1
1 1 1 1 1 1
# staff No. 1
* 1 0 2
2 2 2 2 2 2 2 // two primitive here actually p2 and p3 in my Array
3 3 3 3 3
* 1 1 2
2 2 2 2 2 2 2
3 3 3 3 3
* 1 2 2
2 2 2 2 2 2 2
3 3 3 3 3
* 1 3 2
2 2 2 2 2 2 2
3 3 3 3 3
我的公共课:
public class NewClass {
String [][][][] list = { {{{"adam"}},{{"p1"},{"p2","p3"}},{{"p4","p5","p6"}}} };
// first is name for the folder, second is num of text file, 3rd num of staff, 4th num of primtive
final int namefolder = list.length;
final int page = list[0].length;
这是我读取文本文件并写入另一个文本文件的方法:
public void search (File folder, int name,int numPage){
BufferedReader in;
String line=null;
int staff = list[name][numPage].length; // the length vary for each txt file
for (int numStaff=0;numStaff<staff;numStaff++){
int primitive = list[name][numPage][numStaff].length; //the length vary for each staff
StringBuilder contents = new StringBuilder();
String separator = System.getProperty("line.separator");
try {
in = new BufferedReader(new FileReader(folder));
for (int numPrim=0;numPrim<primitive;numPrim++) {
while(( line = in.readLine()) != null) {
if (!(line.startsWith("#") ||line.startsWith("*") ||line.isEmpty() )) {
contents.append(line);
contents.append(separator);
try {
PrintWriter output = new PrintWriter(new FileOutputStream("C://"+list[name][numPage][numStaff][numPrim]+".txt", true));
try {
output.println(line);
}
finally {
output.close();
}
for (int i = numPrim+1; i < primitive; i++){ // this is for more than 2 primitive
if((line = in.readLine()) != "\n"){ // 2nd or more primitive has no newline
contents.append(line);
contents.append(separator);
try {
PrintWriter output2 = new PrintWriter(new FileOutputStream("C://"+list[name][numPage][numStaff][i]+".txt", true));
try {
output2.println(line);
}
finally {
output2.close();
}
} catch (IOException e) {
System.out.println("Error cannot save");
e.printStackTrace();
}
}
}
} catch (IOException e) {
System.out.println("Error cannot save");
e.printStackTrace();
}
}
}//end of while
}// end for loop for prim
in.close();
}//end of try
catch(IOException e) {
e.printStackTrace();
}
}// end for loop for staff
}
但它表明:
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
(empty)
2 2 2 2 2 2 2
3 3 3 3 3
2 2 2 2 2 2 2
3 3 3 3 3
2 2 2 2 2 2 2
3 3 3 3 3
2 2 2 2 2 2 2
3 3 3 3 3
这个(p2.txt):
这个(p3.txt):
您的计数器计算打印的行数,如果您想计算有多少行,请将其移到if块之外:
while (( line = input.readLine()) != null){
if (!(line.startsWith("#") || line.startsWith("*") ||line.isEmpty() )) {
contents.append(line);
contents.append(separator);
System.out.println(line);
}
count++;
}
一些建议:
void parse() {
while( hasMoreLines() ) {
if( lineMatches() ) {
processLine();
}
}
}
boolean lineMatches() {
if( line.startsWith( "#" ) ) return false;
if( line.startsWith( "*" ) ) return false;
return true;
}
现在有两个选项:可以在lineMatches()
中或processLine()中的某个位置检查模式1
如果采用后一种方法,您可以在写入任何输出之前简单地返回。“我想计算打印行数,但它计算循环数”-有什么区别?您发布的代码应该打印相同的计数!contents
是否打印出正确的行(即除了跳过的行以外的所有行)?@adarshr是的,您是对的,计数没有问题,是我把确切的问题弄糊涂了。虽然我以前已经计算过打印的行数,但在这个程序中,我不知道为什么计数的行数与输出的行数不同,所以我将其删除。
(empty)
(empty)
(empty)
(empty)
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3
while (( line = input.readLine()) != null){
if (!(line.startsWith("#") || line.startsWith("*") ||line.isEmpty() )) {
contents.append(line);
contents.append(separator);
System.out.println(line);
}
count++;
}
void parse() {
while( hasMoreLines() ) {
if( lineMatches() ) {
processLine();
}
}
}
boolean lineMatches() {
if( line.startsWith( "#" ) ) return false;
if( line.startsWith( "*" ) ) return false;
return true;
}