Java解析文本文件并保留换行符? [英] Java parsing text file and preserving line breaks?

查看:1415
本文介绍了Java解析文本文件并保留换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究如何做到这一点,并变得有点困惑,我已经尝试过与扫描仪,但似乎并没有保留换行符,我不知道如何使它确定如果行是一个越线。如果有人有任何建议,我将不胜感激。我一直在使用扫描仪类如下,但不知道如何甚至检查线是否是一个新的行。谢谢

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆