Java檔案操作基礎

來源:互聯網
上載者:User

Java的檔案操作在系統開發中是非常平凡的功能,如:檔案的上傳下載,Excel類檔案資料的匯入匯出等等。

通常的IO操作有兩種,一種是位元組流的操作,一種是字元流的操作。

位元組流有輸入資料流和輸出資料流,字元流也有輸入資料流和輸出資料流。

輸入資料流(從螢幕、硬碟或是其他資料來源讀取資料放入記憶體中)和輸出資料流(用來向螢幕、硬碟等目的地輸出內容)

位元組流就是一個位元組一個位元組的讀取或是輸出(位元組都是8位,0到255之間的整數)

字元流是按照文本的那種字元來讀取和輸出,就是直接讀取數字、字母或是中文字等這些我們能夠直接識別的字元。

關於緩衝流:

緩衝流,是指當輸出的時候帶 有緩衝功能的流(BufferOutputStream),沒有緩衝功能的流當其輸出時是直接存入目的地,如果有緩衝功能,則會將輸出的內容先放置在記憶體中,等到有一定資料量的時候,或是流關閉、或調用flush()方法時,才會將相應的內容儲存到硬碟或是其它目的地中。

位元組流操作時,我們通常用FileInputStream,FileOutputStream。而字元流操作時,我人通常用到FileReader和FileWriter。配合緩衝流使用:BufferedReader和BufferedWriter。BufferedInputStream和BufferedOutputStream。

下面代碼是一個檔案操作的基礎執行個體:

package com.zyujie.io;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader;public class FileOp {/* * 根據路徑判斷是否為目錄 */public boolean isDirectory(String path){File dirName = new File(path);if(dirName.isDirectory()){return true;}else{return false;}}/* * 建立目錄 */public boolean createDirectory(String path){File dirName = new File(path);return dirName.mkdir();}/* * 判斷檔案是否存在 */public boolean isFile(String path){File fileName = new File(path);if(fileName.isFile()){return true;}else{return false;}}/* * 建立檔案 */public boolean createFile(String path) throws IOException{File fileName = new File(path);return fileName.createNewFile();}/* * 向檔案裡寫入內容,如果是向文字檔裡寫內容建議使用字元流的FileWriter,不容易亂碼 * 如果是產生圖片這類檔案,建議使用位元組流的FileOutputStream。或者緩衝流 */public void writeFile(String path){try {//讀取控制台輸入字元,進緩衝區InputStreamReader isr = new InputStreamReader(System.in);BufferedReader br = new BufferedReader(isr);//指定要寫入的目標檔案FileWriter fw = new FileWriter(path);//緩衝對檔案的輸出BufferedWriter bw = new BufferedWriter(fw);while(true){//讀一行String tempStr = br.readLine();//寫一行bw.write(tempStr);//寫入一行分隔字元bw.newLine();//如果寫入字元流中有end,就退出寫入操作if(tempStr.equals("end")){break;}}br.close();isr.close();bw.flush();bw.close();fw.close();} catch (Exception e) {}}/* * 讀檔案,同理寫檔案操作,通過字元流去操作 */public void readFile(String path){try {//讀取檔案,指定目標檔案FileReader fr = new FileReader(path);//將輸入資料流檔案寫入緩衝區BufferedReader br = new BufferedReader(fr);//定義字串變數存取讀出來的值,預設不佔記憶體String tempStr = "";//如果此流已經準備好被讀取while(br.ready()){tempStr += br.readLine();}br.close();fr.close();System.out.println(tempStr);} catch (Exception e) {}}/* * 指定目錄產生圖片和excel類的檔案,採用位元組流,檔案複製的一個功能 */public void copyImage(String path,String newPath){try {//讀取要複製的靶心圖表片File file = new File(path);//將靶心圖表片讀成位元組流FileInputStream fis = new FileInputStream(file);//將位元組流緩衝到緩衝區BufferedInputStream bis = new BufferedInputStream(fis);//指定產生的靶心圖表片位置File newFile = new File(newPath);//準備輸出資料流FileOutputStream fos = new FileOutputStream(newFile);//輸出資料流檔案緩衝BufferedOutputStream bos = new BufferedOutputStream(fos);//位元組流長度int len = 0;//一直迴圈到位元組流長度末尾//不使用緩衝流//while((len = fis.read()) != -1){//fos.write(len);//}//使用緩衝流while((len = bis.read()) != -1){bos.write(len);}bos.flush();bos.close();fos.close();bis.close();fis.close();} catch (Exception e) {}}public static void main(String[] args) throws IOException {FileOp op = new FileOp();String path = "D://zhouyujie//log.txt";String imgPath = "D://zhouyujie//edu.png";String imgNewPath = "D://zhouyujie//edu附件.png";String xlsPath = "D://zhouyujie//tcms.xlsx";String xlsNewPath = "D://zhouyujie//tcms附件.xlsx";boolean flag = op.isFile(path);if(flag){System.out.println("檔案存在");//讀檔案op.readFile(path);//複製圖片op.copyImage(imgPath, imgNewPath);//複製excelop.copyImage(xlsPath, xlsNewPath);}else{boolean f = op.createFile(path);if(f){System.out.println("檔案產生,請在控制台寫入字串。");//指定檔案寫入內容op.writeFile(path);}else{System.out.println("檔案產生失敗");}}}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.