如何在JAVA程序显示时每行添加行号
使用RandomAccessFile类及方法,把程序本身显示两次,一次直接显示,一次给每一行添加行号会显示....
使用RandomAccessFile类及方法,把程序本身显示两次,一次直接显示,一次给每一行添加行号会显示.
展开
1个回答
展开全部
大概就是这样啦,换成你自己的文件名就行了
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
public class ShowSourceCode {
public static void main(String[] args) throws FileNotFoundException, IOException{
RandomAccessFile file = new RandomAccessFile("Add.java", "r");
String tmp;
while((tmp = file.readLine()) != null){
System.out.println(tmp);
}
file.seek(0);
int lineNum = 1;
while((tmp = file.readLine()) != null){
System.out.println(lineNum + ": " + tmp);
lineNum++;
}
}
}
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
public class ShowSourceCode {
public static void main(String[] args) throws FileNotFoundException, IOException{
RandomAccessFile file = new RandomAccessFile("Add.java", "r");
String tmp;
while((tmp = file.readLine()) != null){
System.out.println(tmp);
}
file.seek(0);
int lineNum = 1;
while((tmp = file.readLine()) != null){
System.out.println(lineNum + ": " + tmp);
lineNum++;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询