Java FileWriter和jar文件-找不到资源

Java FileWriter和jar文件-找不到资源,java,resources,jar,Java,Resources,Jar,我最近写了一个程序。文件处理类是下一个: package smartest.eu; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Processor { private Buff

我最近写了一个程序。文件处理类是下一个:

package smartest.eu;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Processor {
    private BufferedWriter out;
    private BufferedReader in;

    // this method opens the file for reading!
    public void openWriter() throws Exception {
        if (out == null) {
            try {
                out = new BufferedWriter(new FileWriter(new File("src/personaldata.txt")));
            }
            catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        else {
            throw new Exception("Writer opened already");
        }
    }

    // this method opens the reader
    public void readWriter() throws Exception {
        if (in == null) {
            try {
                in = new BufferedReader(new FileReader("src/personaldata.txt"));
                in.mark(65538);
            }
            catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        else {
            throw new Exception("Reader already opened");
        }
    }

    // this method reads the file line by line outputting a string as the result
    public String readText() throws Exception {
        String answer1 = "";
        if (in != null) {
            try {
                if ((answer1 = in.readLine()) != null) {
                    answer1 += "\n";
                }
                else {
                    answer1 = "No more data!\nThe stream will be read from the beginning!\n";
                }
            }
            catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        else {
            throw new Exception("Open file for reading at first");
        }

        return answer1;
    }

    // this method writes the given string to a file
    public void writeText(String a) throws Exception {
        if (out != null) {
            out.write(a);
            out.flush();
        }
        else {
            throw new Exception("Open file for writing at first");
        }
    }

    public void resitStream() {
        try {
            in.reset();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
目录结构processor.jar:

1) manifest
2) personaldata.txt
3) smartest/eu/*.class
但是,当我创建jar文件时,它找不到该文件(给出“文件未找到异常”)。我怎样才能修好它? 另外,关于这个问题,你能提一点需要阅读的内容吗?为什么.jar文件在jar中不可用?

new FileReader("src/personaldata.txt")
尝试打开当前目录的子目录
src
中的文件
personaldata.txt
。当前目录是在命令提示符下启动java命令时所处的目录。因此,如果您在
c:\
中并启动
java-jar/path/to/myJar.jar
,它将在c:\src\personaldata.txt中搜索该文件

因此,文件用于访问文件系统。不是jar中的资源。要从类路径加载资源,必须使用

Processor.class.getResourceAsStream("/personaldata.txt")
请查看以了解其工作原理


请注意,写入jar中的文件是不可能的。

谢谢。这是一个有用的提示。