gpt4 book ai didi

java - 使用java中的扫描仪一次读取完整文件

转载 作者:行者123 更新时间:2023-11-30 03:21:40 25 4
gpt4 key购买 nike

我必须用 Java 读取一个文本文件,为此我使用下面的代码:

Scanner scanner = new Scanner(new InputStreamReader(
ClassLoader.getSystemResourceAsStream("mock_test_data/MyFile.txt")));

scanner.useDelimiter("\\Z");
String content = scanner.next();
scanner.close();

据我所知StringMAX_LENGTH 2^31-1

But this code is reading only first 1024 characters from input file(MyFile.txt).

我找不到原因。

最佳答案

我已经阅读了一些评论,因此我认为有必要指出这个答案并不关心好的或坏的做法。对于需要快速解决方案的懒人来说,这是一个愚蠢的、但又好学的扫描仪技巧。

final String res = "mock_test_data/MyFile.txt"; 

String content = new Scanner(ClassLoader.getSystemResourceAsStream(res))
.useDelimiter("\\A").next();

here... 窃取

关于java - 使用java中的扫描仪一次读取完整文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31183564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com