为什么我的程序拒绝访问创建文件的权限? [英] Why is my program denying access to create a file?

查看:333
本文介绍了为什么我的程序拒绝访问创建文件的权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在阅读的有关Java的书中,它通过使用写入文件并将其存储起来的程序演示了序列化。我遇到一个奇怪的错误,我不知道如何阅读,并且它拒绝我创建.txt文件。错误如下:

In the book I'm reading on Java, it demonstrates serialization by using a program that writes to a file and stores it away. I'm getting a strange error that I don't know how to read and it denies me access to creating a .txt file. Here's the error:

Exception in thread "main" java.io.FileNotFoundException: C:\testFile.txt (Access is denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at serializableTests.MyProgram.main(MyProgram.java:18)

该程序有以下两个类别:

Here's the two classes for the program:

用户类别:

public class User implements Serializable {

    private static final long serialVersionUID = 4415605180317327265L;

    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

这是主要的类:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;


public class MyProgram {
    public static void main(String[] args) throws IOException {
        User user = new User();
        user.setUsername("tpage");
        user.setPassword("password123");

        File file = new File("C:\\testFile.txt");
        OutputStream fileOutputStream = new FileOutputStream(file);
        ObjectOutputStream outputStream = new ObjectOutputStream(fileOutputStream);
        outputStream.writeObject(user);
        System.out.println("I've store the user object in a file called " + file.getName());
    }
}




输出

output



2019-04-22 08:40:28,895 [main] INFO  g.t.service.impl.CsvServiceImpl - Directory structure created data/test/tnx-log/tnc.log false 
2019-04-22 08:40:28,895 [main] INFO  g.t.service.impl.CsvServiceImpl - file.getAbsoluteFile() : C:\Users\jigar\apps\workspace\trade-publisher\data\test\tnx-log\tnc.log canWrite() : false
2019-04-22 08:40:28,895 [main] INFO  g.t.service.impl.CsvServiceImpl - txn log file created data/test/tnx-log/tnc.log true 

2019-04-22 08:40:28,957 [main] INFO  g.t.service.impl.CsvServiceImpl - Directory structure created data/test/tnx-log/tnc.log false 
2019-04-22 08:40:28,957 [main] INFO  g.t.service.impl.CsvServiceImpl - file.getAbsoluteFile() : C:\Users\jigar\apps\workspace\trade-publisher\data\test\tnx-log\tnc.log canWrite() : false


@Override
    public void createTxnInfoFile() throws IOException {
        File file = new File(txnLogFile);
        if (!file.exists()) {
            boolean directoryStructureCreated = file.getParentFile().mkdirs();
            logger.info("Directory structure created {} {} ", txnLogFile, directoryStructureCreated);
            logger.info("file.getAbsoluteFile() : " + file.getAbsoluteFile() + " canWrite() : " + file.canWrite());
            boolean fileCreated = file.createNewFile();
            logger.info("txn log file created {} {} ", txnLogFile, fileCreated);
        }
        file = null;
    }


推荐答案

在Windows的最新版本中,没有提升的特权,您将无法写入系统驱动器的根文件夹。

On recent versions of Windows, you cannot write to the root folder of the system drive without elevated privileges.

要使其正常运行,请将位置更改为另一个驱动器或更改为C中的子文件夹,例如您的配置文件目录。 c:\users\yourname\testfile.txt

To make it work, change the location to another drive or change to a subfolder in C, such as your profile directory, e.g. c:\users\yourname\testfile.txt

(请注意,您使用的是.txt结尾,但生成的文件在编辑器中无法读取。序列化是一种二进制格式。)

(Note that you're using a .txt ending but the file produced will not be readable in an editor. Serialization is a binary format.)

编辑:

要在代码更改中实现

File file = new File("C:\\testFile.txt");

类似

File file = new File("C:\\users\\bane\\testFile.txt");

我用过您的SO名称 bane-用您PC上的登录名替换

I've used your SO name "bane" - replace with whatever your login name is on your pc.

这篇关于为什么我的程序拒绝访问创建文件的权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆