Read file operations in Java

Source: Internet
Author: User

InputStream & Reader
    • InputStream (byte stream), The following is the hierarchy of inputstream:

    • Audioinputstream: audio Input Stream class, This method can:
      • Get an audio input stream from an external audio file, stream, or URL
      • Writing external files from an audio input stream
      • Convert the audio input stream to a different audio format
      • AudioSystemThe class includes AudioInputStream methods for many operand objects:
        • Getaudioinputstream (audioformat.encoding targetencoding, Audioinputstream Sourcestream)
        • Getaudioinputstream (audioformat targetformat, Audioinputstream Sourcestream)
        • getAudioInputStream(File file)
        • Getaudioinputstream (inputstream Stream)
        • Getaudioinputstream (url Url)
      • The file code for playing WAV format is as Follows:
 public classAudioinputstream { public Static voidplaywav () {Try{audioinputstream Stream= Audiosystem.getaudioinputstream (NewFile ("sourcefile/1.wav")); byte[] samples = Getsamples (stream);//convert audio to byte arrayInputStream in =NewBytearrayinputstream (samples);    Play (in,stream.getformat ()); //Play audio files}Catch(unsupportedaudiofileexception E) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(ioexception E) {//TODO auto-generated Catch blockE.printstacktrace (); }            }    Private Static byte[] getsamples (audioinputstream Stream) {intLength = (int) (stream.getframelength () *Stream.getformat (). getframesize ()); byte[] samples =New byte[length]; DataInputStream in=NewDataInputStream (stream); Try{in.readfully (samples);        System.out.println (length); } Catch(ioexception E) {//TODO auto-generated Catch blockE.printstacktrace (); }        returnsamples; }    Private Static voidplay (inputstream stream, audioformat format) {intbuffersize = format.getframesize () * Math.Round (format.getsamplerate ()/10); byte[] buffer =New byte[buffersize]; Dataline.info Info=NewDataline.info (sourcedataline.class, format); Try{sourcedataline line=(sourcedataline) Audiosystem.getline (info);            Line.open (format, buffersize);            Line.start (); intNumbytesread = 0;  while(numbytesread! =-1) {numbytesread= Stream.read (buffer, 0, buffer.length); if(numbytesread! =-1) {line.write (buffer,0, numbytesread); //System.out.println (numbytesread);}} Line.drain ();        Line.close (); } Catch(lineunavailableexception E) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(ioexception E) {//TODO auto-generated Catch blockE.printstacktrace (); }    }
    • Bytearrayinputstream: the source of a stream is not necessarily a file, it can also be a piece of space in memory, such as a byte array. A bytearrayinputstream is a class that treats a byte array as a stream input source.
      • New Bytearrayinputstream (byte[] buf, int offset, int Length)
      • New Bytearrayinputstream (byte[] Buf)
    • Fileinputstream: gets the input information from the file system or terminal, the constructor is as Follows:
      • New FileInputStream (file File)
      • New FileInputStream (filedescriptor Fdobj)
      • New FileInputStream (String Name)
Try{fileinputstream fis=NewFileInputStream ("sourcefile/employee"); Try {        byte[] bytes =New byte[fis.available ()];        Fis.read (bytes);    Fis.close (); } Catch(ioexception E) {//TODO auto-generated Catch blockE.printstacktrace (); }} Catch(filenotfoundexception E) {//TODO auto-generated Catch blocke.printstacktrace ();}
    • Reader (character input Stream) with the following hierarchy:

  • Bufferedreader: character reads, default has a buffer of 8192 characters, when BufferedReader reads a text file, it tries to read the character data from the file and place the buffer, and then, if using the Read () method, reads from the buffer First. If the buffer data is not sufficient, it will be read from the file Again.
    • There are two construction methods, size indicates the buffer size is set, and default is 8192:
      • New BufferedReader (Reader In)
      • New BufferedReader (Reader in, int Size)
    • //system.in is a bit stream that, in order to convert to a character stream, can be converted to character using inputstreamreader,//then use BufferedReader to increase the buffering Function. BufferedReader br =NewBufferedReader (NewInputStreamReader (system.in)); String content=NULL;Try {     while(! (content = Br.readline ()). equals ("quit") {System.out.println (content); } br.close ();} Catch(ioexception E) {//TODO auto-generated Catch blocke.printstacktrace ();}
  • Chararrayreader: reading information from a character array
    • There are two methods of construction:
      • New CharArrayReader (char[] Buf)
      • New CharArrayReader (char[] buf, int offset, int Length)
    • See the CharArrayReader category for more Information.
  • Inputstreamreader: converts a byte stream to a character Stream. is a bridge of bytes flowing to a character stream. If you do not specify a character set encoding, the decoding process uses the default character encoding for the Platform.
    • Construction Method:
      • New InputStreamReader (inputstream In)
      • New InputStreamReader (inputstream in, Charset Cs)
      • New InputStreamReader (inputstream in, charsetdecoder DEC)
      • New InputStreamReader (inputstream in, String Charsetname)
    • See the InputStreamReader category for more Information.
  • Stringreader: read in string strings.
    • Construction method
      • New StringReader (String Str)
    • Related code
    • StringReader sr =NewStringReader ("dsfasdfasdfasd");Char[] chars =New Char[5];//reads 5 characters at a timeintLength = 0;Try {      while(length = Sr.read (chars))! =-1) {String Strread=NewString (chars, 0, length). touppercase ();       System.out.println (strread); }} Catch(ioexception E) {//TODO auto-generated Catch blocke.printstacktrace ();}

Read file operations in Java

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.