1, FileInputStream ()
//constructs a byte input stream object with the parameter file nameFileInputStream fin =NewFileInputStream ("Message"); System.out.println ("Input stream can be read in bytes:" +fin.available ()); //set up byte array B, the data read from the input stream is temporarily stored in B byteB[] =New byte[Fin.available ()]; //reading data from an input streamFin.read (b); String STR5=NewString (b); System.out.println (STR5); //close the input stream, the operation of the file, be sure to close the input and output stream at the endFin.close ();
2, Randomaccessfile ()
String str6= ""; ArrayList<String> strs=new arraylist<string>(); Randomaccessfile file=new randomaccessfile ("message", "R"); = file.readline (); while (str6!=null) { strs.add (STR6); = file.readline (); } for (int j=0;j<strs.size (); j + +) { System.out.println (Strs.get (j)); } File.close ();
3. File
File file2=New file ("message"); if (file2.exists () &&file2.isfile ()) { InputStream is=newbyte while(Is.read (buf)! = -1// per read print System.out.println (new String (BUF));}}
4. Avoid garbled characters
File File3 =New File ("message");if (file3.exists () &&File3.isfile ()) {Try {InputStream is = newnew InputStreamReader (IS, "utf-8"); char[] cbuf = new char [Is.available ()]; // StringBuffer Sb2=new stringbuffer () 2 while (Reader.read (cbuf)! = -1) {Sb2.append (CBUF);} System.out.println (Sb2.tostring ()); } catch (Exception e) {}} /span>
5, BufferedReader
File File =NewFile ("Message"); Try{InputStream is=Newfileinputstream (file); if(File.exists () &&File.isfile ()) {BufferedReader br=NewBufferedReader (NewInputStreamReader (IS, "Utf-8")); StringBuffer SB2=NewStringBuffer (); String Line=NULL; while(line = Br.readline ())! =NULL) {sb2.append ( line+ "\ n"); } br.close (); System.out.println (Sb2.tostring ()); } } Catch(Exception e) {}
java-reading data from a file