Four sets of read and write file schemes

Source: Internet
Author: User
Tags ming

Four sets of read-write files scheme: instance

One: Reading a text file with a byte stream

Byte input stream exercise: Reading various data from a text file (letters, strings are supported)

Declaring a Stream object

Try {

FileInputStream fis=New fileinputstream ("C:\\ming.txt");

int data;

System. out. println ("Number of bytes readable:" +fis.available ());

System. out. println ("File contents:");

Looping through data

byte [] buffer=new byte[1024];

StringBuilder sb=New StringBuilder ();

while ((data=fis.read (buffer))!=-1) {

Byte[] Convert to String

String str=New string (Buffer,0,data, "gb2312");

Sb.append (str);

}

System. out. println (Sb.tostring ());

} catch (Exception e) {

}

One: Write a text file with a byte stream

Write data in memory to the hard disk by byte stream

FileOutputStream fos=null;

Try {

String str= "You are the April day of the Earth";

byte [] Words=str.getbytes ("gb2312");

Create a Stream object, write a file in Append mode

fos=New fileoutputstream ("C:\\ming.txt",true);

Write file

Fos.write (words,0,words.length);

System. out. println ("file updated");

} catch (Exception e) {

System. out. println ("Error creating file! ");

}finally{

Try {

if (fos!=null) {

Fos.close ();

}

} catch (Exception E2) {

E2.printstacktrace ();

}

}

Second: Reading a text file using a character stream

Reading a text file using a character stream

Reader fr=null;

Try {

fr=New filereader ("C:\\ming.txt");

Char [] ch=new Char[1024];//broker, Buffer

StringBuffer sbf=New stringbuffer ();

int length=fr.read (CH);//Read the characters into the array

Loop Read and append characters

while (Length!=-1) {

Sbf.append (ch,0,length);

Length=fr.read (CH);

}

System. out. Print (SBF);

} catch (Exception e) {

E.printstacktrace ();

}finally{

Try {

if (fr!=null) {

Fr.close ();

}

} catch (Exception E2) {

TODO: Handle exception

}

}

Two: Write a text file using a character stream

Write a text file using a character stream

FileWriter fw=null;

Try {

fw=New FileWriter ("C:\\ming.txt",true);

Write information

String words= "Frances Yip-Shanghai Beach";

Fw.write (words);

Fw.flush ();

System. out. println ("write success");

} catch (Exception e) {

System. out. println ("File does not exist");

}finally{

Try {

if (fw!=null) {

Fw.close ();

}

} catch (Exception E2) {

E2.printstacktrace ();

}

}

Three: Character input stream BufferedReader read file

FileReader fr=null;

BufferedReader br=null;

Try {

Create a FileReader object

fr=New filereader ("C:\\ming.txt");

Create a BufferedReader object

br=New BufferedReader (FR);

Reading a row of data

String Line=br.readline ();

while (line!=null) {

System. out. println (line);

Line=br.readline ();

}

} catch (Exception e) {

E.printstacktrace ();

}finally{

Try {

if (br!=null) {

Br.close ();

}if (fr!=null) {

Fr.close ();

}

} catch (Exception E2) {

E2.printstacktrace ();

}

}

Three: Use Mybufferedwriter to write text files

FileWriter fw=null;

BufferedWriter bw=null;

Try {

fw=New FileWriter ("C:\\ming.txt",true);

bw=New BufferedWriter (FW);

Write information

Bw.write ("The original scenery of the hometown");

Bw.newline ();

Bw.write ("Moonlight in town-Mavis Hsu");

Bw.flush ();

fw.close ();  

Read File contents

FileReader fr=New filereader ("C:\\ming.txt");

BufferedReader br=New BufferedReader (FR);

String Line=br.readline ();

while (line!=null) {

System. out. println (line);

Line=br.readline ();

}

} catch (Exception e) {

System. out. println ("File does not exist");

E.printstacktrace ();

}finally{

Try {

if (bw!=null) {

Bw.close ();

}

if (fw!=null) {

Fw.close ();

}

} catch (Exception E2) {

TODO: Handle exception

}

}

Four://Use Byte stream class DataOutputStream write binary file

DataOutputStream out=null;

DataInputStream dis=null;

Try {

Creating an input Stream object

FileInputStream fis=New fileinputstream ("c:\\ Fanning. jpg");

dis=new datainputstream (FIS);

Creating an output Stream object

FileOutputStream outfile=New fileoutputstream ("c:\\ Fanning Little Beauty 33.jpg");

out=New DataOutputStream (outFile);

int temp=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 ();

}

}

Four sets of read and write file schemes

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.