我有这段代码:
public static void main(String[] args) {
try {
String filePath = (args[0]);
BufferedReader br = new BufferedReader(new FileReader(filePath));
String strLine = br.readLine();
//Read File Line By Line and Print the content on the console
PrintStream out = new PrintStream(new FileOutputStream( //printing output to user specified text file (command line argument: outputfile)
args[1]+".txt"));
while ((strLine = br.readLine()) != null) {
char [] words = strLine.toCharArray();
System.out.println (strLine);
Arrays.sort(words);
out.println(words);
}
//close the streams
br.close();
}
catch(IOException e){
System.err.println("An IOException was caught, Re-open program and enter correct number of arguments :"+e.getMessage());
}
}
}
它完成了我想要的操作,但它正在跳过输入文件的第一行。
比如说输入文件包含这个单词列表: 再见 红色 蓝色
它会输出: EDR 贝鲁
并跳过打印: 贝伊
答案 0 :(得分:2)
更改
String strLine = br.readLine();
到
String strLine = null;
当while ((strLine = br.readLine()) != null)
执行时,第一行在当前行中被消耗(后来被丢弃)。