gpt4 book ai didi

java - 分割后读取内容

转载 作者:行者123 更新时间:2023-12-02 04:34:56 25 4
gpt4 key购买 nike

我的问题是我有一个代码,但我在修复“找不到符号”错误时遇到问题。这是代码。

public static void writer() throws IOException {
FileReader in = null;
FileWriter out = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
in = new FileReader("Student.txt");
out = new FileWriter("StudentAvg.txt");
br = new BufferedReader(in);
bw = new BufferedWriter(out);
String[] line = new String[28];
line = in.split("\t");

错误指向拆分过程。我的变量有问题吗?

最佳答案

问题是,您还没有读过任何行。

br = new BufferedReader(in);
bw = new BufferedWriter(out);
String[] line = new String[28];
line = in.split("\t"); // <-- this is your input file reader.

我想你想要

br = new BufferedReader(in);
bw = new BufferedWriter(out);
String fromFile;
while ((fromFile = br.readLine()) != null) {
String[] line = fromFile.split("\t");

您也可以使用try-with-resources和类似的东西

try (BufferedReader br = new BufferedReader(//
new FileReader("Student.txt"));
BufferedWriter bw = new BufferedWriter(//
new FileWriter("StudentAvg.txt"))) {
String fromFile;
while ((fromFile = br.readLine()) != null) {
String[] line = fromFile.split("\t");
}
}

关于java - 分割后读取内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30950112/

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