"Spring" Gets the resource file + get body data from the InputStream object from File+

Source: Internet
Author: User

1. Get the resource file or get a text file, etc., can be obtained through the spring of the resource way to get 2. Only the file object can get Body Data 3. Body data is available only with InputStream
1  packagecom.sxd.test.test1;2 3 Importjava.io.BufferedReader;4 Importjava.io.File;5 Importjava.io.FileReader;6 Importjava.io.IOException;7 Importjava.io.InputStream;8 Importjava.nio.file.Files;9 Importjava.util.List;Ten  one Importorg.junit.Test; a Importorg.springframework.core.io.ClassPathResource; - Importorg.springframework.core.io.Resource; -  the  public classGetResource { -      -     /** - * One way to get a resource file-----using the resource class in the Spring rack package +      * @throwsIOException -      */ + @Test a      public voidGetresouce ()throwsioexception{ atResource Resource =NewClasspathresource ("beanfactorytest.xml"); -InputStream in =Resource.getinputstream (); -String FileName =resource.getfilename (); -String Description =resource.getdescription (); -         LongContentLength =resource.contentlength (); -File File =resource.getfile (); in          -          toSystem.out.println ("file name:" +fileName); +System.out.println ("description:" +description); -System.out.println ("body length:" +contentlength); the          *          $         /**Panax Notoginseng * A File object can read the data to the Body-----method 1 -          */ thelist<string> list =files.readalllines (file.topath ()); +          for(String String:list) { a System.out.println (string); the         } +          -          $System.out.println ("------------------------------------------------------------------------------------------- ------ "); $         /** - * A File object can read the body Data----method 2 -          */ theBufferedReader br =NewBufferedReader (NewFileReader (file)); -String str =NULL;Wuyi          while((str=br.readline ())! =NULL){ the System.out.println (str); -         } wu          -System.out.println ("------------------------------------------------------------------------------------------- -------"); about         /** $ * have InputStream can read the body data -          */ -         intContentlen =in.available (); -         byte[] st =New byte[contentlen]; a In.read (st); +System.out.println (NewString (st)); the          -     } $}
View Code

"Spring" Gets the resource file + get body data from the InputStream object from File+

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.