Java Stream objects: InputStream, OutputStream, Reader, Writer

Source: Internet
Author: User
Tags processing text

The flow object is generally closed with the Close method. Frees resources.

InputStream and OutputStream

Both are abstract parent classes of byte input and output, with bytes processing data, each reading and writing a byte, suitable for processing binary files such as audio, video, pictures, etc.

Redaer and writer are abstract parent classes of character input and output, processing data in characters, each time reading or writing a character, suitable for processing text files, implementing classes have FileReader and FileWriter.

Attached: Java code to implement a copy of the picture file:

Package com.buaa.comparable;

Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;

public class Testcopy {

public static void Main (string[] args) {
Copy ();
}

static void Copy () {
1. Create a data source file
File File = new file ("E:\\picture1.jpg");
InputStream is = null;
File file1 = null;

OutputStream OS = null;

try {
Building an input stream pipeline
is = new FileInputStream (file);
3. Create the destination document;
File1 = new File ("e:\\picture2.jpg");
4. Build the output stream pipeline
OS = new FileOutputStream (file1);
int b = 0;

Reads a byte from the source file every time, as if the destination file writes a byte
while ((B=is.read ())!=-1) {
Os.write (b);
}

} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
if (os!=null) {
Os.close ();
}else if (is!=null) {
Is.close ();
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}

Java Stream objects: InputStream, OutputStream, Reader, Writer

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.