JAVAutil。扫描仪使用java中的扫描仪一次性完成文件读取
我必须阅读Java中的文本文件,为此我使用以下代码:
Scanner scanner = new Scanner(new InputStreamReader(
ClassLoader.getSystemResourceAsStream("mock_test_data/MyFile.txt")));
scanner.useDelimiter("\\Z");
String content = scanner.next();
scanner.close();
据我所知String
有MAX_LENGTH 2^31-1
But this code is reading only first 1024 characters from input file(MyFile.txt).
我找不到原因
# 1 楼答案
使用
BufferedReader
的示例非常适合大文件:FileInputStream
用于较小的文件使用
readAllBytes
并对它们进行编码也解决了这个问题你可以看看this问题。非常好
# 2 楼答案
我已经阅读了一些评论,因此我认为有必要指出,这个答案并不关心好的或坏的做法。对于需要快速解决方案的懒人来说,这是一个愚蠢的、很好理解的扫描技巧
从here...偷来的
# 3 楼答案
感谢您的回答:
我终于找到了解决办法
因为我必须一次读一个很长的文件