开发者

Java Scanning of Text Copied From .doc file to .txt or .rtf file Throws "java.util.NoSuchElementException: No line found" Exception

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(Exc开发者_Go百科eption 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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜