Java IO Demo

Source: Internet
Author: User

FileReader FileWriter Read and write English
public void FileReaderAndWriter1 () throws Exception {

File FilePath = new file ("E:/iotest");
if (!filepath.exists ()) {
Filepath.mkdirs ();
}

Write a file
File File = new file (Filepath.getpath (), "writer.txt");
FileWriter writer = new FileWriter (file);


for (int i = 0; i <; i++) {
Writer.write (New char[]{' A ', ' B ', ' C '});
Writer.write (new char[]{' \ R ', ' \ n '});
}
Writer.close ();

Read file
FileReader reader = new FileReader (file);
Char[] Chars=new char[100];
int readcount = 0;
while ((Readcount=reader.read (chars)) >0) {
System.out.print (string.valueof (chars) substring (0, readcount));
}
Reader.close ();
}
FileReader FileWriter Read/write Chinese
public void FileReaderAndWriter2 () throws Exception {

File FilePath = new file ("E:/iotest");
if (!filepath.exists ()) {
Filepath.mkdirs ();
}

Write a file
File File = new file (Filepath.getpath (), "writer.txt");
FileWriter writer = new FileWriter (file);
BufferedWriter bfwriter = new BufferedWriter (writer);

for (int i = 0; i < 10000; i++) {
Bfwriter.write ("Current line number" + i);
Bfwriter.write ("\ r \ n");
}
Bfwriter.flush ();
Writer.close ();
Bfwriter.close ();

Read file
FileReader reader = new FileReader (file);
BufferedReader bfreader =new BufferedReader (reader);
String line= "";
while ((Line=bfreader.readline ())!=null) {
System.out.println (line);
}

}

FileInputStream FileOutputStream
public void FileStream () throws exception{
File file1 =new file ("E:/iotest", "writer.txt");
FileInputStream input =new FileInputStream (file1);
Bufferedinputstream binput =new bufferedinputstream (input);

File file2 =new file ("E:/iotest", "writer1.txt");
FileOutputStream output = new FileOutputStream (file2);
Bufferedoutputstream boutput =new bufferedoutputstream (output);
byte [] b=new byte[1024];
int bcount;

while ((Bcount=binput.read (b)) >0) {
Boutput.write (b, 0, bcount);
}
Boutput.flush ();
Boutput.close ();
Binput.close ();

}

Bitarrayinputstream Bitarrayoutputstream
public void Bitstream () throws exception{
byte [] binput =new byte[]{12,-1,127,-128,0};

Bytearrayinputstream input = new Bytearrayinputstream (binput);
int i;
while ((i = Input.read ())!=-1) {
System.out.println (i);
}

Bytearrayoutputstream output = new Bytearrayoutputstream ();
Output.write (New byte[]{67,97,1});
String strout = output.tostring ();
System.out.println (Strout);
}

CharArrayReader Chararraywriter
public void Charstream () throws exception{
CharArrayReader Reader =new CharArrayReader (new char[]{' A ', ' B ', ' C '});
int i;
while ((I=reader.read ())!=-1) {
System.out.println (i);
}

Chararraywriter writer = new Chararraywriter ();
Writer.write ("Shanghai");
Char [] cbuf =new char[]{' A ', ' B ', ' C ', ' a '};
Writer.write (CBUF);

File File = new file ("E:/iotest/char.txt");
FileWriter FW = new FileWriter (file);

Writer.writeto (FW);

Writer.flush ();
Fw.flush ();

}

Objectstream
public void Objectstream () throws exception{

FileOutputStream fos = new FileOutputStream ("E:/iotest/object.txt");
ObjectOutputStream oos = new ObjectOutputStream (FOS);

Oos.writedouble (10.2);
Oos.writefloat (16.323f);
Oos.writechars ("China");

Student Stu =new Student (1, "CWG", New Date ());

Oos.writeobject (Stu);
Oos.flush ();
Oos.close ();

FileInputStream fis =new fileinputstream ("E:/iotest/object.txt");
ObjectInputStream ois = new ObjectInputStream (FIS);
System.out.println (Ois.readdouble ());
System.out.println (Ois.readfloat ());
System.out.println (Ois.readchar ());
System.out.println (Ois.readchar ());
Object obj = Ois.readobject ();
if (obj instanceof Student) {
Student Rstu = (Student) obj;
System.out.println (Rstu.getid () + "--" +rstu.getusername ());
}

}

public static void Main (string[] args) throws Exception {
Iodemo demo = new Iodemo ();
Demo. FileReaderAndWriter1 ();
Demo. FileReaderAndWriter2 ();
Demo. FileStream ();
Demo. Bitstream ();
Demo. Charstream ();
Demo. Objectstream ();

}

Java IO Demo

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.