import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Test7 {
public static String read(String filename) throws IOException{
BufferedReader in = new BufferedReader(new FileReader(filename));
String s;
int n=1;
StringBuilder sb= new StringBuilder();
//利用循环在读取的每行前面都加上有行号和冒号
while((s = in.readLine()) != null){
sb.append(n+":"+s+"\n");
n++;
}
in.close();
return sb.toString();
}
public static void main(String[] args) throws IOException{
System.out.print(read("E://项目/exam/src/com/itheima/Test7.java"));
}
}
|
|