Java - 在JAR文件中写入txt文件

6

可能是重复问题:
Java程序如何使用JAR文件内部的文件进行读写操作?

我该如何从一个已编译的JAR Java项目中向一个 .txt 文件写入内容?

当我运行我的项目时,它不会报错,但它就是无法将内容写入 JAR 文件中的 .txt 文件。

我是用以下方式创建 JAR 文件的:

netbeans clean /build tool

代码:

public class FileIO   {
    private File file;
    private Scanner filescScanner, lineScanner;
    private PrintWriter fileWriter;
    private String[][] data;

    public FileIO () {
        data = new String[100][2];
    }

    public String[][] getLineScores(){
        return this.loadHighscores(this.getClass().getResourceAsStream("LineHighscores.txt"));
    }

    public String[][] getTimeScores(){
        return this.loadHighscores(this.getClass().getResourceAsStream("TimeHighscores.txt"));
    }

    public void setLineScores( String name,String lines ){
        boolean found= false;
        data = this.getLineScores();
        for(int i = 0; i<data.length && !found ; i++){
            if(data[i][0] == null || "Niemand".equals(data[i][0])){
                data[i][0]=name;
                data[i][1]=lines;
                found=true;
            }
        }
        this.saveHighscores(this.getClass().getResource("LineHighscores.txt"),data);
    }

    public void setTimeScores(String time, String name){
        boolean found= false;
        data = this.getLineScores();
        for(int i = 0; i<data.length && !found ; i++){
            if(data[i][0] == null || "Niemand".equals(data[i][0])){
                data[i][0]=name;
                data[i][1]=time;
                found=true;
            }

        }
        this.saveHighscores(this.getClass().getResource("TimeHighscores.txt"),data);
    }

    private String[][] loadHighscores( InputStream resourceStream){
        int x=0;
        String test = "";
        try{
            filescScanner = new Scanner(resourceStream);
        }
        catch(Exception ioe){
            System.err.println(ioe);
        }

        if (filescScanner.hasNext()){
            while(filescScanner.hasNextLine()&& x<100) {
                lineScanner = new Scanner(filescScanner.nextLine());
                lineScanner.useDelimiter("-/-");

                data[x][0]=lineScanner.next();//name
                data[x][1]=lineScanner.next();//data
                x++;
            }
            lineScanner.close();
            filescScanner.close();
        }
        else{
            data[0][0] = "Niemand";
            data[0][1] = "0";
        }
        return data;
    }

    private void saveHighscores( URL resourceStream, String[][] data){
        int x=0;
        try {
            file = new File(resourceStream.toURI());
        } catch (URISyntaxException ex) {
            Logger.getLogger(FileIO.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            fileWriter = new PrintWriter(file);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FileIO.class.getName()).log(Level.SEVERE, null, ex);
        }
        if(data.length>x){
            while(data.length>x && data[x][0] != null ){
                fileWriter.println(data[x][0]+"-/-"+data[x][1]);
                x++;
            }
            fileWriter.close();
        }
    }

    public static void main(String[] args){
        FileIO file = new FileIO();
        file.setLineScores("55555555", "KoenKevin");
    }
}

无法通过此种方式更新Jar,因为它是一个文件。发布后更改Jar并不是一个好主意。你不能使用文件系统保存你的文件吗? - Peter Lawrey
你不能这样做,必须将文本文件保留在jar包之外。 - fredcrs
3个回答

11

你不能这样做,即使你能够这样做,也不可取:写入到jar包外部的位置。


5

Jar是一个归档文件,意味着它应该保持不变。如果您的jar(应用程序)需要写入某些内容,请写到外部来源。

我建议您创建一个独立的文件夹,并让您的应用程序指向该文件夹,在那里进行所有的外部活动。


3
当你试图在jar文件中写入文本文件时,Java无法识别文本文件的绝对路径。它会类似于:
C:User/adom/documents/jarName.jar!/fileName.txt 

这不是一个绝对路径,因此文件无法被写入。尝试在外部编写文件。


3
是的,但我希望jar文件可以移动,那么在Java中保存特定数据的更好方法是什么? - Koen Demonie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接