编写一程序,给java源程序加上行号保存;然后读取有行号的java源程序,去掉行号,还原愿程序

如题所述

importjava.io.BufferedReader;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.InputStreamReader;
importjava.io.PrintWriter;

publicclassTest {

/**
*@paramargs
*/
publicstaticvoidmain(String[] args) {
//TODOAuto-generated method stub
String srcPath="";//源代码文件路径
String addRowNumPath="";//加行号后的文件保存路径
String removeRowNumPath="";//去行号还原后的文件保存路径
try{
BufferedReader br=newBufferedReader(newInputStreamReader(newFileInputStream(srcPath)));
PrintWriter pw=newPrintWriter(addRowNumPath);
/**
* 加行号保存
* */
String line=null;
introw=1;//行号
while((line=br.readLine())!=null){
String newline=row+" "+line;
pw.println(newline);
row++;//行号递增
pw.flush();
}
pw.close();
br.close();
/**
* 去行号还原
* */
br=newBufferedReader(newInputStreamReader(newFileInputStream(addRowNumPath)));
pw=newPrintWriter(removeRowNumPath);
while((line=br.readLine())!=null){
String newline=line.substring(line.indexOf(" "));
pw.println(newline);
pw.flush();
}
pw.close();
br.close();

}catch(Exception e) {
//TODOAuto-generated catch block
e.printStackTrace();
}
}

}
温馨提示:答案为网友推荐,仅供参考