txt files and java files in the same directory ,
File f2 = new File ("bb.txt");
write this , the path is wrong? Seemingly not found. Why ?
------ Solution ------------------------------------ --------
File f2 = new File("bb.txt"); This is in eclipse, the project's root directory, run the cmd current path .
------ Solution ---------------------------------------- ----
the src directory bb.txt drag on the line
------ Solution ---------------------- ----------------------
this relative path is relative System.getProperty ("user.dir") for the purposes of your output click user.dir content to know.
------ Solution ---------------------------------------- ----
If you do not provide specific disk path ( for example : d: \ \) this line , then the program will default in the course of the current project directory ( that is, more than on a src directory ) as a file directory ! create a file when you run the program code is correct, right- click the project will be able to find the refresh project files !
------ Solution ---------------------------------------- ----
should be placed TESTIO below , System.getProperty ("user.dir")
------ Solution -------------- ------------------------------
for the hair is it so ? Who knows ? Be able to analyze it a little more bottom ?
First java relative path is System.getProperty ("user.dir") as a reference , is your class compiled bin ( relative to the IDE) path , but in the bin directory of your bb.txt under the test / io / TestTxt , of course, can not find it , you first need to understand that your src path is not your last class path and the class path and text files in a directory are not able to find, understand ?
------ Solution ---------------------------------------- ----
try this bar
InputStream is = this.getClass().getClassLoader()
.getResourceAsStream("bb.txt");
------ For reference only ----------------------------------- ----
if not in the same directory
------ For reference only -------------------------- -------------
txt file is not and java file in the same directory you ?
why this will not work ? Who explain ?
Is compiled class files in the same directory you ? Who knows ?
------ For reference only -------------------------------------- -
for the hair is it so ? Who knows ? Be able to analyze it a little more bottom ?
------ For reference only -------------------------------------- -
for the hair is it so ? Who knows ? Be able to analyze it a little more bottom ?
because the eclipse is project unit drops . TestIo this project which has bb.txt files?
------ For reference only -------------------------------------- -
should be placed under the project , and not with the same level of SRC .
没有评论:
发表评论