【发布时间】:2014-04-11 22:15:17
【问题描述】:
所以我需要编写一个程序来读取包含文本的文件,然后将行号添加到每一行。到目前为止,我打印出了行号,而不是只打印出每一行,而是打印出每一行中的所有文本。我怎样才能让它打印出来呢?
这是我目前的代码:
public static void main(String[] args) throws FileNotFoundException {
try {
ArrayList<String> poem = readPoem("src\\P7_2\\input.txt");
number("output.txt",poem);
}catch(Exception e){
System.out.println("File not found.");
}
}
public static ArrayList<String> readPoem(String filename) throws FileNotFoundException {
ArrayList<String> lineList = new ArrayList<String>();
Scanner in = new Scanner(new File(filename));
while (in.hasNextLine()) {
lineList.add(in.nextLine());
}
in.close();
return lineList;
}
public static void number (String filename,ArrayList<String> lines) throws FileNotFoundException{
PrintWriter out = new PrintWriter(filename);
for(int i = 0; i<lines.size(); i++){
out.println("/* " + i + "*/" + lines);
}
out.close();
}
【问题讨论】:
-
首先,你使用Java 7吗?它会让你的工作更轻松