Java 扫描从 .doc 文件复制到 .txt 或 .rtf 文件的文本抛出“java.util.NoSuchElementException:找不到行”例外

发布于 2024-12-05 05:06:27 字数 1267 浏览 0 评论 0原文

基本上,我所做的是将文本从 Word 文档(97-2003 Word Doc)复制到文本文件或富文本文件,而 Java 扫描实用程序由于某种原因不喜欢它。

这是我设置的用于处理文件读取操作的类:

import java.io.*;
import java.lang.*;
import java.util.*;

public class FileReader 
{
    private Scanner read;

    public void openFile(String name, String path)
    {
        try
        {
            read = new Scanner(new File(path + "/" + name));
            System.out.println("Succesfully opened " + name + " in " + path + "!");
        }
        catch(Exception e)
        {
            System.out.println("Could not open file.");
        }
    }

    public boolean hasNextEntry()
    {
        boolean result = false;

        if(read.hasNext())
        {
            result = true;
        }
        return result;
    }

    public String getNextLine()
    {
        String result = "";
        try
        {
            result = read.nextLine();
        }
        catch(Exception e)
        {
            System.out.println("Error getting next line --> " + e);
        }
        return result;
    }
}

我在 Main 函数中所做的事情是:

FileReader fr = new FileReader();
String dir = System.getProperty("user.dir");
fr.openFile("Text.txt", dir);
String line = fr.getNextLine();

有什么想法吗?

Basically, what I do is copy text from a Word document (97-2003 Word Doc) to a text file or rich text file, and the Java scanning utility doesn't like it for some reason.

Here is the class i have set up to deal with my file reading operations:

import java.io.*;
import java.lang.*;
import java.util.*;

public class FileReader 
{
    private Scanner read;

    public void openFile(String name, String path)
    {
        try
        {
            read = new Scanner(new File(path + "/" + name));
            System.out.println("Succesfully opened " + name + " in " + path + "!");
        }
        catch(Exception e)
        {
            System.out.println("Could not open file.");
        }
    }

    public boolean hasNextEntry()
    {
        boolean result = false;

        if(read.hasNext())
        {
            result = true;
        }
        return result;
    }

    public String getNextLine()
    {
        String result = "";
        try
        {
            result = read.nextLine();
        }
        catch(Exception e)
        {
            System.out.println("Error getting next line --> " + e);
        }
        return result;
    }
}

What i do in my Main function is:

FileReader fr = new FileReader();
String dir = System.getProperty("user.dir");
fr.openFile("Text.txt", dir);
String line = fr.getNextLine();

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两仪 2024-12-12 05:06:27

Word .doc 文件是(专有格式)二进制文件 - 没有“行”可言。您无法像尝试那样阅读它们(作为文本)。

您正在调用 Scanner.nextLine() ,它尝试查找下一行分隔符并返回其之前的文本。我怀疑它无法找到行分隔符(或者它只是在尝试处理二进制文件时感到恶心)。

如果您的下一个问题是“那么我该如何阅读它们?” ...答案是 Apache POI 项目

Word .doc files are (proprietary format) binary files - there's no "lines" to speak of. You can't read them like you're trying (as text).

You're calling Scanner.nextLine() which attempts to find the next line separator and return the text prior to it. I suspect it's unable to find a line separator (or it just pukes trying to deal with a binary file).

If your next question is, "How do I read them then?" ... the answer is the Apache POI project

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文