go語言開發環境搭建_GO語言開發環境搭建
阿新 • • 發佈:2021-02-17
讀入的操作(核心)
//3.讀入的操作
//read(char[] cbuf):返回每次讀入cbuf陣列中的字元的個數。如果達到檔案末尾,返回-1
char[] cbuf = new char[5];
int len;
while((len = fr.read(cbuf)) != -1){
//方式一:
//錯誤的寫法
// for(int i = 0;i < cbuf.length;i++){
// System.out.print(cbuf[i]);
// }
//正確的寫法
// for(int i = 0;i < len;i++){
// System.out.print(cbuf[i]);
// }
//========================================================================================
//方式二:
//錯誤的寫法,對應著方式一的錯誤的寫法
// String str = new String(cbuf);
// System.out.print(str);
//正確的寫法
String str = new String(cbuf,0,len);
System.out.print(str);
}
從記憶體中寫出資料到硬碟的檔案裡
從記憶體中寫出資料到硬碟的檔案裡。
說明:
1. 輸出操作,對應的File可以不存在的。並不會報異常
2.
File對應的硬碟中的檔案如果不存在,在輸出的過程中,會自動建立此檔案。
File對應的硬碟中的檔案如果存在:
如果流使用的構造器是:FileWriter(file,false) / FileWriter(file):對原有檔案的覆蓋
如果流使用的構造器是:FileWriter(file,true):不會對原有檔案覆蓋,而是在原有檔案基礎上追加內容
使用FileReader和FileWriter實現文字檔案的複製
public void testFileReaderFileWriter() {
FileReader fr = null;
FileWriter fw = null;
try {
//1.建立File類的物件,指明讀入和寫出的檔案
File srcFile = new File("hello.txt");
File destFile = new File("hello2.txt");
//不能使用字元流來處理圖片等位元組資料 [視訊591]
// File srcFile = new File("愛情與友情.jpg");
// File destFile = new File("愛情與友情1.jpg");
//2.建立輸入流和輸出流的物件
fr = new FileReader(srcFile);
fw = new FileWriter(destFile);
//3.資料的讀入和寫出操作
char[] cbuf = new char[5];
int len;//記錄每次讀入到cbuf陣列中的字元的個數
while((len = fr.read(cbuf)) != -1){
//每次寫出len個字元
fw.write(cbuf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.關閉流資源
//方式一:
// try {
// if(fw != null)
// fw.close();
// } catch (IOException e) {
// e.printStackTrace();
// }finally{
// try {
// if(fr != null)
// fr.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
//方式二:
try {
if(fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用位元組流FileInputStream處理文字檔案,可能出現亂碼(視訊592)
//使用位元組流FileInputStream處理文字檔案,可能出現亂碼。
@Test
public void testFileInputStream() {
FileInputStream fis = null;
try {
//1. 造檔案
File file = new File("hello.txt");
//2.造流
fis = new FileInputStream(file);
//3.讀資料
byte[] buffer = new byte[16]; //最小為8時才不出現亂碼
int len;//記錄每次讀取的位元組的個數
while((len = fis.read(buffer)) != -1){
String str = new String(buffer,0,len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis != null){
//4.關閉資源
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
結論:
1. 對於文字檔案(.txt,.java,.c,.cpp),使用字元流處理
2. 對於非文字檔案(.jpg,.mp3,.mp4,.avi,.doc,.ppt,...),使用位元組流處理
開發中不會使用檔案流,一般使用緩衝流。
自己的規範實現
package com.atguigu.test_1;
import java.io.*;
public class BufferedTest_1 {
public static void main(String[] args) throws IOException {
//造檔案
File srcFile = new File("桌布.png");
File destFile = new File("複製的桌布.png");
//造流
//造節點流
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
//造緩衝流
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
//複製的細節:讀取、寫入
byte[] bytes = new byte[5];
int len;
//13467:用fis和fos
//88:用bis和bos
long start = System.currentTimeMillis();
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}
long end = System.currentTimeMillis();
System.out.println(end - start);
//資源的關閉:關閉緩衝流即可
bis.close();
bos.close();
}
}