English 中文(简体)
Java台不能在C号:驱动器
原标题:Java desktop app cannot write a file on C: drive

我正试图在C:驾驶车上写一个档案,但我例外。

java.io.IOException: Access granted.

法典:

public class Test {

    public static void main(String[] args) {
        try {
            StringBuilder sb = new StringBuilder(File.separator);
            sb.append("index.txt");
            // sb is "\index.txt"
            File f = new File(sb.toString());
            boolean isCreated = f.createNewFile();
            System.out.println(isCreated);
        } catch (IOException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

实际上,我得到的是,我没有获准在那里书写档案,但我确实可以做些什么。 如果我有一份备忘录,我只获得许可,但我不知道如何这样做。

可能的解决办法是检查,如果我能在那里书写档案,但为了检查,我可能会试图先验地写一份档案,然后删除,以便检查是否有可能在那里撰写档案,但我并没有找到最佳解决办法。

最佳回答

我已写出一种方法,即用一种手法,并检查你是否可以在其中填写档案:

static boolean canWrite(String folderPath) {
    File file = new File(folderPath);
    String new_file = "HastaLaVistaBaby";
    if (file.isDirectory()) {
        try {
            new File(file + "\" + new_file).createNewFile();
        } catch (IOException e) {
            return false;
        }
        new File(file + "\" + new_file).delete();
        return true;
    } else {
        return false;

    }
}

为了改进这一系统,您可以检查档案,并取得家长名录,并采用这种方法。

问题回答

这行文如下:

 sb.append("C:\index.txt");

额外的斜.越.。

无论像我这样硬打造一个档案名称,还是从用户那里获得档案名称,你都需要全方位的路程和档案名称。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签