|
1.Java flows is an important content of basic reading and writing files and copy knowledge test sites is a lot of interviews. Therefore, through this paper, a simple test summary.
2. The figure shows the text [IO / Binary IO]
3. Copy the text experimental Java implementation code:
package com.gdufe.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class TestInputOutputStream {
// Private final static String SOURCE = "t.txt";
// Private final static String TARGET = "tt.txt";
// Private final static String SOURCE = "p.png";
// Private final static String TARGET = "pp.png";
private final static String SOURCE = "D: \\ Tool_Software \\ mysql-installer-community-5.6.23.0.msi";
private final static String TARGET = "mysql-installer-community-5.6.23.0.msi";
//D:\Tool_Software\Ryuyan.zip
public static void main (String [] args) {
// TestInputOutputStream.copy1 (SOURCE, TARGET);
// TestInputOutputStream.copy2 (SOURCE, TARGET);
TestInputOutputStream.copy3 (SOURCE, TARGET);
System.out.println ( "- End ---");
}
public static void copy1 (String src, String tar) {
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream (src);
output = new FileOutputStream (tar);
int r;
while ((r = input.read ()) = -! 1) {
output.write ((byte) r);
}
} Catch (Exception e) {
e.printStackTrace ();
} Finally {
try {
input.close ();
output.close ();
} Catch (IOException e) {
e.printStackTrace ();
}
}
}
public static void copy2 (String src, String tar) {
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream (src);
output = new FileOutputStream (tar);
int length = 0;
byte [] b = new byte [1024];
while ((length = input.read (b)) = -! 1) {
output.write (b, 0, length);
}
} Catch (Exception e) {
e.printStackTrace ();
} Finally {
try {
input.close ();
output.close ();
} Catch (IOException e) {
e.printStackTrace ();
}
}
}
public static void copy3 (String src, String tar) {
InputStream input = null;
OutputStream output = null;
try {
input = new DataInputStream (new BufferedInputStream (new FileInputStream (src)));
output = new DataOutputStream (new BufferedOutputStream (new FileOutputStream (tar)));
/ *** Passes pro-test, buffer buffered stream read indeed faster **** /
int length = 0;
byte [] b = new byte [1024]; // read the first stream into the byte array
while ((length = input.read (b)) = -! 1) {
output.write (b, 0, length);
}
} Catch (Exception e) {
e.printStackTrace ();
} Finally {
try {
input.close ();
output.close ();
} Catch (IOException e) {
e.printStackTrace ();
}
}
}
} |
|
|
|