字节,字节数组读写
package com.test2;
import java.io.*;
public class test3 {
public static void main(String[] args) throws IOException {
String origin_path = "C:\\Users\\xxx\\sss\\Picture\\123.png";
String change_path = "MyClass\\123.png";
long startTime = System.currentTimeMillis();
method4(origin_path, change_path);
long endTime = System.currentTimeMillis();
System.out.println("共耗时:" + (endTime - startTime) + "毫秒");
}
public static void method1(String origin_path, String change_path) throws IOException {
FileInputStream f1 = new FileInputStream(origin_path);
FileOutputStream f2 = new FileOutputStream(change_path);
int by;
while ((by = f1.read()) != -1) {
f2.write(by);
}
f1.close();
f2.close();
}
public static void method2(String origin_path, String change_path) throws IOException {
FileInputStream f1 = new FileInputStream(origin_path);
FileOutputStream f2 = new FileOutputStream(change_path);
byte[] bys = new byte[2048];
int len;
while ((len = f1.read(bys)) != -1) {
f2.write(bys, 0, len);
}
f1.close();
f2.close();
}
public static void method3(String origin_path, String change_path) throws IOException {
BufferedInputStream bf1 = new BufferedInputStream(new FileInputStream(origin_path));
BufferedOutputStream bf2 = new BufferedOutputStream(new FileOutputStream(change_path));
int by;
while ((by = bf1.read()) != -1) {
bf2.write(by);
}
bf1.close();
bf2.close();
}
public static void method4(String origin_path, String change_path) throws IOException {
BufferedInputStream bf1 = new BufferedInputStream(new FileInputStream(origin_path));
BufferedOutputStream bf2 = new BufferedOutputStream(new FileOutputStream(change_path));
byte[] bys = new byte[1024];
int len;
while ((len = bf1.read(bys)) != -1) {
bf2.write(bys, 0, len);
}
bf1.close();
bf2.close();
}
}
字符,字符数组读写
package com.test2;
import java.io.*;
public class test6 {
public static void main(String[] args) throws IOException {
String origin_path = "C:\\Users\\herol\\Desktop\\123.txt";
String change_path = "MyClass\\幻想爱情故事.txt";
long startTime = System.currentTimeMillis();
method5(origin_path, change_path);
long endTime = System.currentTimeMillis();
System.out.println("共耗时:" + (endTime - startTime) + "毫秒");
}
public static void method1(String origin_path, String change_path) throws IOException {
FileReader f1 = new FileReader(origin_path);
FileWriter f2 = new FileWriter(change_path);
int by;
while ((by = f1.read()) != -1) {
f2.write(by);
}
f1.close();
f2.close();
}
public static void method2(String origin_path, String change_path) throws IOException {
FileReader f1 = new FileReader(origin_path);
FileWriter f2 = new FileWriter(change_path);
char[] bys = new char[2048];
int len;
while ((len = f1.read(bys)) != -1) {
f2.write(bys, 0, len);
}
f1.close();
f2.close();
}
public static void method3(String origin_path, String change_path) throws IOException {
BufferedReader bf1 = new BufferedReader(new FileReader(origin_path));
BufferedWriter bf2 = new BufferedWriter(new FileWriter(change_path));
int by;
while ((by = bf1.read()) != -1) {
bf2.write(by);
}
bf1.close();
bf2.close();
}
public static void method4(String origin_path, String change_path) throws IOException {
BufferedReader bf1 = new BufferedReader(new FileReader(origin_path));
BufferedWriter bf2 = new BufferedWriter(new FileWriter(change_path));
char[] bys = new char[1024];
int len;
while ((len = bf1.read(bys)) != -1) {
bf2.write(bys, 0, len);
}
bf1.close();
bf2.close();
}
public static void method5(String origin_path, String change_path) throws IOException {
BufferedReader bf1 = new BufferedReader(new FileReader(origin_path));
BufferedWriter bf2 = new BufferedWriter(new FileWriter(change_path));
String line;
while ((line = bf1.readLine()) != null) {
bf2.write(line);
bf2.newLine();
bf2.flush();
}
bf1.close();
bf2.close();
}
}