尝试将文件创建到(可能)正确的目录时java.io.FileNotFoundException

时间:2015-01-25 22:04:59

标签: java path relative-path filepath fileoutputstream

我的问题是,当我使用它时:

final File f = newFile(ButtonOneEvent.class.getProtectionDomain().getCodeSource().getLocation().getPath());
filePath = f.toString() + "/PlayerList.txt"; //Playerlist.txt being the file I want to create, filePath being an empty string.
System.out.println(f);

要为此定义类的本地目录:

FileOutputStream fout = new FileOutputStream(Start.filePath); //**Reference Point**
if(inputData != null) {     //Ignore the if else statement
    new PrintStream(fout).println("Text Stuff")
} else {
    new PrintStream(fout).println("Text Stuff")
}
fout.close();

当我想要创建此文件时,我在“参考点”行上获得java.io.FileNotFoundException,当然它还没有。 此代码由JButton激活 BTW Sytem.out.println(f)输出:

*/Users/HillFamily/Library/Application%20Support/Youth%20Digital/Software/Forge/mcp/eclipse/Football%20Card%20Sorter/bin* <- Directory

StackTrace :(抱歉这么久!)

java.io.FileNotFoundException: /Users/HillFamily/Library/Application%20Support/Youth%20Digital/Software/Forge/mcp/eclipse/Football%20Card%20Sorter/bin/PlayerList.txt (No such file or directory)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:194)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:84)
    at events.ButtonOneEvent.actionPerformed(ButtonOneEvent.java:33)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6414)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
    at java.awt.Component.processEvent(Component.java:6179)
    at java.awt.Container.processEvent(Container.java:2084)
    at java.awt.Component.dispatchEventImpl(Component.java:4776)
    at java.awt.Container.dispatchEventImpl(Container.java:2142)
    at java.awt.Component.dispatchEvent(Component.java:4604)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4618)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4279)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4209)
    at java.awt.Container.dispatchEventImpl(Container.java:2128)
    at java.awt.Window.dispatchEventImpl(Window.java:2492)
    at java.awt.Component.dispatchEvent(Component.java:4604)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:717)
    at java.awt.EventQueue.access$400(EventQueue.java:82)
    at java.awt.EventQueue$2.run(EventQueue.java:676)
    at java.awt.EventQueue$2.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:690)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:687)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

3 个答案:

答案 0 :(得分:0)

很可能是因为您的Start.filePath是目录(不是文件),无法创建或根本不存在。此外,Start.filePath依赖于系统,必须符合您的平台。您需要确保这是正确的,并且您所指的位置也具有足够的写操作访问权限。另一个问题,你的完整路径名如何知道你正在研究/写入正确的位置?路径中的某些目录确实存在吗?

要从修复操作开始 - 您可能需要检查System.out.println(Start.filePath)是否打印任何有意义的内容。如果您遇到空格和URL编码问题(例如,%20是ASCII中的空格),您需要在问题上下文中开始考虑它们。

作为示例(从Windows的角度来看),请参阅以下内容:

    public static void main(String[] args) {
        try{

         FileOutputStream fos = new FileOutputStream("C:/myjavaprojects/folder%20with%20a%20space/haha.txt");
         // This is unchecked exception - we have to rethrow/catchprocess it
         BufferedOutputStream bos = new BufferedOutputStream(fos);

         bos.write("Writing to file".getBytes());        
         bos.flush();
         bos.close();
        } catch (FileNotFoundException fnfe){

            try{
                FileOutputStream fos = new FileOutputStream("C:/myjavaprojects/folder with a space/haha.txt");
                 BufferedOutputStream bos = new BufferedOutputStream(fos);
                 bos.write("Writing to file".getBytes());            
                 bos.flush();
                 bos.close();
            } catch(FileNotFoundException e){
                e.printStackTrace();
            } catch (IOException ioe){
                ioe.printStackTrace();
            }

        } catch (IOException ioe){
            ioe.printStackTrace();
        }       

    }
}

答案 1 :(得分:0)

首先:看看这个方法'newFile',也许你必须改为正确的文件调用:

文件f =新文件(mypath +“PlayerList.txt”);

其次,尝试查看你的类成员'filePath'是否未被更改(Start类的成员)。

答案 2 :(得分:0)

如果要创建OutputStream以写入文件使用:
如果文件不存在,这也将创建文件。

try {
        Path p = Paths.get("demo", "test.txt");
        Files.createDirectories(p.getParent());
        DataOutputStream out = new DataOutputStream(Files.newOutputStream(p, StandardOpenOption.CREATE, StandardOpenOption.WRITE));
        out.writeBytes("Hello-World!!");
        out.flush();
        out.close();            
    } catch (IOException ex) {
        Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
    }