错误于
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 and catch块之外声明dataString
...仅此而已。 ;)如果在循环中声明它,或者在这种情况下,在try catch块中声明它,则其生命周期将受到限制。
像这样:
String dataString = null;
在try-catch块中:
dataString = Arrays.toString(dataArray);
关于java - 公共(public)字符串-无法解析,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20597459/