Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.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 FileInputStream的FileNotFoundException,即使.getResource(filename)找到它_Java_Filenotfoundexception_Fileinputstream_Getresource - Fatal编程技术网

Java FileInputStream的FileNotFoundException,即使.getResource(filename)找到它

Java FileInputStream的FileNotFoundException,即使.getResource(filename)找到它,java,filenotfoundexception,fileinputstream,getresource,Java,Filenotfoundexception,Fileinputstream,Getresource,这是我在stackoverflow上问的第一个问题,因此,如果您注意到我的问题中有不愉快/不好/不适当的地方,请友好地向我指出 我试着用java做我的家庭作业,因为我厌倦了C++,而且我已经用Python做了一些事情。然而,我在读取二进制文件时遇到了问题(按顺序,二进制文件应该包含一个双精度数字和两个浮点数) 具体来说:.getResource(filename)查找文件,但当我打开文件输入流(path)(在公共静态整数Leggere(Dati[]dato,String nomefile)中)时

这是我在stackoverflow上问的第一个问题,因此,如果您注意到我的问题中有不愉快/不好/不适当的地方,请友好地向我指出

我试着用java做我的家庭作业,因为我厌倦了C++,而且我已经用Python做了一些事情。然而,我在读取二进制文件时遇到了问题(按顺序,二进制文件应该包含一个双精度数字和两个浮点数)

具体来说:
.getResource(filename)
查找文件,但当我打开
文件输入流(path)
(在
公共静态整数Leggere(Dati[]dato,String nomefile)
中)时,它抛出
文件NotFoundException

这是我的代码:

import java.util.*;
import java.io.*;

public class Main{
    public static void main(String[] args) {
        Dati [] data  = new Dati[23500];
        int contatore = 0;
        for(; contatore < 23500; contatore++){
            data[contatore] = new Dati(0, 0, 0);
        }
        contatore = 0;

        String path = Dati.class.getClassLoader().getResource("valori.bin").getPath().toString();
        contatore = Leggere(data, path);
        Acquisire(data, contatore);
        Scrivere(data, "risultati.txt", contatore);
    }

    public static Integer Leggere(Dati [] dato, String nomefile){
        int j = 0;
        try{
            DataInputStream in = new DataInputStream(new FileInputStream(nomefile));
            while(in.available() > 0){
                dato[j].dato1 = in.readDouble();
                dato[j].dato2 = in.readFloat();
                dato[j].dato3 = in.readFloat();
                j++;
            }
            in.close();
        }
        catch(IOException e){
            System.out.println("Problemi nell'apertura del file");
            System.out.println(nomefile);
            System.exit(0);
        }

        return j;
    }

    public static void Scrivere(Dati [] dato, String nomefile, int count){
        PrintWriter output;
        try {
            output = new PrintWriter(nomefile);

            Integer j = 0;
            while(j < count){
                output.println(dato[j]);
                j++;
            }

            output.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


    }

    public static void Acquisire(Dati [] dato, int count){
        Scanner cinput = new Scanner(System.in);
        double c = 0.0;
        int j = 0;

        System.out.println("Inserisci un fattore di conversione: ");
        while(c == 0.0){
            c = cinput.nextDouble();
        }

        while(j < count){
            dato[j].dato1 *= c;
            dato[j].dato2 *= c;
            dato[j].dato3 *= c;
        }

        cinput.close();
    }
}
我知道这是一个
FileNotFoundException
,因为它在调试模式下这么说

您将如何完成这段代码?你知道问题出在哪里吗?你能举一个例子来解决这个问题吗,或者举一个替代方法的例子来解决这个问题吗

问题的解决方案

我写这篇文章是为了让有类似问题的人找到他们的解决方案

“故障”代码

发生的事情是,
.getResource(“valori.bin”)
设法找到了文件(它的路径是
.getPath()
),但当我试图打开
文件输入流(路径)
时,我得到了一个
文件NotFoundException

工作代码


这样就行了。通过这样做,我不需要担心路径名,因为
.getResourceAsStream()
按原样返回可用流。我不知道前一个代码为什么不起作用,但是启发式告诉我不要太担心,就这样吧

您可能需要查看类加载器中的
getResourceAsStream
方法。它为您提供了一个输入流,您可以直接插入,而不必处理完整路径和此类问题。

在我看来,其中一个API可能使用相对路径,而另一个需要绝对路径,您可能会弄错该路径。@Tim Biegeleisen哦,我明白了。好的,FileInputStream()获得了
/C:/Users/Sebastian/Desktop/Archivio/scoola/5C%20a.s.%202016-2017/Compiti%20Estate%202016/Informatica/03%20-%20Conversione/bin/valori.bin
。这是一个绝对路径,不是吗?在所有URL编码的情况下,文件名真的是这样吗?@Robert这是什么
String path=Dati.class.getClassLoader().getResource(“valori.bin”).getPath().toString()会提供给我,但这是URL编码的,在某些情况下可能有意义。但是,文件系统不处理URL编码,而是按原样处理名称。使用OK还原URL编码,谢谢。我在main中尝试过这样做:
InputStream=Dati.class.getClassLoader().getResourceAsStream(“valori.bin”)
并更改了函数Leggere()中的所有内容以使其正常工作。现在它似乎成功地打开了它,但在从文件中读取了几个字节后,我出现了EOF异常,但我想这是一个完全不同的问题来处理。是时候修复在该文件上实际以二进制编写的Python程序了。可能是因为64位Python浮点,我有Java 32位浮点错误。
Problemi nell'apertura del file
/C:/Users/Sebastian/Desktop/Archivio/Scuola/5C%20a.s.%202016-2017/Compiti%20Estate%202016/Informatica/03%20-%20Conversione/bin/valori.bin
String path = Dati.class.getClassLoader().getResource("valori.bin").getPath().toString();
DataInputStream in = new DataInputStream(new FileInputStream(path));
InputStream stream = Dati.class.getClassLoader().getResourceAsStream("valori.bin");
DataInputStream in = new DataInputStream(stream);