Java - 在JAR文件中写入txt [英] Java - Writing to txt in a JAR file

查看:209
本文介绍了Java - 在JAR文件中写入txt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何从 JAR java编译的项目?

How do I write to a .txt file from a JAR java compiled project?

当我运行我的项目时,错误,但它不会写入JAR文件中的.txt文件。

When I run my projects, it doesn't give errors, but it just doesn't write to the .txt inside JAR file.

使用JAR文件:

I make the JAR file using:

netbeans clean /build tool



代码:



Code:

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不能识别文本文件的绝对路径。

When you are trying to write a text file inside a jar, java is not recognizing absolute path to text file.

它会像

C:User/adom/documents/jarName.jar!/fileName.txt 

这不是绝对路径,所以无法写入文件。尝试在外部写文件。

This is not a absolute path so file could not be written. Try writing file externally.

这篇关于Java - 在JAR文件中写入txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆