使用扫描仪读取行 [英] Read line with Scanner

查看:131
本文介绍了使用扫描仪读取行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑对于更多读者:问题是我的输入文件已损坏。

EDIT for further readers: the problem was that my input file was corrupted.

我不明白我在做什么错误:

I don't understand what I'm doing wrong :

我使用的是这段代码:

    File f = new File("C:\\Temp\\dico.txt");
    BufferedReader r = null;
    try {
        r = new BufferedReader(new FileReader(f));
        String scan;
        while((scan=r.readLine())!=null) {
            if(scan.length()==0) {continue;}
            //treatment
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Lexique.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Lexique.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if(r!=null) try {
            r.close();
        } catch (IOException ex) {
            Logger.getLogger(Lexique.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

哪种工作正常。现在,出于某种原因,我想换一台扫描仪。我的代码成了:

Which is working fine. Now, for some reason, I wanted to change for a Scanner. My code became :

    File f = new File("C:\\Temp\\dico.txt");
    Scanner r = null;
    try {
        r = new Scanner(f);
        String scan;
        while(r.hasNextLine()) {
            scan = r.nextLine();
            if(scan.length()==0) {continue;}
            //treatment
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Lexique.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Lexique.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if(r!=null) r.close();
    }

这一次,我们永远不会输入,因为r.hasNextLine()总是返回false。关于我做错了什么的任何想法?

This time, we never enter the while, because r.hasNextLine() always returns "false". Any idea about what I'm doing wrong ?

我确切地说没有其他改变,文件仍然是相同的。

I precise that nothing else changed, the file is still the same.

编辑:我也准确地说我尝试了另一个文件并得到了相同的结果,这意味着它显然不是来自文件。

EDIT : I also precise that I tried with another file and got the same result, meaning it doesn't comes from the file apparently.

该文件如下所示:

a
à
abaissa
abaissable
abaissables
abaissai
abaissaient
abaissais
abaissait
...

编辑2:
文件的内容似乎有问题,因为只有当我将文件中的内容复制/粘贴到问题时问题仍然存在另一个。很明显,如果我自己编写内容,它可以工作,如果我使用我的dico.txt文件的一部分内容,它就不起作用。有什么解释吗?

Edit 2 : The content of the file seems to be problematic since the problem persists only if I copy/paste the content from my file to another. In clear, if I write the content myself, it works, if I use a part of the content of my dico.txt file, it doesn't work. Any explanation ?

编辑3:
这些是我文件的链接。我建议你避免使用非常庞大的dico.txt。

Edit 3 : These are the links to my files. I advice you to avoid the dico.txt which is very huge.

dico.txt: https://drive.google.com/file/d/0B0sroFy9HZlBNDl3MUwzVnh6VU0/edit?usp=sharing

dico.txt : https://drive.google.com/file/d/0B0sroFy9HZlBNDl3MUwzVnh6VU0/edit?usp=sharing

test.txt: https://drive.google .com / file / d / 0B0sroFy9HZlBemZjbXU1RmlmdjQ / edit?usp = sharing

test.txt : https://drive.google.com/file/d/0B0sroFy9HZlBemZjbXU1RmlmdjQ/edit?usp=sharing

推荐答案

此代码读取文件行line。

This code reads the file line by line.

public static void readFileByLine(String fileName) {
  try {
   File file = new File(fileName);
   Scanner scanner = new Scanner(file);
   while (scanner.hasNext()) {
    System.out.println(scanner.next());
   }
   scanner.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } 
 }

您还可以将分隔符设置为行分隔符,然后执行相同的操作。

You can also set a delimiter as a line separator and then perform the same.

 scanner.useDelimiter(System.getProperty("line.separator"));

您必须检查是否有可用的下一个令牌,然后读取下一个令牌。您还需要双重检查给予扫描仪的输入。即dico.txt。默认情况下,Scanner根据空格中断输入。请确保输入的分隔符在正确的位置

You have to check whether there is a next token available and then read the next token. You will also need to doublecheck the input given to the Scanner. i.e. dico.txt. By default, Scanner breaks its input based on whitespace. Please ensure that the input has the delimiters in right place

您的评论更新的答案:

我刚刚尝试使用以下内容创建输入文件

I just tried to create an input file with the content as below

a
à
abaissa
abaissable
abaissables
abaissai
abaissaient
abaissais
abaissait

尝试使用以下代码阅读它。它工作正常。

tried to read it with the below code.it just worked fine.

 File file = new File("/home/keerthivasan/Desktop/input.txt");
     Scanner scr = null;
         try {
            scr = new Scanner(file);
            while(scr.hasNext()){
                System.out.println("line : "+scr.next());
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ScannerTest.class.getName()).log(Level.SEVERE, null, ex);
        }

输出:

line : a
line : à
line : abaissa
line : abaissable
line : abaissables
line : abaissai
line : abaissaient
line : abaissais
line : abaissait

所以,我相信这应该有效。由于您在Windows环境中工作,行结束(EOL)序列(0x0D 0x0A,\\\\ n)实际上是两个ASCII字符,CR和LF字符的组合。如果您将Scanner实例设置为使用分隔符,如下所示,它可能会提取

so, I am sure that this should work. Since you work in Windows ennvironment, The End of Line (EOL) sequence (0x0D 0x0A, \r\n) is actually two ASCII characters, a combination of the CR and LF characters. if you set your Scanner instance to use delimiter as follows, it will pick up probably

 scr = new Scanner(file);
 scr.useDelimiter("\r\n");

然后循环读取行。希望这有帮助!

and then do your looping to read lines. Hope this helps!

这篇关于使用扫描仪读取行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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