找不到符号:在try-catch中实例化SmbFileInputStream

时间:2013-12-10 18:22:54

标签: java jcifs

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

我正在尝试创建一个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的实例化失败。

谢谢!

1 个答案:

答案 0 :(得分: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语句。

如果您无法将所有内容放入同一范围,则可以将该变量从try块中取出。即:

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块中抛出一个找不到文件的异常,那么肯定会在catch块中抛出一个异常。捕获中第一行之后的所有内容都将永远不会执行。