Java Read file

Source: Internet
Author: User

1  Packagecom;2 3 ImportJava.io.*;4 5 /**6 * Hello world!7  *8  */9  Public classReadFileTen { One      Public Static voidMain (string[] args) { AString fileName = "F:/aa.json"; -String result =ReadFile (fileName); - System.out.println (result); the     } -     //FileReader read characters, may appear garbled, FileInputStream read bytes, will not appear garbled -     Private Staticstring ReadFile (String fileName) { -StringBuilder SB =NewStringBuilder (); +         Try { -             //Create a FileReader object to read a stream of characters +FileReader FileReader =NewFileReader (fileName); A             //buffers The input of the specified file atBufferedReader BufferedReader =NewBufferedReader (filereader); -             //Create a FileWriter object to write a character stream -FileWriter FileWriter =NewFileWriter ("F:/response2.json"); -             //will buffer the output of the file -BufferedWriter BW =NewBufferedWriter (fileWriter); - String Eachline; in             //read one line at a time -              while((Eachline=bufferedreader.readline ())! =NULL){ to sb.append (eachline); +                 //write a line, need to wrap, otherwise print on a line, need close, otherwise empty - Bw.write (eachline); the bw.newline (); *             } $ bw.close ();Panax Notoginseng  -}Catch(FileNotFoundException e) { the e.printstacktrace (); +}Catch(IOException e) { A e.printstacktrace (); the         } +         returnsb.tostring (); -     } $     Privatestring Readfilebybyte (String fileName) { $FileInputStream InputStream =NewFileInputStream () -         return -     } the}

Java Read 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.