【问题标题】:FileNotFoundException while reading and printing a ppm file读取和打印 ppm 文件时出现 FileNotFoundException
【发布时间】:2015-11-29 20:52:39
【问题描述】:

我想编写一个函数,它接收 PPM 文件的名称并返回一个包含图像数据的数组。

import.java.io

public class read {

    public static void main(String[] args) {
        int[][][] pic = read("test.ppm");
        StdOut.println(pic);
    }

    public static int[][][] read (String ppmfile){
        StdIn.setInput(ppmfile);
        int n = StdIn.readInt();
        int[][][] data = new int[n][n][n];
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data.length; j++) {
                for (int k = 0; k < data.length; k++) {
                    data[i][j][k] = StdIn.readInt();
                }
            }
        }
        return data;
    }
}

这是我写的代码,但我得到了错误:

java.io.FileNotFoundException: test.ppm (No such file or directory) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.&lt;init&gt;(FileInputStream.java:138) at java.io.FileInputStream.&lt;init&gt;(FileInputStream.java:93) at StdIn.setInput(StdIn.java:147) at read.read(read.java:10) at read.main(read.java:5)

【问题讨论】:

  • 您需要提供正确的 test.ppm 路径
  • 文件在哪里?
  • 找不到文件。哪一部分你不明白?文件名错误或不在您所说的位置。与代码无关。
  • 该文件位于与读取的代码完全相同的位置,这就是我不理解错误的原因。

标签: java filenotfoundexception


【解决方案1】:

您的文件路径错误。可以在文件上右键复制路径,得到准确的路径。

或者你可以使用这个代码,它会打开一个 GUI 窗口,让你选择一个文件。

JOptionPane.showMessageDialog(null, "Please choose a file");        
JFileChooser input = new JFileChooser();
int a = input.showOpenDialog(null);
String file = "";

if (a == JFileChooser.APPROVE_OPTION) {
    File selectedFile = input.getSelectedFile();
    file = selectedFile.getPath();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-17
    • 2018-08-23
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多