簡體   English   中英

使用Java的File類以及如何獲得創建文件的正確訪問權限

[英]Using Java's File class and how to gain correct access permissions to create files

以下是我編寫的房屋清潔功能的代碼,該功能應檢查文件是否存在,如果不存在,則創建文件並向其中添加一些數據。 但是,當我檢查是否已使用file.canRead()file.canWrite()讀取和寫入file.canRead()file.canWrite()在檢查時都返回false,但是程序應可以訪問指定的文件路徑。

public void HouseCleaning()
{
    //inform the user that the file is not available 
    System.out.println("According the the checks we have run, the current system you are on we do not have the required files set up");
    System.out.println("...");
    //create info.txt
    try
    {
        File file = new File("C:\\GameCounter\\info.txt");
        System.out.println(file.canRead());
        System.out.println(file.canWrite());
        if(file.canRead() && file.canWrite())
        {

            //then we can create the file
            System.out.println("we can do this");
            if(!file.exists())
            {
                //file does not exist
                if(file.createNewFile())
                {
                    //file has been created
                    System.out.println("File has been successfully created!");
                    PrintWriter writer = new PrintWriter("C:\\GameCounter\\info.txt", "UTF-8");
                    writer.println("Info File:");
                    writer.flush();
                    writer.close();
                }
                else
                {
                    //file has not been created!
                    System.out.println("for some reason the file cannot be created!");
                }

            }
            else
            {
                //file must already exist? so check for other required ones!
            }
        }
        else
        {
            System.out.println("we require extre permissions!");
        }





    }
    catch(Exception e)
    {
        //error has been thrown
        System.out.println(e);

    }
}

因此,我的問題是首先是從理論上講,如果下面的代碼正確無誤,那么它就是對硬盤本身的許可嗎? 如果代碼不正確,請糾正我。 非常感謝您對此的任何幫助。

如果文件不存在,則canReadcanWrite方法canRead返回false

引用文檔(canRead):

返回:當且僅當此抽象路徑名指定的文件存在並且可以由應用程序讀取時,才返回true;否則返回true。 否則為假

和(canWrite):

返回:當且僅當文件系統實際包含此抽象路徑名表示的文件並且允許應用程序寫入該文件時,才返回true;否則返回true。 否則為假。

file.canRead() file.canWrite()返回false的原因很可能是因為您尚未創建文件。

Java Doc public boolean canRead()測試應用程序是否可以讀取此抽象路徑名表示的文件。

當您首先創建文件時,該方法調用將返回true。 記住,文件是系統文件的表示,僅創建File對象的實例就不會創建文件

我建議您更改程序,並使用Javas Exception Handling提供的優點。

private final static String COUNTER = "C:\\GameCounter\\info.txt";

    public static void main(String[] args) {
        File file = new File(COUNTER);
        if (!file.exists()) {
            try {
                PrintWriter writer = new PrintWriter(COUNTER, "UTF-8");
                writer.println("Info File:");
                writer.flush();
                writer.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        /// ... more to come
    }

這要短得多,而且無論如何您都必須處理異常。 如果無法將文件寫入(在我的測試中,我只是創建了該文件並為其分配了readonly屬性),您將收到相應的異常:

java.io.FileNotFoundException: C:\GameCounter\info.txt (Access denied)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(FileOutputStream.java:270)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
    at java.io.PrintWriter.<init>(PrintWriter.java:192)
    at java.io.PrintWriter.<init>(PrintWriter.java:232)
    at xyz.main(xyz.java:12)

在我的示例中,我僅將異常轉儲到屏幕上。 在現實生活中,您需要對異常做出反應,並可能重新拋出自己定義的異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM