Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用jar文件读/写外部.txt文件_Java_Eclipse_Jar - Fatal编程技术网

Java 使用jar文件读/写外部.txt文件

Java 使用jar文件读/写外部.txt文件,java,eclipse,jar,Java,Eclipse,Jar,我是一个新手,这是我的第一篇帖子。我在eclipse上做了一个应用程序,效果非常好。它使用一些.txt文件来获取分数和玩家选项 问题是,当我试图将其导出为可运行的jar文件时,这就是问题所在,它生成了一个jar文件,使我无法在我拥有的任何.txt文件上进行编写。我之所以知道这一点,是因为我尝试了很多解决方案,虽然不是几百个,但越来越接近了,其中一些方案允许我阅读文件,但我仍然不能在上面写字。我意识到这是jar文件的正常功能,所以我的问题是: 如何在包含所有txt文件的同一目录中为jar文件创建一

我是一个新手,这是我的第一篇帖子。我在eclipse上做了一个应用程序,效果非常好。它使用一些.txt文件来获取分数和玩家选项

问题是,当我试图将其导出为可运行的jar文件时,这就是问题所在,它生成了一个jar文件,使我无法在我拥有的任何.txt文件上进行编写。我之所以知道这一点,是因为我尝试了很多解决方案,虽然不是几百个,但越来越接近了,其中一些方案允许我阅读文件,但我仍然不能在上面写字。我意识到这是jar文件的正常功能,所以我的问题是:

如何在包含所有txt文件的同一目录中为jar文件创建一个外部文件夹?这样它就可以在这些文件中读写,我应该在现有代码中使用什么方法

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

    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”上很好。在所有文件中。

您尝试过这个吗?: 私有最终字符串CUBES_SCORE=“./SCORE.txt”; 如果您希望它位于子目录中,则还必须创建子目录。
另外,看看这个:

我认为在你的道路上有一些问题 私有最终字符串cubesScore=“..\resF\score.txt”


希望有帮助:)

类路径中的.jar文件是只读的。(有些系统可能允许您对其进行写入,但由于这不能保证,您应该将其视为只读。)改为在可靠可用的跨平台位置写入文件,如
System.getProperty(“user.home”)
.Ty对于您的帮助,解决方案比我想象的更简单……很多,我不敢相信我已经经历了这么多的时间和斗争,最后,一个单身汉。在“/score.txt”之前,缺少的链接-。-,已经测试并运行,创建了文件,没有第一次读取,因为没有信息,在所有行上将信息写入0(起始值),然后读取并运行。现在我有另一个问题,但它与这个问题无关,首先我会尝试解决,如果没有,我会创建一个新的帖子,所以这个问题得到了解决。我不知道如何结束这篇文章。问题已经解决,我不知道如何结束这个问题:/,仍然等待答复