不断出现语法错误,插入“}”以完成阻止,即使我已经进行了请求的更改

问题描述 投票:0回答:1
private static int copyFile(String inFile, String outFile) {
    //input and output file read/write declarations
    FileReader in = null;
    FileWriter out = null;
    //Create reader and writer
    try {
        in = new FileReader(inFile);
    }catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        return -1;
    }finally {
          try {in.close();
    try {
        out = new FileWriter(outFile);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return -1;
    }finally {
    }
    
    //loop to read all characters from FileReader and write them to FileWriter
    int i;
    try {
        while((i = in.read()) != -1) {
            out.write(i);
        }
        return 0;
    }catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return -2;
    }finally {}
    
}

我最后添加到 try catch 块的末尾,但 eclipse 一直给我错误 此行有多个标记 - 语法错误,插入“}”来完成块 - 语法错误,插入“Finally”来完成 BlockStatements

java filereader filewriter
1个回答
0
投票

不确定您在这里要做什么,但是当您声明一个 try 块时,您需要 catch 错误或添加 finally 子句。

  private static int copyFile(String inFile, String outFile) throws IOException {
        //input and output file read/write declarations
        FileReader in = null;
        FileWriter out = null;
        //Create reader and writer
        try {
            in = new FileReader(inFile);
        }catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return -1;
        }finally {
            try {in.close();
                try {
                    out = new FileWriter(outFile);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return -1;
                }

                //loop to read all characters from FileReader and write them to FileWriter
                int i;
                try {
                    while((i = in.read()) != -1) {
                        out.write(i);
                    }
                    return 0;
                }catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return -2;
                }
            }
            finally {
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.