文件字節流:
代碼
package ht;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class F {
public static void main(String[] args) {
new F().copy("C:/Users/ACER/Desktop/計算機組成原理 指令縮寫.png", "C:/Users/ACER/Desktop/NEW計算機組成原理 指令縮寫.png");
}
public static void copy(String inPath,String outPath){
try {
FileInputStream in = new FileInputStream(inPath);
FileOutputStream out = new FileOutputStream(outPath);
byte b[] = new byte[10]; //字節數組
int len=0;
//用while不用擔心字節數組的大小問題,都能存進去
//沒有讀到值了默認是-1,才結束
while((len=in.read(b)) != -1){
out.write(b,0,len); //從b裏寫:0開始,長度爲len
}
out.flush(); //清空緩衝區
out.close(); //關流
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
文件字符流(適合複製內容爲字符的文件)
代碼
package ht;
import java.io.FileReader;
import java.io.FileWriter;
public class F {
public static void main(String[] args) {
new F().copy("C:/Users/ACER/Desktop/文本.txt", "C:/Users/ACER/Desktop/NEW文本.txt");
}
public static void copy(String inPath,String outPath){
try {
FileReader fr = new FileReader(inPath);
FileWriter fw = new FileWriter(outPath);
char [] c= new char[10]; //大小不重要,因爲while讀
//一直讀 , 直到爲空-1
int len=0;
while((len=fr.read(c)) != -1){
fw.write(c,0,len); //寫從c的0開始,長度爲len
}
fw.flush(); //清空緩衝區
fw.close(); //關流
fr.close();
} catch (Exception e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
}
}
如果copy了圖片,可能就是這樣黑shai的
緩衝字節流:
緩衝流速度比上面快很多
代碼:
package ht;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class F {
public static void main(String[] args) {
try {
new F().copy("C:\\Users\\ACER\\Desktop\\計算機組成原理 指令縮寫.png","C:\\Users\\ACER\\Desktop\\NEW計算機組成原理 指令縮寫.png");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void copy(String inPath,String outPath) throws Exception{
//將字節輸入流對象放入緩衝字節輸入流對象中
FileInputStream in = new FileInputStream(inPath);
BufferedInputStream BIS = new BufferedInputStream(in);
//將字節輸出流對象放入緩衝字節輸出流對象中
FileOutputStream out = new FileOutputStream(outPath);
BufferedOutputStream BOS = new BufferedOutputStream(out);
byte b[] = new byte[10]; //大小不重要,因爲while讀
int len=0;
while((len=BIS.read(b)) != -1){
BOS.write(b,0,len); //從b裏寫:0開始,長度爲len
}
BOS.flush(); //清空緩存區
BOS.close(); //關流順序(遲到早退,後開先關)
out.close();
BIS.close();
in.close();
}
}
緩衝字符流:
代碼:
package ht;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class F {
public static void main(String[] args) {
try {
new F().copy("C:\\Users\\ACER\\Desktop\\文本.txt","C:\\Users\\ACER\\Desktop\\NEW文本.txt");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void copy(String inPath,String outPath) throws Exception{
//將字符輸入流對象放入緩衝字符輸入流對象中
FileReader fr = new FileReader(inPath);
BufferedReader BR = new BufferedReader(fr);
//將字符輸出流對象放入緩衝字符輸出流對象中
FileWriter fw = new FileWriter(outPath);
BufferedWriter BW = new BufferedWriter(fw);
char c[] = new char[10]; //大小不重要,因爲while讀
int len=0;
while((len=BR.read(c)) != -1){
BW.write(c,0,len);//從c裏寫:0開始,長度爲len
}
BW.flush(); //清空緩存區
BW.close(); //關流順序(遲到早退,後開先關)
fw.close();
BR.close();
fr.close();
}
}