获得权限后获取权限被拒绝错误

时间:2015-08-17 03:32:17

标签: java excel error-handling io conceptual

我正在将excel电子表格文件(.xls)写入我有权限的目录,xlsOutput是我在项目根目录下创建的具有完全权限的目录(右键单击目录>属性>资源?所有三个组(所有者,组,其他)的rwx

但是我得到了流动的堆栈跟踪:

java.io.FileNotFoundException: /xlsOutput (Permission denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:194)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:145)
    at jxl.Workbook.createWorkbook(Workbook.java:301)
    at jxl.Workbook.createWorkbook(Workbook.java:286)
    at com.generalatomics.ctg.taxengine.automation.tools.testhelper.writers.ExcelTemplateWriter.write(ExcelTemplateWriter.java:33)
    at com.generalatomics.ctg.taxengine.automation.tools.testhelper.TestClasses.testXMLToXLS(TestClasses.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

不确定我为什么不能写到这个目录?我错过了一些明显的东西吗我觉得我是。非常感谢任何帮助或帮助,谢谢。

编辑:

@Test
public void testXMLToXLS() throws Exception {
    ITemplateGenerator tGen = new CalcTemplateGenerator();
    TestTemplate template = tGen.generateTemplate("xmlDir");
    ITemplateWriter writer = new ExcelTemplateWriter();

    String file = "/xlsOutput";
    File f = new File(file);
    logger.debug("Can write: "+f.canWrite()); // returns false but why??
    writer.write(template, file);
}

public void write ( TestTemplate template , String path ) throws Exception {

    CellFormat formatHeaders = new WritableCellFormat( new WritableFont( WritableFont.createFont("Calibri"), 12,  WritableFont.BOLD ) );
    CellFormat formatText = new WritableCellFormat( new WritableFont( WritableFont.createFont("Calibri"), 12,  WritableFont.NO_BOLD ) );

    // Create workbook object at the specified path (output directory)
    WritableWorkbook workbook = Workbook.createWorkbook( new File ( path));

    // Create a spreadhseet with name of topic and the index number at which to insert
    WritableSheet sheet = workbook.createSheet( template.getTopic(), 0) ;
    ...etc
} 

编辑:

想出来......

String file =&#34; xlsOutput / testFile.xls&#34 ;;

我不应该包括&#34; /&#34;在目录前...愚蠢的错误。谢谢大家的帮助

2 个答案:

答案 0 :(得分:0)

您提供了一个绝对文件系统路径/xlsOutput,一个以/开头。如果要省略前导“/”,JRE会将其解释为相对于系统属性 user.home ,这可能是您项目的目录。试试吧。

答案 1 :(得分:0)

我犯了一个愚蠢的错误,包括&#34; /&#34; @immibis谢谢