使用java在文件中查找没有行时遇到困难?
当查找文件中的行数时,它会显示更多行数。
例如,在我的文件中,word 文档中只有 26 行。但是当使用java程序计数时,它显示118。
File f=new File("C:\\Users\\os05\\Desktop\\Venkatesan(13-02-10).doc");
FileReader fr = new FileReader(f);
LineNumberReader ln = new LineNumberReader(fr);
int count = 0;
while (ln.readLine() != null)
{
count++;
}
System.out.println("No of lines:"+count);
上面的代码,如何计算数字。线....?
While find out no of line in a file, it displays more number of lines.
for example in my file having only 26 line in word document. But while count using java program it displays 118.
File f=new File("C:\\Users\\os05\\Desktop\\Venkatesan(13-02-10).doc");
FileReader fr = new FileReader(f);
LineNumberReader ln = new LineNumberReader(fr);
int count = 0;
while (ln.readLine() != null)
{
count++;
}
System.out.println("No of lines:"+count);
The above code, how is calculate the no. of line....?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您尝试将 Word 文档视为纯文本文件 (*)。
然而,Word 文档是具有专有格式的二进制文件,您需要正确解释该文件以提取其中包含的信息。
有一些库可以处理此类文件,例如 Apache POI。
如果您只是想进行实验和学习,那么坚持使用简单的文本文件(例如,由记事本生成的文件)可能会更容易。
(*) 即使不存在纯文本。
You're trying to treat a Word document as if it were a plain text file (*).
A Word document however is a binary file with a proprietary format that you need to interpret correctly to extract the information contained in it.
There are libraries out there that handle such files, for example Apache POI.
If you just want to do this for experimentation and learning, then it might be easier to just stick with simple text files (as produced by Notepad, for example).
(*) even if there is no such thing as plain text.
如果是Microsoft Word文档,它们是二进制文件,你不能这样做。
您需要为 Microsoft Word 文件找到合适的 api。
If it is Microsoft Word Document, they are binary files, you couldn't do that way.
You need to find appropriate api for microsoft word files.
您的问题是您正在查看一个文档文件 - 该文件不是以纯文本形式保存的。为了找到 Microsoft Word 文件中的行数,您将不得不使用专用的库...
该文件格式可在 www.wotsit.org 上找到,但我怀疑仅此一个会对您有所帮助。 。
Your problem is that you are looking at a doc file - which is not held in plain text. In order to find the number of lines in a microsoft word file, you are going to have to use a dedicated library...
The file format is available at www.wotsit.org, but I doubt that this alone will help you...
您还可以使用 open office api 访问 Office 文档的内容。
OpenOffice.org API 常见问题解答
You may also use the open office api to access to contents of Office documents.
FAQ on OpenOffice.org API