Demo
├─src
│└─com
│└─rgsc
│└─xml│├─xmlread.java│└─stu.xml 1. Error mode: String filepath= "Src/com/rgsc/xml/stu.xml";file F = newfile (FilePath);reading fails when published as a jar package, so do not use this method 2. Class byte code modeString FilePath = XmlRead.class.getResource ("/com/rgsc/xml/stu.xml"). GetFile ();//String FilePath = XmlRead.class.getResource ("Stu.xml"). GetFile (); Relative paths can be usedfile F = new file (FilePath);
Note: 1. The default is to find the package from the current class, and if you want to find it from the root, add "/" first. 2. In this way, the working directory must be in English and cannot have spaces 2. Class Loader modeString FilePath = XmlRead.class.getClassLoader (). GetResource ("Com/rgsc/xml/stu.xml"). GetFile (); File f =NewFile (FilePath);Note: 1. By default, from the Classpath root directory, you do not need to join the "/" in the first. 2. In this way, the working directory must be in English and cannot have spaces
Java gets file mode from different directory