Java 扫描从 .doc 文件复制到 .txt 或 .rtf 文件的文本抛出“java.util.NoSuchElementException:找不到行”例外
基本上,我所做的是将文本从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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