简体   繁体   中英

FileNotFoundException while reading and printing a ppm file

I want to write a function that receives a name of a PPM file and returns an array containing the image's data.

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;
    }
}

This is the code I wrote but I get the error:

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.<init>(FileInputStream.java:138) at java.io.FileInputStream.<init>(FileInputStream.java:93) at StdIn.setInput(StdIn.java:147) at read.read(read.java:10) at read.main(read.java:5)

Your path is wrong for your file. You can right click on the file and copy the path to get the accurate path.

Or you can use this code which will bring up a GUI window that will let you choose a file.

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM