【发布时间】:2017-05-14 15:31:12
【问题描述】:
所以我有一个很长的文本文件,使用 Scanner 加载它大约需要半小时,所以我试图切换到 BufferedReader,这是我现在拥有的代码
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader Br1 = null;
BigInteger num = new BigInteger ("0");
String Line = "";
try {
Br1 = new BufferedReader (new FileReader("text.txt"));
System.out.println("Read line method");
Line = Br1.readLine();
while(Line != null) {
num = num.add(new BigInteger(Line));
System.out.println(Line);
Line = Br1.readLine();
}
System.out.println("number " + num);
} catch (IOException ioe) {System.out.println("error");}
}
为了测试它,我制作了文本文件 2 1 0 所有数字都在不同的行中
我希望它给我一个 BigInteger 210,但它给了我 3,我尝试用不同的方式添加到 BigInteger,但我无法让它正常工作。我该怎么做?
【问题讨论】:
-
add将添加数字,因此您基本上是在计算 2+1+0=3。你为什么想要 210? -
你还能期待什么,num.add(new BigInteger(Line)); ?
-
嗯,我试过了,shiftLeft 和其他一些东西,但没有任何效果
-
你为什么要首先创建一个这样的巨型 BiInteger。我的猜测是,有一种更好的方法可以实现您想要实现的目标。你想达到什么目标?
标签: java bufferedreader