[Java IO] Basics: Reading files

Source: Internet
Author: User

1. Read the file path string 1.1 using ClassLoaderclass method to get the file path
System. out. println(This. GetClass(). getClassLoader(). GetResource("."). GetPath());//Get the project's class file pathSystem. out. println(This. GetClass(). getClassLoader(). GetResource(""). GetPath());//Get the project's class file pathSystem. out. println(This. GetClass(). GetResource("/"). GetPath());//Get the project's class file pathSystem. out. println(Thread. CurrentThread(). Getcontextclassloader(). GetResource(""). GetPath());System. out. println(Thread. CurrentThread(). Getcontextclassloader(). GetResource("."). GetPath());System. out. println(This. GetClass(). GetResource(""). GetPath())//★ Get the package path where the current file is locatedSystem. out. println(This. GetClass(). GetResource("."). GetPath())//★ Get the package path where the current file is located

Results:
/d:/workspace/dgcerttest/build/classes/
/d:/workspace/dgcerttest/build/classes/
/d:/workspace/dgcerttest/build/classes/
/d:/workspace/dgcerttest/build/classes/
/d:/workspace/dgcerttest/build/classes/
/d:/workspace/dgcerttest/build/classes/com/test/
/d:/workspace/dgcerttest/build/classes/com/test/

1.2 By getCanonicalPath()And getAbsolutePath()Get file path
new File("");// 设定为当前文件夹 System.out.println(directory.getCanonicalPath());// 获取标准的路径 System.out.println(directory.getAbsolutePath());// 获取绝对路径 //结果:D:\workspace\java_basenew File("/");// 设定为当前文件夹 System.out.println(panFu.getCanonicalPath());//得到所在磁盘System.out.println(directory.getAbsolutePath());// 获取绝对路径 //结果:D:\
2. Get IO stream via file path 2.1 FileInputStream: Reads a file at a specific location on the local disk

String path= "D:\HwTemp\dongguan_test\test.xml";
FileInputStream fis=new FileInputStream (path);
But in this way in the change of environmental testing and re-copied to someone else's disk, not put in the project, with the project relative path to access convenience.
This works in a different environment can be directly run, that isString ->FileInputStream

//project path + relative path to project (with) String path = System.getproperty ("User.dir" ) + "\\  src\\  file.txt";//Will all the above-obtained path "\ " converted to" \\  "path = Path.replaceall (" \\  \\ ", "\\  \\  \\  \\ "); SYSTEM.OUT.PRINTLN (path); FileInputStream fis = new FileInputStream (path),//d:\\  Workspace\\  java_base\\  src\\  file.txt  

Note: The string \\ represents a " \ " but the first parameter in the Relaceall is a regular expression, which in regular \\\\ notation represents a " \ "
But System.getProperty("user.dir") if the Java code is placed in a JSP, it will be the IDE's installation path. So the code cannot be used in a JSP page

2.2 InputStream: Method get****AsStreamRead the file in this project:

The class method returns InputStream
Reference documentation: Java reads files in the project

    • Method One: (not validated) place the XML file in the Web-inf directory, and then
is"/WEB-INF/xmlfile.xml" );
    • Method Two: Put the XML file in the/web-inf/classes directory or classpath jar package, you can use ClassLoader static method Getsystemresourceasstream (String s) read;
String s_xmlpath="com/spf/web/ext/hotspot/hotspotxml/hotspot.xml"in=ClassLoader.getSystemResourceAsStream(s_xmlpath);
    • Method Three: XML at random under a package path
String s_xmlpath="com/spf/web/ext/hotspot/hotspotxml/hotspot.xml";ClassLoader classLoader=HotspotXmlParser.class.getClassLoader();in=classLoader.getResourceAsStream(s_xmlpath);
3. Annex 3.1, annex 1: Obtaining system parameters via System.getproperties ()
Properties Props=system. GetProperties();//System PropertiesSystem. out. println("Java version of the runtime environment:"+props. GetProperty("Java.version"));System. out. println("Java Operating Environment vendor:"+props. GetProperty("Java.vendor"));System. out. println("Java Vendor's URL:"+props. GetProperty("Java.vendor.url"));System. out. println("Installation path for Java:"+props. GetProperty("Java.home"));System. out. println("Java version of the virtual machine specification:"+props. GetProperty("Java.vm.specification.version"));System. out. println("Java's virtual Machine specification provider:"+props. GetProperty("Java.vm.specification.vendor"));System. out. println("Java's virtual Machine specification Name:"+props. GetProperty("Java.vm.specification.name"));System. out. println("virtual machine implementation version of Java:"+props. GetProperty("Java.vm.version"));System. out. println("Java Virtual machine Implementation provider:"+props. GetProperty("Java.vm.vendor"));System. out. println("Java Virtual machine implementation name:"+props. GetProperty("Java.vm.name"));System. out. println("Java Runtime Environment Specification version:"+props. GetProperty("Java.specification.version"));System. out. println("Java Runtime Environment Specification vendor:"+props. GetProperty("Java.specification.vender"));System. out. println("Java Runtime Environment Specification name:"+props. GetProperty("Java.specification.name"));System. out. println("Java class format version number:"+props. GetProperty("Java.class.version"));System. out. println("Classpath for Java:"+props. GetProperty("Java.class.path"));System. out. println("List of paths searched when loading library:"+props. GetProperty("Java.library.path"));System. out. println("Default temporary file path:"+props. GetProperty("Java.io.tmpdir"));System. out. println("path to one or more extended directories:"+props. GetProperty("Java.ext.dirs"));System. out. println("Name of the operating system:"+props. GetProperty("Os.name"));System. out. println("Architecture of the Operating system:"+props. GetProperty("Os.arch"));System. out. println("version of the operating system:"+props. GetProperty("Os.version"));System. out. println("file delimiter:"+props. GetProperty("File.separator")); In UNIX systems is "/"System. out. println("path delimiter:"+props. GetProperty("Path.separator")); In a Unix system is ":"System. out. println("line delimiter:"+props. GetProperty("Line.separator")); In UNIX systems is "/n"System. out. println("User's account name:"+props. GetProperty("User.Name"));System. out. println("User's home directory:"+props. GetProperty("User.home"));System. out. println("User's current working directory:"+props. GetProperty("User.dir"));
3.2 Annex 2: The most basic path conversion
 @Test public  void  Span class= "Hljs-title" >test_getxmlpath () {String path = Thread.CurrentThread (). Getcontextclassloader (). GetResource (). toString (); Path = Path.replace ( "/' ,  ' \ \ \ ' ); ///replace \  Path = path.replace (, ); //Remove file:  Path = path.replace ( "classes\\" , Span class= "hljs-string" "" "); //remove class\  path = path.substring (1 ); //remove the first \, such as \d:\javaweb ...  Path + =  "web. xml" ; System. out . println (path);} 
@Testpublicvoidtest_getXmlPath() {    String path = System.getProperty("user.dir")+"\\src\\file.txt";    System.out.println();    //★将所有的上面得到的路径中"\"转换成"\\"    path = path.replaceAll("\\\\","\\\\\\\\");    System.out.println(path);}

[Java IO] Basics: Reading files

Related Article

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.