Four sets of reading and writing programs

Source: Internet
Author: User

the file class is used to access the properties of files or directories

Flow: refers to a series of flowing characters, which is a channel for sending information in FIFO mode. Between the program and the data source is connected by a stream.

first set: byte stream read Write scheme

fileinputstream: byte stream read text file

  FileInputStream fis=New fileinputstream ("e:\\ read file. txt")        ; byte []bytes=newbyte[1024x768];         int data;          while ((Data=fis.read (bytes))!=-1)        {            string str=new string (bytes,0, data);            System.out.println (str);        }        Fis.close ();    }

fileoutputstream: byte stream write to hard disk

FileOutputStream fos=New fileoutputstream ("E:\\1.txt");        String Word= "College entrance examination is the watershed of life";         byte [] bytes = word.getbytes ();        Fos.write (bytes);        Fos.close ();        System.out.println ("Write succeeded!") );            }}

Second set: Character stream read Write scheme

FileReader: character Stream reads text

    FileReader fr=New filereader ("e:\\ read file. txt")    ; Char []chars=newchar[1024x768];     int data;      while ((Data=fr.read (chars))!=-1)    {                string str=new  String (chars);        System.out.println (str);}    }

FileWriter: character Stream writes text

FileWriter fw=New FileWriter ("E:\\2.txt");                Fw.write ("New June");                System.out.println ("Write succeeded!") );        Fw.close ();    }

third set: <bufferedreader, bufferedwriter> General and FileReader and FileWriter combined use

BufferedReader: Custom Cache size, read text 8,192 Char

    FileReader fr=New filereader ("e:\\ read file. txt");    BufferedReader br=new  BufferedReader (FR);    String line;      while ((Line=br.readline ()) =null)    {        System.out.println (line);    }    Br.close ();    Fr.close ();}

BufferedWriter: Writing text

   FileWriter fw=New FileWriter ("E:\\5.txt");        BufferedWriter BW=new  BufferedWriter (FW);        Bw.write ("ok!!" );                Bw.close ();        Fw.close ();                System.out.println ("Write Success!!!") );    }

Fourth set: can read binary (img image, etc.)

DataInputStream: loading the local img into memory

FileInputStream fis=NewFileInputStream ("E:\\5.txt"); FileOutputStream Fos=NewFileOutputStream ("D:\\55.txt"); DataInputStream Dis=NewDataInputStream (FIS); DataOutputStream dos=NULL; byte[]bytes=New byte[1024]; intdata;  while((Data=dis.read (bytes))!=-1) {dos=NewDataOutputStream (FOS);        Dos.write (bytes);        } dos.close ();        Dis.close ();        Fos.close ();                Fis.close (); System.out.println ("Copy succes!!!"); }

DataOutputStream: writes the In-memory binary data to a file on the hard disk

DataOutputStream out=NULL; DataInputStream Dis=NULL; Try {            //creating an input stream objectFileInputStream fis=NewFileInputStream ("c:\\ Fanning. jpg"); Dis=NewDataInputStream (FIS); //creating an output stream objectFileOutputStream outfile=NewFileOutputStream ("C:\\ Fanning little Beauty 33.jpg"); out=NewDataOutputStream (OutFile); inttemp=Dis.read ();  while(Temp!=-1) {out.write (temp); Temp=Dis.read (); } System.out.println ("Copy succeeded");            Fis.close ();        Outfile.close (); } Catch(Exception e) {System.out.println ("File does not exist"); }finally{            Try {                if(dis!=NULL) {dis.close (); }                if(out!=NULL) {out.close (); }            } Catch(Exception E2) {e2.printstacktrace (); }        }

Note: In Java, how are byte arrays and string strings converted?

1. String Turn byte[]

String str = "Hello";
byte[] Srtbyte = Str.getbytes ();

2, byte[] turn string

Byte[] Srtbyte;
String str = new string (srtbyte);
System.out.println (str);

Four sets of reading and writing programs

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.