Java file relative path

xiaoxiao2021-03-06  49

Several times, open the file in Java with a relative path, you can't find the error of the file, change it to the absolute path. Prepare for check today, how to write relative paths.

Test code: public class testfilepath {? Public static void main (string [] args) {??? testfilepath testfilepath1 = new testfilepath (); ??? file file = new file ("test.txt"); ??? system .out.println (file.getabsolutepath ()) ;;?}}

Under the command line, perform the Class file with the java command, execute under different paths, the result is different. As follows: Execute method 1: f: / 2004 / projects / jbproject / myserver / classes> java test_bak.testfilepathf: /2004/projects/jbproject/myserver/classes/test.txt Execute 2: f: / 2004 / Projects / JBProject / myServer> java -classpath classes test_bak.TestFilePathF: /2004/projects/jbproject/myServer/test.txt execution method 3: F: / 2004 / projects / jbproject> java -classpath myServer / classes test_bak.TestFilePathF: / 2004 / projects /jbproject/test.txt

It can be seen that the starting point of the relative directory is not the path where the Class file is located, but the position of the Java interpreter.

However, when executed under JBuilder, execute the default command by jbuilder, h: /jbuilder9/jdk1.4/bin/javaw -classpath ..... test_bak.testfilepath execution result is F: / 2004 / Projects / JBProject / MyServer / Test.txt, if the Java application operation file is developed under JBuilder, the starting point of the relative directory is generally the last layer of the Classes /, which is the application directory, written, execute, execute with -classpath classes, It can be obtained as developing as developing.

?

转载请注明原文地址:https://www.9cbs.com/read-115464.html

New Post(0)