gpt4 book ai didi

java - 公共(public)字符串 - 无法解析

转载 作者:行者123 更新时间:2023-12-01 13:42:33 25 4
gpt4 key购买 nike

错误

bw.write(dataString);

我该如何解决这个问题?
dataString 无法解析为变量。

public class test {
public static void main(String[] args){
ArrayList<String> data = new ArrayList<String>();

try (BufferedReader br = new BufferedReader(new FileReader("src/test.txt"))) {
String CurrLine;

while((CurrLine = br.readLine()) != null) {
data.add(CurrLine);
}
String[] dataArray = new String[data.size()];
String dataString = Arrays.toString(dataArray);

String[] client = dataString.split("<::>");
Integer nameId = Arrays.binarySearch(client, "Test");
Integer versId = nameId + 1;
System.out.println(client[nameId] + "\n" + client[versId]);


} catch(FileNotFoundException ex) {
System.out.println("FNFE");
} catch(IOException ex) {
System.out.println("IOE");
}
try{
File file = new File("src/test.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(dataString);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}

最佳答案

在 try 和 catch block 之外声明dataString...仅此而已。 ;) 如果您在循环内声明它,或者在本例中是您的 try catch block ,则其生命周期仅限于它。

像这样:

String dataString = null;

在 try-catch block 内:

dataString = Arrays.toString(dataArray);

关于java - 公共(public)字符串 - 无法解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20597459/

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