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+