5

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

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)

这是该程序的两个类:

用户等级:

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());
    }
}

输出

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;
    }
4

2 回答 2

13

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

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

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

编辑:

在代码更改中实现这一点

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

类似于

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

我使用了您的 SO 名称“bane” - 替换为您 PC 上的任何登录名。

于 2013-04-10T01:24:59.340 回答
0
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CopyFiles {
    private File targetFolder;
    private int noOfFiles;
    public void copyDirectory(File sourceLocation, String destLocation)
            throws IOException {
        targetFolder = new File(destLocation);
        if (sourceLocation.isDirectory()) {
            if (!targetFolder.exists()) {
                targetFolder.mkdir();
            }

            String[] children = sourceLocation.list();
            for (int i = 0; i < children.length; i++) {
                copyDirectory(new File(sourceLocation, children[i]),
                        destLocation);

            }
        } else {

            InputStream in = new FileInputStream(sourceLocation);
            OutputStream out = new FileOutputStream(targetFolder + "\\"+ sourceLocation.getName(), true);
            System.out.println("Destination Path ::"+targetFolder + "\\"+ sourceLocation.getName());            
            // Copy the bits from instream to outstream
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
            noOfFiles++;
        }
    }

    public static void main(String[] args) throws IOException {

        File srcFolder = new File("C:\\sourcefolder\\");
        String destFolder = new String("C:\\destination\\");
        CopyFiles cf = new CopyFiles();
        cf.copyDirectory(srcFolder, destFolder);
        System.out.println("No Of Files got Retrieved from Source ::"+cf.noOfFiles);
        System.out.println("Successfully Retrieved");
    }
}
于 2014-12-11T09:36:49.877 回答