Summary of how to read a file using relative paths in Java, and getResourceAsStream ()

Source: Internet
Author: User

74450241

Read the file's wording, relative path

There are four ways to read test.txt in the current directory structure

    • Simple and rude file File = new file ("Src/test.txt")
    • Using the relative path of a class
    • class loader using the current thread
    • Read the file under Web Engineering using getrealpath () read

New File ("Src/test.txt"new file (Testrelativepath.  Class. GetResource ("/test.txt"new File (Thread.CurrentThread (). Getcontextclassloader (). GetResource ("test.txt"new File (Getservletcontext (). Getrealpath ("/web-inf/classes/test.txt"));

Let me introduce you here:

Simple and rude file File = new file ("Src/test.txt");
@Test/*** This method "" "Empty represents this Java project testsometechnology because the actual project does not have the SRC directory after packaging, so this method is not commonly used*/     Public  voidTESTMETHOD1 ()throwsioexception{File File=NewFile ("Src/test.txt"); BufferedReader BR=NewBufferedReader (Newfilereader (file)); String Len=NULL;  while((Len=br.readline ())! =NULL) {System.out.println (len); }    }
Using the relative path of a class

TestRelativePath.class.getResource ("/test.txt"). GetFile ()

@Test/***  use the relative path of the class  * This method "/" represents the bin. Everything under the SRC folder and the Resources folder will be loaded under the bin because these two files are configured for source */     Public  voidTESTMETHOD2 ()throwsioexception{File File=NewFile (Testrelativepath.class. GetResource ("/test.txt"). GetFile ()); BufferedReader BR=NewBufferedReader (Newfilereader (file)); String Len=NULL;  while((Len=br.readline ())! =NULL) {System.out.println (len); }    }
class loader using the current thread

Thread.CurrentThread (). Getcontextclassloader (). GetResource ("Test.txt"). GetFile ()

@Test/*** This is the class loader through the current thread * This method "" Empty represents the bin . So just fill in the test, the contents of the folder will be loaded under the bin, because these two files are configured in order to source*/     Public  voidTESTMETHOD3 ()throwsioexception{File File=NewFile (Thread.CurrentThread (). Getcontextclassloader (). GetResource ("Test.txt"). GetFile ()); BufferedReader BR=NewBufferedReader (Newfilereader (file)); String Len=NULL; if((Len=br.readline ())! =NULL) {System.out.println (len); }    }
Read under Web EngineeringThe file use Getrealpath ()Read

The directory is as follows:

Read index.jsp

@WebServlet (name = "Testservlet", Urlpatterns = "/testservlet") Public classTestservletextendsHttpServlet {protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {doget (request,response); }    /*** Web project root directory is webRoot, use "/" for WebRoot webRoot below index.jsp file*/    protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, ioexception {file file=NewFile (Getservletcontext (). Getrealpath ("/index.jsp")); BufferedReader BR=NewBufferedReader (Newfilereader (file)); String Len=NULL;  while((Len=br.readline ())! =NULL) {System.out.println (len); }    }}

Read Test.txt file
But if you want to read Test.txt, we can use the way it is.

New File (Thread.CurrentThread (). Getcontextclassloader (). GetResource ("Test.txt"). GetFile ());

You can also use Getrealpath ()
However, because it is rooted in webroot, we need to read from classes : The classes of idea isequivalent to the classpath of Esclipse . Classes Description:

Code:

New File (Getservletcontext (). Getrealpath ("/web-inf/classes/test.txt");
getResourceAsStream () method detailed

getResourceAsStream () uses the same as the Getresouce () method, after the file is obtained with GetResource (), the new FileInputStream (file) and getResourceAsStream () has the same effect:

Two code effects like

New FileInputStream (new File (Thread.CurrentThread (). Getcontextclassloader ().  GetResource ("test.txt"). GetFile ())); InputStream inputStream2 = Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream(" Test.txt ");

Summary of how to read a file using relative paths in Java, and getResourceAsStream () (goto)

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.