【问题标题】:Statically Loading a .txt File静态加载 .txt 文件
【发布时间】:2015-11-07 22:23:30
【问题描述】:

我知道这个问题以前经常被问到,但其他答案都没有对我有用。我正在尝试静态加载 .txt 文件。它在编译器 (Eclipse) 中运行良好,但在导出后出现 FileNotFound 异常。

我需要一个方法来接收文件路径并静态加载该 .txt 文件,并将该文件作为字符串返回。我想我必须对loadRecourceAsStream() 做点什么,但我不确定。

这是我现在的加载方式:

public static String getFilePath(String path) {

    String line = null;
    String file = "";

    try {

        FileReader fileReader = new FileReader(path);
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) != null){
            file = file + line;
        }

        bufferedReader.close();
    } catch(FileNotFoundException e) {
        System.out.println("Unable to open file " + path + "\n");
        e.printStackTrace();
    } catch(IOException e) {
        e.printStackTrace();
    }

    return file;
}

这是我尝试过的其他一些事情。它们都在编译器中工作,但在导出后不工作:

public static String loadFileAsString(String path){
    StringBuilder builder = new StringBuilder();

    try{
        BufferedReader br = new BufferedReader(new FileReader(path));
        String line;
        while((line = br.readLine()) != null)
            builder.append(line + "\n");
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream(path);
        br.close();
    }catch(IOException e){
        e.printStackTrace();
    }

    return builder.toString();
}

还有:

public static String getFile(String path) {
    Scanner controlBoard = new Scanner(System.in);
    controlBoard = new Scanner(Utils.class.getResourceAsStream(path));

    String file = controlBoard.nextLine();
    file += "\n" + controlBoard.nextLine();
    file += "\n" + controlBoard.nextLine();

    return file;

} 

还有:

 public static String getFile(String path) {
    System.out.println("test");
    StringBuilder out = new StringBuilder();
    try {
        InputStream in = new FileInputStream(new File(path));
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line;
        while((line = reader.readLine()) != null) {
            out.append(line + "\n");
        }
        reader.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch(IOException e) {
        e.printStackTrace();
    }

    return out.toString();
}

对我应该做什么有什么想法吗?

【问题讨论】:

  • 你检查过创建的.jar 吗?它是否包含您尝试访问的文件?在 jar 内的哪个文件夹中?当您尝试使用 getResourceAsStream 时,这需要匹配您的代码
  • 什么“导出后不起作用”?您遇到了哪些异常和/或错误? “不起作用”没有提供足够的信息让任何人在不胡思乱想的情况下帮助您。 “对我应该做什么有什么想法吗?”是的。首先提供有关无效的详细信息。
  • @Andrew Henle 正如我所说,一个 FileNotFoundException
  • @Marged 它在里面,位于 NameOfJar.jar/maps/Level1.txt。对路径执行 /maps/Level1.txt 仍然会导致错误。

标签: java file load


【解决方案1】:

我认为您的问题必须与导出时如何运行程序有关,因为我已经获取了您的代码,创建了一个 jar,运行它并且它在不更改一行的情况下运行。好吧,我添加了一个 main 函数来运行它。我添加代码以防万一,但您会看到代码是相同的。

package com.iseji.app.main;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Main {

public static void main (String args[]){
    System.out.println(getFilePath(args[0]));
}

public static String getFilePath(String path) {

    String line = null;
    String file = "";

    try {

        FileReader fileReader = new FileReader(path);
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) != null){
            file = file + line;
        }

        bufferedReader.close();
    } catch(FileNotFoundException e) {
        System.out.println("Unable to open file " + path + "\n");
        e.printStackTrace();
    } catch(IOException e) {
        e.printStackTrace();
    }

    return file;
}
}

所以问题是您是如何将文件导入到 jar 中的。您实际上可以只使用 IDE(Eclipse、IntelliJ 或其他)的“导出到”功能。另一方面,我建议您使用像 maven 或 gradle 这样的生命周期软件框架。作为一个例子,我展示了我用来创建 jar 的 gradle 文件(但正如我所说,这并不重要)

apply plugin: 'java'

sourceCompatibility = 1.5
version = '1.0'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

jar {
    manifest {
        attributes 'Main-Class': 'com.iseji.app.main.Main'
    }
}

不管如何将代码导出到 jar 中,关键是 Manifest 文件的外观。您必须确保它指示哪个是主类。验证它是否类似于:

Manifest-Version: 1.0
Main-Class: com.iseji.app.main.Main

通过这种方式,您将能够正常运行它,将要读取的文件作为参数传递

java -jar ReadingFile-1.0.jar ../../src/main/resources/sample.txt

正如我在开始时所说,您的代码很好。只需仔细检查您是如何导出代码并运行它的

【讨论】:

  • 我的清单文件有这一行:Main-Class: main.Main。我正在使用基本的 eclipse Export>executable jar 进行导出。我的 txt 文件的位置是 maps.Level1。我确实有一些我设法像这样加载的图像:ImageIO.read(ImageLoader.class.getResourceAsStream(path)); 我尝试将路径更改为 \maps\Level1.txt 和 .\maps\Level1.txt。不知道导出有什么问题。
  • 它必须以明确的方式指向您的班级。如果您的 Main 类在 main 包中很好(在我的情况下,它是在 com.iseji.app.main 包中)。
  • 但是运行jar的时候如何指定路径呢?
  • 什么意思,运行jar时指定路径?你的意思是像\maps\Level1.txt?
  • 我的意思是您如何提供txt所在的位置。如果您在我的示例中看到我通过“java -jar ReadingFile-1.0.jar ../../src/main/resources/sample.txt 运行../../src/main/resources/sample.txt 是txt 文件与 jar 文件所在位置的相对位置。我之所以问,是因为在您得到的错误函数(FileNotFound 异常)中,您似乎没有指向 txt 所在的位置。
猜你喜欢
  • 2021-05-28
  • 1970-01-01
  • 2019-08-31
  • 2015-03-10
  • 2014-11-12
  • 2021-04-03
  • 2012-01-21
  • 2016-11-06
  • 2017-11-08
相关资源
最近更新 更多