1

这是我的第一篇文章,所以对于我肯定要提交的任何格式/内容虚假,我提前道歉。

我正在尝试创建一个 SmbFileInputStream 以从中创建一个 POI-HSSF 工作簿。我正在从网络驱动器中提取文件以获取一些输入。SmbFileInputStream 在 try-catch 中实例化。

package com.tem;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import jcifs.smb.*;

    public static HSSFWorkbook loadWorkbook(String fileName){
    // create a new file input stream with the input file specified by fileName
    NtlmPasswordAuthentication auth = new      NtlmPasswordAuthentication("10.6.5.7","tyler","123");

    try{SmbFileInputStream fin = new SmbFileInputStream("smb:" + fileName);}

    catch(FileNotFoundException f){
        SmbFileInputStream fin = new SmbFileInputStream("smb:" + fileName);
        HSSFWorkbook wb = new HSSFWorkbook();
        System.err.println("Workbook not found at ["+fileName+"], so a blank workbook has been created");
        return wb;
    }


  // create a new org.apache.poi.poifs.filesystem.Filesystem
    Path path = Paths.get(fileName);

    POIFSFileSystem poifs = null;
    HSSFWorkbook wb = new HSSFWorkbook();

    try{poifs = new POIFSFileSystem(fin);}
        catch(java.io.IOException i){throw new IllegalArgumentException("loadWorkbook could not create poifs from filename ["+fileName+"]");}

    try{wb = new HSSFWorkbook(poifs);}
        catch(java.io.IOException i){throw new IllegalArgumentException("loadWorkbook could not read workbook from filename ["+fileName+"]");}

    try{fin.close();}
        catch(java.io.IOException i){throw new IllegalArgumentException("loadWorkbook could not close workbook from filename ["+fileName+"]");}

    return wb;
}

我收到的确切错误消息是

com\tem\POIStuff.java:737: error: cannot find symbol
    try{poifs = new POIFSFileSystem(fin);}
                                    ^
symbol:   variable fin
location: class POIStuff
com\tem\POIStuff.java:741: error: cannot find symbol
    try{fin.close();}
        ^
symbol:   variable fin
location: class POIStuff
2 errors

我正在与其他程序员一起编写此代码,因此其中一些代码略高于我的理解。但我不明白为什么 SmbFileInputStream 的实例化失败。

谢谢!

4

1 回答 1

0

问题是 fin 在到达那条线时超出了范围。这是您的代码中的范围。

public static HSSFWorkbook loadWorkbook(String fileName){ //start method scope
    try{//start try scope
    } //end try scope
    catch(...) { //start catch scope
    }//end catch scope
}//end method scope

变量只能在其各自的范围内使用。在 try 范围内实例化的变量不存在于该 try 范围之外。我会结合你的尝试语句。

如果您不能将所有内容都放在同一个范围内,则可以将该变量从 try 块中取出。IE:

public static HSSFWorkbook loadWorkbook(String fileName){
    SmbFileInputStream fin = null;
    try{
        fin = new SmbFileInputStream(...);
    } catch(...) {
    }
    //do stuff with fin
}

就个人而言,我宁愿将所有内容放在同一个范围内并实现我自己的异常类:

public static HSSFWorkbook loadWorkbook(String fileName)throws CustomException{
    try{
        SmbFileInputStream fin = new SmbFileInputStream(...);
        //do stuff with fin
        return wb;
    } catch(FileNotFoundException f) {
        //handle file not found
        throw new CustomException("File Not found ...", f);
    } catch(IOException io) {
        //handle io exception
        throw new CustomException("loadWorkBook failed", io);
    }
}

你也不应该这样做:

try{
    SmbFileInputStream fin = new SmbFileInputStream("smb:" + fileName);
} catch(FileNotFoundException e){
    SmbFileInputStream fin = new SmbFileInputStream("smb:" + fileName);
    //unreachable code
    ...
}

因为如果实例化 SmbFileInputStream 在 try 块中抛出一个 file not found 异常,那么它肯定会在 catch 块中抛出一个异常。catch 中第一行之后的所有内容都不会执行。

于 2013-12-10T18:48:05.387 回答