java文件拒绝访问权限,为什么我的程序拒绝访问创建文件的权限?

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.(Unknown Source)

at java.io.FileOutputStream.(Unknown Source)

at serializableTests.MyProgram.main(MyProgram.java:18)

Here's the two classes for the program:

User class:

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;

}

}

And here's the main class:

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;

}

解决方案

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

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

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

EDIT:

To implement this in code change

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

to something like

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值