0

我开始编写一个可以解析旧式 .vmg 文件*的脚本。我想从扫描仪实用程序开始,这样我就可以一开始将消息一一剔除。这就是我得到的。

import java.io.File;
import java.io.IOException;
import java.util.Scanner;


public class Strip {
    public static void main(String[] args)
    {
        try
        {
            File file = new File("all.vmg");
            Scanner sc = new Scanner(file);
//          sc.useDelimiter("BEGIN:VMSG");
            while (sc.hasNext())
            {
            String string = sc.next();
            System.out.println(string);
            }

        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

    }


}

它编译并运行良好。虽然它不会打印出任何东西。首先,我使用了现在已注释的分隔符。然后觉得可能有问题。所以现在它只使用它的默认空白分隔符。但是无论如何都没有要打印的行。我不得不猜测的是,由于某种原因,hasNext 的评估结果不正确?* .vmg 文件是一般格式的文本文件

BEGIN:VMSG
VERSION:1.1
X-IRMC-STATUS:
X-IRMC-BOX:INBOX
X-NOK-DT:20110224T215100Z
X-MESSAGE-TYPE:DELIVER
BEGIN:VCARD
VERSION:3.0
N:
TEL:+37999999999
END:VCARD
BEGIN:VENV
BEGIN:VBODY
Date:24.02.2008 21:51:00
Sample mobile text message 
END:VBODY
END:VENV
END:VMSG

我还尝试为脚本提供其他几个没有打印出来的简单文本文件。

4

3 回答 3

0

hasNext()匹配任何东西(?s).*它在尝试查找匹配项时使用模式。

尝试使用hasNextLinewhich 使用换行符作为分隔符。

于 2013-07-31T16:25:43.770 回答
0

First of all, based on your comment in another answer, the program is throwing something that you aren't catching. Change your exception type in your catch to the plain Exception class, that'll ensure you can catch any Throwable.

Once that's fixed, then you can tackle the actual problem.

EDIT: So I went and pasted the code you supplied into Eclipse, and the contents of the file as well, and ran it without a single problem. At this point, I have no idea what the issue would be, other than for some reason it can't find the file. If that were the case, however, it would've thrown a very visible error.

In any case, it appears you've solved it on your own, though again I'm at a loss as to why that works and your original doesn't.

于 2013-07-31T17:08:52.947 回答
0

好的,这使脚本正常工作。但我不知道怎么做。所以也许有人可以告诉我幕后发生的事情。我将其发布为答案,因为它在某种程度上是一种解决方案,而且还因为编辑原始问题会使它变得非常长。

不同之处在于我在 while 循环之前启动了扫描仪对象。而且我使用 filereader 和 bufferedreader 进行扫描仪输入(尽管它的手册说按照我的第一个脚本它应该可以使用文件对象)

import java.io.*;
import java.util.Scanner;


public class Strip {
    public static void main(String[] args)
    {
Scanner sc = null;
        try
        {
            sc = new Scanner(new BufferedReader(new FileReader("all.vmg")));
            sc.useDelimiter("BEGIN:VMSG");
            while (sc.hasNextLine())
            {
            System.out.println(sc.nextLine());
            }
            sc.close(); 
        }
        catch (Exception e)
        {
           System.out.println("Caught Something whatever?");
        }

    }


}
于 2013-08-01T08:37:49.537 回答