Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:定义为全局变量时处理IOException_Java_Global Variables - Fatal编程技术网

Java:定义为全局变量时处理IOException

Java:定义为全局变量时处理IOException,java,global-variables,Java,Global Variables,当变量是全局定义的时,如何处理IOException 我得到的错误是错误:(8,43)java:unreportedexception java.io.FileNotFoundException;必须捕获或声明要抛出 这是我的代码: import java.io.*; public class generator { private static char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k

当变量是全局定义的时,如何处理
IOException
我得到的错误是
错误:(8,43)java:unreportedexception java.io.FileNotFoundException;必须捕获或声明要抛出

这是我的代码:

import java.io.*;
public class generator {
    private static char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    private static StringBuilder partialSolution = new StringBuilder();
    private static File fout = new File("out.txt");
    private static FileOutputStream fos = new FileOutputStream(fout); //the syntax error is here
    private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

    private static void bt(int maxIndex, int index) throws IOException {

        if (index == maxIndex) {
            String solution = partialSolution.toString();
            System.out.println(solution);
            bw.write(solution);
            bw.newLine();
        } else {
            for (char c : alphabet) {
                partialSolution.append(c);
                bt(maxIndex, index + 1);
                final int lastCharIndex = partialSolution.length() - 1;
               partialSolution.deleteCharAt(lastCharIndex);
            }
        }
    }

    private static void btCaller(int maxIndex) throws IOException {
        bt(maxIndex, 0);
        bw.close();

    }

    public static void main(String[] args) throws IOException {
        btCaller(3);

    }
}
我知道我可以执行
抛出IOException
并尝试/捕获:

但是,当我使用这些方法时,我得到了一个语法错误(
错误:(8,5)java:invally start of type
)。我把代码放错地方了吗?
如何解决此错误?

使用静态块。(但是,将流声明为静态变量不是一个好主意。请彻底了解资源管理。)

private static char[]字母表={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
私有静态StringBuilder partialSolution=新StringBuilder();
私有静态文件fout=新文件(“out.txt”);
私有静态文件输出流;
专用静态缓冲写入程序bw;
静止的{
试一试{
fos=新文件输出流(fout);
bw=新的缓冲写入程序(新的输出流写入程序(fos));
}捕获(IOE异常){
e、 printStackTrace();
}
}
使用静态块。(但是,将流声明为静态变量不是一个好主意。请彻底了解资源管理。)

private static char[]字母表={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
私有静态StringBuilder partialSolution=新StringBuilder();
私有静态文件fout=新文件(“out.txt”);
私有静态文件输出流;
专用静态缓冲写入程序bw;
静止的{
试一试{
fos=新文件输出流(fout);
bw=新的缓冲写入程序(新的输出流写入程序(fos));
}捕获(IOE异常){
e、 printStackTrace();
}
}

fos=newfileoutputstream(fout)是一个显而易见的想法在main中,这会导致下一行出现任何错误,我需要这些变量是全局变量,因为
bt
方法是递归的,因此如果我在方法中定义这些变量,文件将被覆盖,那么使用try catchYep在静态块中定义文件加载是否有意义!有人刚刚回答了这个问题,谢谢。显而易见的想法是把
fos=newfileoutputstream(fout)在main中,这会导致下一行出现任何错误,我需要这些变量是全局变量,因为
bt
方法是递归的,因此如果我在方法中定义这些变量,文件将被覆盖,那么使用try catchYep在静态块中定义文件加载是否有意义!有人刚刚回答了这个问题,谢谢。
try {
    private static FileOutputStream fos = new FileOutputStream(fout);
} catch (IOException e) {
    e.printStackTrace();
}