Java Read Stream Summary

Source: Internet
Author: User

Java reads a lot, FileInputStream, InputStreamReader, BufferedReader, write it down.

 Public  class readfromfile {    /** * Read files in bytes, often used to read binary files, slices, sounds, images, and other files. */     Public Static void readfilebybytes(String fileName) {File File =NewFile (FileName); InputStream in =NULL;Try{System.out.println ("reads the contents of the file in bytes, one byte at a time:");//read one byte at a timein =NewFileInputStream (file);intTempbyte; while((Tempbyte = In.read ())! =-1) {System.out.write (tempbyte);        } in.close (); }Catch(IOException e) {E.printstacktrace ();return; }Try{System.out.println ("read the contents of the file in bytes, read multiple bytes at a time:");//Read multiple bytes at a time            byte[] Tempbytes =New byte[ -];intByteread =0; in =NewFileInputStream (FileName); Readfromfile.showavailablebytes (in);//Read into multiple bytes into a byte array, Byteread is the number of bytes read in at one time             while((Byteread = In.read (tempbytes))! =-1) {System.out.write (tempbytes,0, Byteread); }        }Catch(Exception E1)        {E1.printstacktrace (); }finally{if(In! =NULL) {Try{In.close (); }Catch(IOException E1) {                }            }        }    }/** * Read files in characters, commonly used for reading text, numbers and other types of files * /     Public Static void Readfilebychars(String fileName) {File File =NewFile (FileName); Reader reader =NULL;Try{System.out.println ("reads the contents of the file in characters, one byte at a time:");//read one character at a timeReader =NewInputStreamReader (NewFileInputStream (file));intTempchar; while((Tempchar = Reader.read ())! =-1) {//For Windows, \ r \ n These two characters are together to represent a newline.                 //But if the two characters are displayed separately, the lines are changed two times.                 //So, block \ r, or block \ n. Otherwise, there will be a lot more empty rows.                 if(((Char) tempchar)! =' \ r ') {System.out.print (Char) Tempchar);        }} reader.close (); }Catch(Exception e)        {E.printstacktrace (); }Try{System.out.println ("read the contents of the file in characters, read multiple bytes at a time:");//Read more than one character at a time            Char[] Tempchars =New Char[ -];intCharread =0; Reader =NewInputStreamReader (NewFileInputStream (FileName));//Read multiple characters into a character array, charread to read characters at a time             while((Charread = Reader.read (tempchars))! =-1) {//also shield off \ r not show                if(Charread = = tempchars.length) && (Tempchars[tempchars.length-1] !=' \ r ') {System.out.print (tempchars); }Else{ for(inti =0; i < Charread; i++) {if(Tempchars[i] = =' \ r ') {Continue; }Else{System.out.print (tempchars[i]); }                    }                }            }        }Catch(Exception E1)        {E1.printstacktrace (); }finally{if(Reader! =NULL) {Try{Reader.close (); }Catch(IOException E1) {                }            }        }    }/** * Read files in behavioral units, often used to read line-oriented format files */     Public Static void Readfilebylines(String fileName) {File File =NewFile (FileName); BufferedReader reader =NULL;Try{System.out.println ("read the contents of the file in the behavior unit and read the entire line at a time:"); Reader =NewBufferedReader (NewFileReader (file)); String tempstring =NULL;intline =1;//read one line at a time until the end of the file is read in null             while((tempstring = Reader.readline ())! =NULL) {//Show line numberSystem.out.println ("line"+ Line +": "+ tempstring);            line++;        } reader.close (); }Catch(IOException e)        {E.printstacktrace (); }finally{if(Reader! =NULL) {Try{Reader.close (); }Catch(IOException E1) {                }            }        }    }/** * Random reading of file contents * *     Public Static void readfilebyrandomaccess(String fileName) {Randomaccessfile Randomfile =NULL;Try{System.out.println ("Random reading of a file content:");//Open a random Access file stream as read-onlyRandomfile =NewRandomaccessfile (FileName,"R");//File length, number of bytes            LongFilelength = Randomfile.length ();//Read the starting position of the file            intBeginindex = (Filelength >4) ?4:0;//Move the starting position of the read file to the Beginindex location. Randomfile.seek (Beginindex);byte[] bytes =New byte[Ten];intByteread =0;//Read 10 bytes at a time and read the remaining bytes if the contents of the file are less than 10 bytes.             //Assign the number of bytes read to Byteread             while((byteread = randomfile.read (bytes))! =-1) {System.out.write (bytes,0, Byteread); }        }Catch(IOException e)        {E.printstacktrace (); }finally{if(Randomfile! =NULL) {Try{Randomfile.close (); }Catch(IOException E1) {                }            }        }    }/** * Shows the number of bytes left in the input stream */    Private Static void showavailablebytes(InputStream in) {Try{System.out.println ("The number of bytes in the current byte input stream is:"+ in.available ()); }Catch(IOException e)        {E.printstacktrace (); }    } Public Static void Main(string[] args) {String FileName ="C:/temp/newtemp.txt";        Readfromfile.readfilebybytes (FileName);        Readfromfile.readfilebychars (FileName);        Readfromfile.readfilebylines (FileName);    Readfromfile.readfilebyrandomaccess (FileName); }}

Java Read Stream Summary

Related Article

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.