我正在尝试写入文件,我需要在第一行写入记录总数,并在 while 循环中写入所有其他行,一切正常,但是,在第一行需要写入记录总数我该怎么做那个,请帮帮我!!谢谢!!
这是我的代码:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class headerline {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File folderall = new File("FilesIn1");
File[] BFFileall = folderall.listFiles();
for (final File file : BFFileall) {
String str = file.getName();
BufferedReader br = null;
BufferedWriter lbwp = null;
BufferedWriter lb = null;
try {
int lbwpcount = 1;
int lbcount = 1;
String reprintbwletterbwpca = (str);
lbwp = new BufferedWriter(new FileWriter(reprintbwletterbwpca));
lbwp.write("Total line number: " + lbwpcount);
String reprintbwletterbwpcalb = (str);
lb = new BufferedWriter(new FileWriter(reprintbwletterbwpcalb));
lb.write("Total line number: " + lbwpcount);
br = new BufferedReader(new FileReader(file));
String line;
line = br.readLine();
while ((line = br.readLine()) != null) {
String[] actionID = line.split("|");
String actionid = actionID[2];
String custnumber = actionID[3];
lbwp.write("ActionID: " + actionid + ",CustomerNumber: " + custnumber + "\r\n");
lbwpcount++;
lb.write("ActionID: " + actionid + ",CustomerNumber: " + custnumber + "\r\n");
lbcount++;
}
lbwp.close();
lb.close();
} catch(Exception e1) {
e1.printStackTrace();
}
}
}
}
假设文件有 1201 行,它应该在第一行打印 Total line number: 1200。比“ActionID:” + actionid +“,CustomerNumber:” + custnumber ....
假设其他文件有 1451 行,它应该在第一行打印总行号:1450。比“ActionID:” + actionid +“,CustomerNumber:” + custnumber ....
我不知道我该怎么做,请帮助我!我可以在完成while循环后将第一行写为最后一行吗?先谢谢了!!