Java - 在 JAR 文件中写入 txt

标签 java file-io jar executable-jar streamwriter

<分区>

Possible Duplicate:
How can a Java program use files inside the .jar for read and write?

如何从 JAR 写入 .txt 文件java编译项目?

当我运行我的项目时,它没有给出错误,但它只是不写入 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 之外的位置。

关于Java - 在 JAR 文件中写入 txt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13937904/

相关文章:

java - 我在我的代码中输入了一些针对控制台的内容,但我找不到我搞砸的地方

java - 错误 “can' t 创建 Java VM”尝试使用 Ruby Java Bridge (RJB) gem

python - 如何显示列表?

c - 如何在devcpp中以非阻塞模式打开文件?

java - 在另一台未配置的计算机上运行 selenium webdriver 测试

java - 定期刷新大对象

java - : "from.<Integer>"?是什么意思

c++ - 读取文件 on.get 函数时程序卡住

java - JNLP 将 jar 文件下载到本地系统的位置。

vba - 从 VBA/VBScript/Visual Basic Classic 调用 Java 库 (JAR)