gpt4 book ai didi

java - 如何从 .t​​xt 文件获取多个字符串

转载 作者:行者123 更新时间:2023-12-02 06:41:21 26 4
gpt4 key购买 nike

好的,所以我正在尝试创建一个 java 程序,它将读取 txt 文件并将每一行转换为字符串。例如,txt 文件可能类似于

 This should be a string
This should be another string
And another string

目前我的代码只允许我将 txt 文件转换为一个字符串。

 String Content = new Scanner(new File ("CONTENTS.txt")).useDelimeter(",").next();
System.out.println(Content);

最佳答案

请阅读javadoc for Scanner :

Scanner scanner = new Scanner(new FileInputStream(fileName));
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
// process line here..
}

关于java - 如何从 .t​​xt 文件获取多个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19105878/

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