Java gets file mode from different directory

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.