java之位元組流,字元流,轉換流
阿新 • • 發佈:2019-01-28
1.使用位元組的輸入流和輸出流來進行復制
位元組流不但可以寫文字還可以寫圖片,音訊
public static void main(String[] args){
FileInputStream f = new FileInputStream("/Users/lanou/Desktop/test/11.png");
FileInputStream f2 = new FileInputStream("/Users/lanou/Desktop/test/112233.png");
byte[] b = new byte[1024;]
int len = 0;
while((len = f1.read(b))!= -1 ){
f2.write(b,0,len)
}
f1.close();
f2.close();
}
首先繫結兩個檔案
然後用陣列的方式讀寫
最後關閉資料流
需求:將一個資料夾複製到另一個資料夾
引數 原資料夾 filestart
要被新增的資料夾 File fileend
//複製資料夾的方法
public static void fun(File filestart,File fileend) throws IOException {
File newfile = new File(filestart, fileend.getName());
newfile.mkdir();
File[] listFiles = filestart.listFiles();
for (File subfile : listFiles) {
if(subfile.isFile()) {
FileInputStream fileInputStream = new FileInputStream(subfile);
File file3 = new File(newfile, subfile.getName());
FileOutputStream fileOutputStream =new FileOutputStream(file3);
int len = 0;
byte[] b =new byte[1024];
while((len = fileInputStream.read(b))!=-1) {
fileOutputStream.write(b, 0, len);
}
}else {
fun(subfile, newfile);
}
}
}
字元流
也是一個字元一個字元的讀
和位元組流的區別就是字元流只能操作文字,不能寫圖片,音訊。
write是所有字元輸出流的父類,並且是抽象類
FileWriter
構造方法
1.檔案
2.字串
字元流的寫入
public static void main(String[] args) throws IOException {
FileWriter fileWriter = new FileWriter("/Users/lanou/Desktop/Xtest/haha.txt");
fileWriter.write(100);
//字元輸出流在寫入檔案的時候需要呼叫重新整理功能
//建議每次寫入最好都重新整理一次
fileWriter.flush();
char[] c = {'l','w','s','d'};
fileWriter.write(c);
fileWriter.flush();
fileWriter.write(c, 1, 2);
//使用字串直接寫入
fileWriter.write("床前明月光\n疑是地上霜\n");
fileWriter.flush();
fileWriter.write("白日依山盡", 2, 3);
fileWriter.flush();
//關閉資源前會重新整理
fileWriter.close();
}
/*
* 字元輸入流
* Reader (所有字元輸入流的父類,抽象類)
* fileReader
* 寫的時候能直接寫入字串
* 讀的時候不能 因為字串很難界定
*/
public static void main(String[] args) throws IOException {
FileReader fileReader = new FileReader("/Users/lanou/Desktop/Xtest/haha.txt");
char[] c = new char[1024];
int len = 0;
while ((len = fileReader.read(c)) != -1) {
System.out.println(new String(c, 0, len));
}
}
轉換流
OutputStreamWriter
可以根據不同的編碼形式寫入 需要使用到FIleOutputStream 類
inputStreamReader(位元組流轉向字元流)
可以讀取不同編碼格式檔案
需要使用到FIleInputStream 類
利用轉換流寫檔案 預設方式查UTF-8
public static void getUTF8() throws IOException {
// 建立 轉換流 字元流轉向位元組流
FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/Xtest/utf8.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos);
// 寫檔案
osw.write("丁鵬");
// 關閉資源
// 只關閉外層的流就可以了
osw.close();
}
使用GBK編碼來寫入檔案
private static void getGBK() throws IOException {
FileOutputStream f = new FileOutputStream("/Users/lanou/Desktop/Xtest/GBK.txt");
OutputStreamWriter o = new OutputStreamWriter(f, "gbk");// 這裡忽略大小寫
o.write("丁鵬");
o.close();
}
以utf- 8讀取
private static void readUtf() throws IOException {
FileInputStream fInputStream = new FileInputStream("/Users/lanou/Desktop/Xtest/utf8.txt");
InputStreamReader i = new InputStreamReader(fInputStream);
char[] c =new char[1024];
int len = 0;
while((len = i.read(c))!= -1) {
System.out.println(new String(c, 0, len));
}
i.close();
}
以gbk讀取
private static void readgbk() throws IOException {
FileInputStream fInputStream = new FileInputStream("/Users/lanou/Desktop/Xtest/utf8.txt");
InputStreamReader i = new InputStreamReader(fInputStream,"gbk");
char[] c =new char[1024];
int len = 0;
while((len = i.read(c))!= -1) {
System.out.println(new String(c, 0, len));
}
i.close();
}