Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

java - Netbeans - Reading a data file in src folder

I have a scanner that's trying to read a file named info.data in the src folder.I get Exception in thread "main" java.io.FileNotFoundException: info.data (The system cannot find the file specified). What's the address I should put in the scanner?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If the input file is always part of your application (i.e. you also put this into the .jar file later) you should use getResourceAsStream() in order to read its contents.

InputStream in = getClass().getResourceAsStream(filename);
Scanner scanner = new Scanner(in);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...