在Java中,访问文件路径的正确方法是什么?

在Java中,访问文件路径的正确方法是什么?,java,Java,当我尝试写入文件时,我收到一个FileNotFoundException,但我使用的其他类可以从同一路径中的文件读取。下面是我可以从我的参考资料中读取文件的示例: newMap = false; int numTilesAcross; BufferedImage tileset; String testMapPath = "/Resources/Maps/testmap.map"; String testTileSetPath = "/Resources/Tile

当我尝试写入文件时,我收到一个FileNotFoundException,但我使用的其他类可以从同一路径中的文件读取。下面是我可以从我的参考资料中读取文件的示例:

newMap = false;
    int numTilesAcross;
    BufferedImage tileset;
    String testMapPath = "/Resources/Maps/testmap.map";
    String testTileSetPath = "/Resources/Tilesets/testtileset.gif";
    String itemsPath = "/Resources/Sprites/items.gif";
    //Uses specified file as input, then transforms file contents into a 2D array "map"
    try {

        InputStream in = getClass().getResourceAsStream(testMapPath);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        numCols = Integer.parseInt(br.readLine());
        numRows = Integer.parseInt(br.readLine());
        map = new int[numRows][numCols];
        width = numCols * tileSize;
        height = numRows * tileSize;

        String delims = "\\s+";
        for (int row = 0; row < numRows; row++) {
            String line = br.readLine();
            String[] tokens = line.split(delims);
            for (int col = 0; col < numCols; col++) {
                map[row][col] = Integer.parseInt(tokens[col]);
                //System.out.println(map[row][col]);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
这是我的项目的文件结构:

您的
FileNotFoundException
可以通过执行以下操作来解决:

public void saveItemLocations(){
    String itemLocationPath = "/Resources/Items/itemLocations.txt";
    File dir = new File("/Resources/Items");
    dir.mkdirs();  // guarantees the directory hierarchy will be created if needed.

    try (BufferedWriter bw = new BufferedWriter(new FileWriter(itemLocationPath))){
        bw.write("test");
   } catch (IOException e) {
        e.printStackTrace();
    }
}
但是请注意,
/Resources/Items/itemLocations.txt
位于文件系统上,它不是
资源


请记住,文件系统上的“资源”和“文件”之间有很大的区别。资源通常包含在
jar
war
ear
文件中。它们作为资源可读,但绝对不可写。而文件系统中的文件可以是可读写的。似乎您正在尝试写入资源


Oracle的这一点可以帮助您更好地理解文件和资源之间的区别。

FileInputStream类直接与底层文件系统一起工作。如果有问题的文件不在那里,它将无法打开它


getResourceAsStream()
方法的工作原理不同。它尝试使用调用它的类的类加载器来定位和加载资源。例如,这使它能够查找嵌入到jar文件中的资源。

而不是使用
/
,您将需要\\getClass()。getResourceAsStream(testMapPath)导致差异。在这两种情况下,“根”是不同的。您无法如此轻松地写入jar文件。请记住,文件系统中的“资源”和“文件”之间有很大的区别。资源通常包含在jar、war和ear文件中。它们作为资源可读,但绝对不可写。而文件系统中的文件是可读写的。似乎您正试图写入资源。使用Class.getResource()使用类加载器而不是文件IO来读取“文件”。路径是来自类路径根之一的路径(即,它是包路径)。classloader通常从应用程序的jar文件内部加载资源,而不是从文件系统上的任意位置加载资源。这些资源是只读的。文件IO完全不同。它需要文件系统上的路径。您首先需要将应用程序视为部署在用户计算机上的应用程序,而不是开发人员计算机上的开发项目。用户将在哪里找到这些文件?您将如何安装该应用程序?这些文件是应用程序的一部分,因此是只读的,还是用户创建和修改的数据?
public void saveItemLocations(){
    String itemLocationPath = "/Resources/Items/itemLocations.txt";
    File dir = new File("/Resources/Items");
    dir.mkdirs();  // guarantees the directory hierarchy will be created if needed.

    try (BufferedWriter bw = new BufferedWriter(new FileWriter(itemLocationPath))){
        bw.write("test");
   } catch (IOException e) {
        e.printStackTrace();
    }
}