Java解析文本文件并保留换行符? [英] Java parsing text file and preserving line breaks?
问题描述
for(String fileName:f.list()){
fileCount ++;
扫描仪sc =新扫描仪(新文件(f,fileName));
int count = 0;
String outputFileText =;
//System.out.println(fileCount);
String text =;
while(sc.hasNext()){
String line = sc.nextLine();
$ div $解析方案
如果你只是想读取文件,我会建议使用 $ b $ pre $ $ $ $ $ $ LineNumberReader lnr =新的LineNumberReader(新的FileReader(f));
String line =;
while(line!= null){
line = lnr.readLine();
if(line == null){break;}
/ * do stuff * /
}
I have been researching how to do this and becoming a bit confused, I have tried so far with Scanner but that does not seem to preserve line breaks and I can't figure out how to make it determine if a line is a line break. I would appreciate if anyone has any advice. I have been using the Scanner class as below but am not sure how to even check if the line is a new line. Thanks
for (String fileName : f.list()) {
fileCount++;
Scanner sc = new Scanner(new File(f, fileName));
int count = 0;
String outputFileText = "";
//System.out.println(fileCount);
String text="";
while (sc.hasNext()) {
String line = sc.nextLine();
}
}
If you're just trying to read the file, I would suggesting using LineNumberReader
instead.
LineNumberReader lnr = new LineNumberReader(new FileReader(f));
String line = "";
while(line != null){
line = lnr.readLine();
if(line==null){break;}
/* do stuff */
}
这篇关于Java解析文本文件并保留换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!