이 질문은 stackoverflow에 대한 첫 번째 질문이므로 불쾌한 점이나 부정적/부적절한 점이 제 질문에있는 경우 친절하게 지적하십시오.FileInputStream과 FileInputStream을 사용하여 FileNotFoundException을 발견했습니다.
저는 C++에 질려서 파이썬에서 이미 작업을했기 때문에 학교 숙제를 Java로 시도했습니다. 그러나 이진 파일 (두 개의 부동 소수점 숫자가 순서대로 포함되어 있어야 함)에서 읽는 데 문제가있었습니다.
특히 : .getResource(filename)
이 파일을 찾지 만 FileInputStream(path)
(public static Integer Leggere(Dati [] dato, String nomefile)
내부)을 열면 FileNotFoundException
이 표시됩니다.
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
있어 알고
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
: 두 번째는하여 getResource()
방법으로 발견 파일의 경로입니다.
이 코드를 어떻게 작성 했습니까? 문제가 뭔지 알아? 문제를 해결하기위한 예제 또는 이에 대처할 수있는 대체 방법의 예를 게시 할 수 있습니까? 비슷한 문제를 가진 사람들이 자신의 해결책을 찾을 수 있도록 문제
TO
솔루션은 이걸 쓰고 있어요.
"오작동"코드
String path = Dati.class.getClassLoader().getResource("valori.bin").getPath().toString();
DataInputStream in = new DataInputStream(new FileInputStream(path));
무슨 일이 것은 .getResource("valori.bin")
은 (그리고 .getPath()
과 경로의) 파일을 찾을 수 관리,하지만 난 FileInputStream(path)
을 열려고 할 때, 나는 FileNotFoundException
을 얻었다이었다.
작동 코드
InputStream stream = Dati.class.getClassLoader().getResourceAsStream("valori.bin");
DataInputStream in = new DataInputStream(stream);
이 방법은 않습니다. 이렇게하면 .getResourceAsStream()
이 사용 가능한 스트림을있는 그대로 반환하기 때문에 경로 이름을 염려 할 필요가 없습니다. 나는 왜 이전 코드가 작동하지 않았는 지 정확히 알지 못하지만, 경험적 방법은 나에게 많은 것을 걱정하지 말라고 말했고, 그렇습니다!
내 머리 꼭대기에서 API 중 하나가 상대 경로를 사용하고 다른 하나는 절대 경로가 필요합니다.이 경로는 잘못된 경로 일 수 있습니다. –
@Tim Biegeleisen 아, 알겠습니다. 음, FileInputStream()은'/C:/Users/Sebastian/Desktop/Archivio/Scuola/5C%20a.s.%202016-2017/Compiti%20Estate%202016/Informatica/03%20-%20Conversione/bin/ '이됩니다. valori.bin'을 매개 변수로 사용합니다. 이것은 절대 경로입니다. 그렇지 않습니까? –
URL 인코딩이 모두 포함 된 파일 이름이 실제로 해당 파일 이름입니까? – Robert