Stream object with buffer, basic data writing type, and copy command in command line

Source: Internet
Author: User

When we see the word Buffererd, we may be able to guess that these two classes should be stream classes with buffers. As we thought, they do have a buf data member, which is a character array with a default size of 2048 bytes. When we read data, BufferedInputStream fills up the buf as much as possible. When reading data using the read () method, it actually reads data from the buf first, instead of reading data directly from a data source (such as a hard disk. BufferedInputStream calls the read () method of InputStream to read data from the specified data source only when the data in the buf is insufficient.

Buf, a data member of BufferedOutputStream, is a 512-byte array. When we call the write () method to write data, it is actually written to the buf first, data is written to a specified device (such as a hard disk) only when the buf is full ). We can also manually call the flush () function to refresh the buffer and forcibly write data out of the memory.

The following two classes are used to implement the file copy function:


[Java]
Package cls;
 
Import java. io .*;
 
Public class BufferedStreamDemo
{
Public static void main (String [] args) throws Exception
{
// Specify the file from the command line parameters
File fSource = new File (args [0]);
File fDest = new File (args [1]);

// Create a stream object with buffer
BufferedInputStream bis = new BufferedInputStream (new FileInputStream (fSource ));
BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream (fDest ));

// Prompt message
System. out. println ("copy" + fSource. length () + "bytes ");


Byte [] buf = new byte [1];
While (bis. read (buf )! =-1) // read () returns the int type. The return value-1 indicates that the object has ended.
Bos. write (buf); // write data

// Refresh the buffer
Bos. flush ();

// Close the stream
Bos. close ();
Bis. close ();

// Prompt message
System. out. println ("copy" + fDest. length () + "bytes finished ");
}
}

Package cls;

Import java. io .*;

Public class BufferedStreamDemo
{
Public static void main (String [] args) throws Exception
{
// Specify the file from the command line parameters
File fSource = new File (args [0]);
File fDest = new File (args [1]);

// Create a stream object with buffer
BufferedInputStream bis = new BufferedInputStream (new FileInputStream (fSource ));
BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream (fDest ));

// Prompt message
System. out. println ("copy" + fSource. length () + "bytes ");


Byte [] buf = new byte [1];
While (bis. read (buf )! =-1) // read () returns the int type. The return value-1 indicates that the object has ended.
Bos. write (buf); // write data

// Refresh the buffer
Bos. flush ();

// Close the stream
Bos. close ();
Bis. close ();

// Prompt message
System. out. println ("copy" + fDest. length () + "bytes finished ");
}
}
6. DataInputStream and DataOutputStream

The DataInputStream and DataOutputStream classes provide methods for writing Basic Java data types, such as int, double, and boolean. Because the size of the basic data types in Java is fixed and won't be changed because of different machines, you don't have to worry about the data sizes of different platforms when writing data.

There is a writeUTF () method worth noting. This method writes the characters in the specified String object, but before writing data, it first writes two bytes of length data, which indicates the size of the characters with the written characters. The advantage is that when we use readUTF () to read data, we don't have to consider the data size issue. Just read it, because in readUTF () the length of the read data is controlled internally.


[Java]
Package cls;
 
Import java. io .*;
 
Class Student
{
String name;
Int score;

// Constructor
Public Student (String name, int score)
{
This. name = name;
This. score = score;
}

// Return name
Public String getName ()
{
Return name;
}
// Returns the score
Public int getScore ()
{
Return score;
}
}
 
Public class DataStreamDemo
{
Public static void main (String [] args) throws Exception
{
// Create three Student objects
Student [] sd = new Student [] {new Student ("dog", 100), new Student ("pig", 200), new Student ("cat ", (300 )};

// Create an output stream object
DataOutputStream dos = new DataOutputStream (new FileOutputStream (args [0]); // write to the file

// Use the enhanced for loop to write data
For (Student st: sd)
{
Dos. writeUTF (st. getName (); // write String
Dos. writeInt (st. getScore ());
}

Dos. flush (); // refresh the buffer
Dos. close (); // close the stream
 
// Read data from the file
DataInputStream dis = new DataInputStream (new FileInputStream (args [0]);

For (int I = 0; I <3; ++ I)
{
System. out. println (dis. readUTF (); // you can specify the length of a String.
System. out. println (dis. readInt ());
}

Dis. close ();
}
}

Package cls;

Import java. io .*;

Class Student
{
String name;
Int score;

// Constructor
Public Student (String name, int score)
{
This. name = name;
This. score = score;
}

// Return name
Public String getName ()
{
Return name;
}
// Returns the score
Public int getScore ()
{
Return score;
}
}

Public class DataStreamDemo
{
Public static void main (String [] args) throws Exception
{
// Create three Student objects
Student [] sd = new Student [] {new Student ("dog", 100), new Student ("pig", 200), new Student ("cat ", (300 )};

// Create an output stream object
DataOutputStream dos = new DataOutputStream (new FileOutputStream (args [0]); // write to the file

// Use the enhanced for loop to write data
For (Student st: sd)
{
Dos. writeUTF (st. getName (); // write String
Dos. writeInt (st. getScore ());
}

Dos. flush (); // refresh the buffer
Dos. close (); // close the stream

// Read data from the file
DataInputStream dis = new DataInputStream (new FileInputStream (args [0]);

For (int I = 0; I <3; ++ I)
{
System. out. println (dis. readUTF (); // you can specify the length of a String.
System. out. println (dis. readInt ());
}

Dis. close ();
}
}

 

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.