使用BuferedReader读取一个文本文件,为每行加上行号

 使用BuferedReader读取一个文本文件,为每行加上行号_第1张图片

public class Homework02 {
    public static void main(String[] args) throws IOException {
        String filePath = "E:\\aa.txt";
        File file = new File(filePath);
        String s ="";
        int linecount=0;
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"gbk"));
        while((s= br.readLine())!=null){
            System.out.print((++linecount)+" "+s+"\n");
        }
        br.close();
    }
}

你可能感兴趣的:(韩顺平java习题,java,服务器,linux)