Java无法创建文件 - Linux

时间:2013-04-10 09:46:38

标签: java file-io makefile ioexception bufferedwriter

当我使用Java程序在Ubuntu中创建文件时,我收到此错误:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at actions.create.actionPerformed(create.java:18)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3312)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:702)
    at java.awt.EventQueue$4.run(EventQueue.java:700)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

显然文件没有创建,甚至尝试以root身份运行,但仍然没有。这是我的代码:

    File shortcut;
    shortcut = new File(global.location + "/" + global.name + ".desktop");

    try {
    FileWriter fWrite = new FileWriter(shortcut.getAbsoluteFile());
    BufferedWriter bWrite = new BufferedWriter(fWrite);
    shortcut.createNewFile();

    bWrite.write("[Desktop Entry]\nType=" + global.type + "\nTerminal=" + global.term + "\nIcon=" + global.icon + "\nExec=" + global.exec + "\nName=" + global.name);
    bWrite.close();
    } catch (IOException e1) {}

有人可以帮忙解决这个问题吗?在使用java创建文件时,我总是遇到这个问题。

3 个答案:

答案 0 :(得分:0)

你应该做

System.out.println(shortcut.getAbsoluteFile());

并仔细检查目录是否真的存在。我有一种感觉,shortcut.createNewFile();因此而抛出了NPE。

答案 1 :(得分:0)

尝试输入父文件夹和文件名的文件创建者:

File subFolder=new File(folderName,subFolder);
File f=new File(folderName,filename;)
System.out.println(f.getAbsolutePath()); //this to see the actual path of the file
subFolder.mkdirs();
BufferedWriter writer=new BufferedWriter(new FileWriter(f));

答案 2 :(得分:0)

我会说实话,我不知道你在这里做了什么,好像你在黑暗中试图制作文件。我想向你展示一个可以帮助你的课程(我创建的):

package your.package;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileHelper
{
    public static boolean createFile(String dir)
{
    return FileHelper.createFile(dir, false);
}

public static boolean createFile(String dir, boolean isDirectory)
{
    boolean returning = false;
    boolean created = false;

    Path p = Paths.get(dir);
    try
    {
        if (Files.exists(p))
        {
            returning = true;
        }
        else if (isDirectory)
        {
            Files.createDirectory(p);
            created = true;
            returning = true;
        }
        else
        {
            Files.createFile(p);
            created = true;
            returning = true;
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    if (returning)
    {
        Util.println((created ? "Created New " : "") + (isDirectory ? "Directory" : "File") + (created ? "" : " Already Exists") + " In: " + p.toString());
    }
    return returning;
}

public static boolean writeToFile(String fileName, String stuff)
{
    return FileHelper.writeToFile(fileName, stuff, true);
}

public static boolean writeToFile(String fileName, String stuff, boolean newLine)
{
    try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true)))
    {
        writer.write(stuff);
        if (newLine) writer.newLine();
        return true;
    }
    catch (IOException x)
    {
        System.err.format("IOException: %s%n", x);
        x.printStackTrace();
        return false;
    }
}

public static BufferedReader getFileReader(String fileName)
{
    Charset c = Charset.forName("US-ASCII");
    Path p = Paths.get(fileName);

    try
    {
        return Files.newBufferedReader(p, c);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    return null;
}

public static boolean deleteDirectory(File dir)
{
    Util.requireNonNull(dir);

    if (!dir.exists() || !dir.isDirectory())
    {
        throw new IllegalArgumentException("\"dir\" Must exist!");
    }

    String[] files = dir.list();

    for (int i = 0, len = files.length; i < len; i++)
    {
        File f = new File(dir, files[i]);

        if (f.isDirectory())
        {
            deleteDirectory(f);
            Util.println("Found new directory to delete: " + f.getPath() + " Deleting now...");
        }
        else
        {
            f.delete();
            Util.println("Deleted File: " + f.getPath());
        }
    }
    Util.println("Deleted Directory: " + dir.getPath());
    return dir.delete();
}
}

让我知道这是否有助于解决您的问题!顺便说一句,你不需要为这个文件给我任何功劳。现在是公众!好运和快乐的编码!