ฉันต้องการจะรวมแฟ้มข้อความในโฟลเดอร์ทรัพยากรที่โหลแฟ้ม นี่คือขนาดเล็กที่สุดตัวอย่าง:
import java.io.File;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Main main = new Main();
main.readFile( "test.txt" );
}
public void readFile(String fileName){
File file = new File( getClass().getResource( fileName ).getPath() );
try{
Scanner scanner = new Scanner( file );
while ( scanner.hasNextLine() ){
System.out.println( scanner.nextLine() );
}
}catch (Exception e){
System.out.println( e );
}
}
}
นี่คือสิ่งที่อยู่ใน test.txt แฟ้ม:
hello
world
นี่คือวิธีของฉันโครงการดูเหมือนว่า:
ฉันสามารถวิ่งรหัสยเรียบร้อยแล้ว อย่างที่ฉันสร้างโหลคือตาม:
ฉันต้องไปแล้วเพิ่มโฟลเดอร์ทรัพยากรได้โดยการคลิก"+"สัญลักษณ์. อย่างไรก็ตามตอนที่ฉันจะโทรเรียกขวดโหลที่ถูกสร้างแฟ้ม,มันเป็นยังบ่น FileNotFoundException.
มีข้อเสนอแนะอะไรมั้ย?