读取文件java长行

时间:2015-05-24 20:19:36

标签: java file-io non-ascii-characters nosuchelementexception

我试图从java文件中读取文本行,而我尝试使用的while循环从未运行过(例如,readText.hasNextLine()被评估为false)。我尝试用不同的文件做同样的事情并且它在那段时间工作,但是当我在文件中添加更长的文本行时它再也没有用了。扫描仪可以读取的行长度是否有限制?为什么我的文件似乎不再包含文本? 这是我的代码:

print("Welcome...")

l = int(input("Specify length [m]: "))

if l < 0:
    print("You've entered a wrong index.Please specify.")
    l = int(input("Specify length [m]: "))

if l == 0:
    print("You've entered a wrong index.Please specify.")
    l = int(input("Specify length [m]: "))

print("Calculation...") 

P = float(input("Force P [kN]: "))
A = P
B = P

with open('output.txt', 'w') as outfile:

    if P > 0:
        print("Result A je ", P, "kN", file=outfile)
        print("Result B je ", P, "kN", file=outfile)
        print("Calculation...")

        maxM = (P*l)/3
        print("Maximum value", maxM, "kNm", file=outfile)
    if P < 0:
        print("You've entered an invalid argument.Please specify.")
        P = float(input("Force P [kN]: "))
        print("Result A je ", P, "kN", file=outfile)
        print("Result B je ", P, "kN", file=outfile)

        print("Calculation...")

        maxM = (P*l)/3
        print("Maximum result : ", maxM, "kNm", file=outfile)

在我添加while循环之前,我收到了这个错误:

import java.util.*;
import java.io.*;
public class errorSolve{
   public static void main(String[] args) throws FileNotFoundException{
         Scanner readText = new Scanner (new File("try.txt"));
         while(readText.hasNextLine()){
         System.out.println(readText.nextLine());
         }
   }
}

2 个答案:

答案 0 :(得分:0)

I tested here and it worked, man. And an object of type Scanner has no row limit. I tested with a 100Mb text file.

A similar doubts: here

答案 1 :(得分:0)

好吧所以我发现这个失败的原因并不是代码失败,因为我的文件有错误的引号。显然,如果您从Microsoft Word复制并粘贴文本,他们会使用不同类型的引号,这些引号会抛出扫描程序,因为它们不是ASCII代码字符。 (“vs&#34;)