java-reading data from a file

Source: Internet
Author: User

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

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.