코드 :는 자바에서 FileNotFoundException이 문제를 해결 할 수 없습니다
import java.io.*;
import java.util.Scanner;
public class WriteCSV {
public static void main(String[] args) {
String inputFilename = "coords.txt";
String outputFilename = changeFileExtToCsv(inputFilename);
// Open files
PrintWriter output = openOutput(outputFilename);
Scanner input = openInput(inputFilename);
String line;
while (input.hasNextLine())
{
line = input.nextLine();
line.replace(' ', ',');
output.println(line);
}
input.close();
output.close();
}
/**
* Changes file extension to ".csv"
* @param filename
* @return new filename.extension
*/
public static String changeFileExtToCsv(String filename) {
return filename.substring(0,filename.lastIndexOf('.')) + ".csv";
}
/**
* Open input for reading
* @param filename
* @return a Scanner object
*/
public static Scanner openInput(String filename) {
Scanner in = null;
try {
File infile = new File(filename);
in = new Scanner(infile);
} catch (FileNotFoundException e) {
e.printStackTrace();
//System.out.println(filename + " could not be found");
System.exit(0);
}
return in;
}
/**
* Open output for writing
* @param filename
* @return a PrintWriter object
*/
public static PrintWriter openOutput(String filename) {
PrintWriter output = null;
try {
File outFile = new File(filename);
output = new PrintWriter(outFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
//System.out.println(filename + " could not be found");
System.exit(0);
}
return output;
}
}
오류 메시지 :
java.io.FileNotFoundException: coords.txt (The system cannot find the file specified)
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.util.Scanner.<init>(Scanner.java:611)
at WriteCSV.openInput(WriteCSV.java:44)
at WriteCSV.main(WriteCSV.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Process finished with exit code 0
라인 (44)은 File infile = new File(filename);
및 coords.txt (그리고 .CSV)가 같은 디렉토리 된 .java 파일에 있습니다 안으로있다.
File 객체에 파일 이름과 확장자 만 지정하면 .java 파일이있는 동일한 디렉토리에서 해당 파일을 찾지 않아야합니까?
전체 경로를 입력하면 프로그램이 제대로 작동합니다 (coords.txt 파일이있는 한).
또한 등급 지정 프로그램에 제출하면 (작동 방식이 숨겨 짐) "프로그램 시간 초과"라고 표시됩니다. 그리고 그게 무슨 뜻인지 모르겠습니다.
내 나쁜. 게시물의 편집을 참조하십시오. –