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