使用jar文件

时间:2017-01-24 20:08:11

标签: java eclipse jar

我是新手,这是我的第一篇文章。我已经在日食上做了一个完美的游戏应用程序。它使用几个.txt文件来获得分数和播放器选项。

问题是当我尝试将其导出为runnable jar文件时,这就是问题所在,它会生成一个jar文件并使我无法在任何.txt文件上编写。我知道这是因为我试过,不是数百但是接近,解决方案,其中一些允许我读取文件,但我仍然无法写在他们身上。我意识到这是jar文件的正常运行,所以我的问题是:

如何在包含所有txt文件的同一目录中创建/创建外部文件夹?这样它可以读取和写入这些文件,以及我应该在现有代码中使用哪些方法?

我只是展示了我如何读/写其中一个文件,但对于其他每个文件它都是一样的,我还会显示其他过去解决方案的一些评论:

    private final String cubesScore =  "resF\\score.txt";
    //private final String cubesScore =  "/score.txt";
    //private final String cubesScore =  "//resF//score.txt";

    try{
        /*
        try{
            File file = new File(cubesScore);
            //FileReader reader = new FileReader(new File(new File("."), "score.txt"));

            if(file.createNewFile()){
                System.out.println("File created successfully!");
            } else{
                System.out.println("File already exists.");
            }

        } catch(IOException e){
            System.out.println("An error occurred on score.txt!!");
        }
        */
        Scanner scanner = new Scanner(new File(cubesScore));

        //InputStream source = this.getClass().getResourceAsStream(cubesScore);
        //Scanner scanner = new Scanner(source);

        /*
        InputStream inputStream = this.getClass().getResourceAsStream(cubesScore);
        InputStreamReader inputReader = new InputStreamReader(inputStream);
        BufferedReader reader = new BufferedReader(inputReader);
        */
        int value;
        int i = 0;
        while(scanner.hasNext()){
            value = scanner.nextInt();
            if(value >= 0){
                mostCubesDestroyed[i] = value;
                System.out.println(value);
            }
            else 
                System.out.println("File corrupted");
            ++i;
        }
        scanner.close();
    } catch (NullPointerException | FileNotFoundException e) {
        System.out.println("File Not Found/Null");
    }

写:

        try {
             PrintWriter out = new PrintWriter(cubesScore);
             //OutputStream out = this.getClass().getResourceAsStream(cubesScore);
             //out = new PrintWriter(out);
             //BufferedWriter out = new BufferedWriter(new FileWriter(cubesScore));
             //out.write(mostCubesDestroyed[0]);
             //out.newLine();
             out.println(mostCubesDestroyed[0]);
             System.out.println(mostCubesDestroyed[0]+" Cubes GameMode 0");
             //out.write(mostCubesDestroyed[1]);
             //out.newLine();
             out.println(mostCubesDestroyed[1]);
             System.out.println(mostCubesDestroyed[1]+" Cubes GameMode 1");
             //out.write(mostCubesDestroyed[2]);
             //out.newLine();
             out.println(mostCubesDestroyed[2]);
             System.out.println(mostCubesDestroyed[2]+" Cubes GameMode 2");
             //out.write(mostCubesDestroyed[3]);
             //out.newLine();
             out.println(mostCubesDestroyed[3]);
             System.out.println(mostCubesDestroyed[3]+" Total Cubes Destroyed");
             out.close();
          } catch (IOException e) {
                 System.out.println("Error, try again!!");
      }

我意识到保留评论的代码会让它稍微难以阅读,但我还是想告诉你一些我尝试过的东西......

我还试图在应用程序第一次运行时创建该文件,但没有成功:

      try{
            File file = new File(cubesScore);
            //FileReader reader = new FileReader(new File(new File("."), "score.txt"));

            if(file.createNewFile()){
                System.out.println("File created successfully!");
            } else{
                System.out.println("File already exists.");
            }

        } catch(IOException e){
            System.out.println("An error occurred on score.txt!!");
        }

所以这就是我的问题,如果有人曾经在这里,并以某种方式设法找到解决方案,或者你只是知道如何产生所需的结果,即在jar外部的.txt文件上读/写(因为内部引线)各种各样的问题)然后请告诉我我应该做什么,我已经看到超过一百个帖子和视频,仍然无法找到所需的解决方案。

编辑:这已在下面解决,原来我需要一个。在“/score.txt”上a。在所有文件中。

2 个答案:

答案 0 :(得分:1)

你试过这个吗?: private final String CUBES_SCORE =" ./ score.txt&#34 ;; 如果你想在子目录中,你也必须创建子目录。 另外,请看一下:How do I create a file and write to it in Java?

答案 1 :(得分:0)

我认为你的道路上有一些问题  private final String cubes =" .. \ resF \ score.txt&#34 ;;

希望它有所帮助:)

相关问题