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