编写一程序,给java源程序加上行号保存;然后读取有行号的java源程序,去掉行号,还原愿程序
1个回答
展开全部
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();
}
}
}
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();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询