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

标签 java arrays string try-catch public

错误

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/

相关文章:

c - 为什么两个不同函数的数组和指针参数的地址相同?

c++ - 如何使用 std::ifstream 读取 UTF-8 编码的文本文件?

java - Spring MongoDB 和 Apache Shiro

java - 无限滚动问题,向下滚动且 api 调用函数有效且列表位置转到第一项

php - 查找数组值之间步数的有效方法

c - 在二阶指针上使用下标运算符

java - 在java中将数组列表添加到二维数组列表

javascript - 字符串操作,将 ""替换为(空格)

java - android gradle -- 编译两个纯java项目到dex

java - IntelliJ 插件开发 : PluginException cannot create class Action