gpt4 book ai didi

java - 如何将文件的每一行保存到数组中?

转载 作者:行者123 更新时间:2023-12-01 17:46:18 25 4
gpt4 key购买 nike

我尝试了下面发布的方法,但仍然没有成功。我收到找不到符号的错误。我还能如何修改下面的代码。我必须将线放在底部吗?

import java.util.Scanner;
import java.io.*;
import java.util.Arrays;
/**
* Write a description of class lab8 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class lab8
{
public static void main(String[] args) throws IOException
{
FileInputStream fileIn = new FileInputStream("haikuFun.txt");
Scanner scrn = new Scanner(fileIn);
String[] lines = fileIn.readAllLines(new File("haikuFun.txt").toPath());
while(scrn.hasNext()) {
String s = scrn.nextLine();
System.out.println(s);
}

}
}

最佳答案

我会使用Files.readAllLines(Path)喜欢

String[] lines = Files.readAllLines(new File("haikuFun.txt").toPath());

关于java - 如何将文件的每一行保存到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54878053/

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