Method one
/**
Read files in bytes, which are often used to read binary files, such as pictures, sounds, images and other files.
Of course, you can read strings.
*/
/* It seems that the network environment is quite complicated, and the characters transmitted every time are fixed-length. In this way? */public String readString1(){ try { //FileInputStream is used to read raw byte streams such as image data. To read a character stream, consider using FileReader. FileInputStream inStream=this.openFileInput(FILE_NAME); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer=new byte[1024]; int length=-1; while( (length = inStream.read(buffer) != -1) { bos.write(buffer,0,length); // The explanation of the .write method SDK is Writes count bytes from the byte array buffer starting at offset index to this stream. // The content still exists after the stream is closed} bos.close(); inStream.close(); return bos.toString(); // Why not take out the buffer size at once? Why do I still write it to bos? Isn't it better to return new(buffer,"UTF-8")? // Return new String(bos.toByteArray(),"UTF-8"); }}Method 2
// Someone said that it is better to read strings from FileReader, so use FileReader
// Is it a bit inefficient to read one at a time? private static String readString2(){ StringBuffer str=new StringBuffer(""); File file=new File(FILE_IN); try { FileReader fr=new FileReader(file); int ch = 0; while((ch = fr.read())!=-1 ) { System.out.print((char)ch+" "); } fr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("File reader error"); } return str.toString();}Method Three
/Read string by byte/
/* I personally feel that the best way is to read bytes (read at one time) and read bytes. Isn't it better to read bytes after reading and transcoding once*/
private static String readString3()
{
String str="";File file=new File(FILE_IN);try { FileInputStream in=new FileInputStream(file); // size is the length of the string, read int size=in.available(); byte[] buffer=new byte[size]; in.read(buffer); in.close(); str=new String(buffer,"GB2312");} catch (IOException e) { // TODO Auto-generated catch block return null; e.printStackTrace();}return str;}
Method 4
/InputStreamReader+BufferedReader reads strings, InputStreamReader class is a bridge from byte stream to character stream/
/* Read by line is a good way to read the formatted data to be processed*/private static String readString4(){ int len=0; StringBuffer str=new StringBuffer(""); File file=new File(FILE_IN); try { FileInputStream is=new FileInputStream(file); InputStreamReader isr= new InputStreamReader(is); BufferedReader in= new BufferedReader(isr); String line=null; while( (line=in.readLine())!=null ) { if(len != 0) // Handle line breaks problem { str.append("/r/n"+line); } else { str.append(line); } len++; } in.close(); is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return str.toString();}The road must be taken step by step. Remember the path you have walked and never make the same mistakes again. This is true growth! Welcome to give advice and exchanges.
The above method of reading files into strings in Java is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.