java IO 學習

來源:互聯網
上載者:User

標籤:put   buffer   ane   ade   iter   ted   建立   存在   字元流   

java IO 編程是指通過java 程式來操作檔案。比如檔案的讀寫,刪除,備份等。需要掌握檔案流的基本概念。 掌握IO編程方法,把握位元組流和字元流檔案在程式中是以流的形式來操作的什麼是位元組流:位元組流是以位元組為單位byte去讀取的,位元組流可以用於讀取二進位檔案以及任何類型的檔案什麼是字元流:字元流是以字元為單位去讀取的 字元流可以讀取文字檔。不能操作二進位檔案輸入: 位元組流 InputStream 字元流 Reader輸出: 位元組流 OutputStream 字元流 Wirter IO編程--File 類要做java IO 編程就需要匯入java.io包import java.io.*; 使用File類擷取到檔案對象。 File類只能擷取到檔案對象 而無法讀取或者寫入檔案package File_IO;import java.io.*;/*** 主要是學習java的檔案IO操作, java IO 操作需要匯入java.io包裡麵包含了所有的java io 類* Created by admin on 2017/8/27.*/public class JavaIO {public static void main(String[] args){// File 類的使用需要傳入檔案作為參數, 擷取檔案對象File file = new File("E:\\PythonCode\\spider\\China\\20170201152643\\iast0ic4wfw.jpg");// 擷取到File對象之後,我們就能擷取檔案的一些屬性// file.canExecute() 返回布爾值 檔案是否可以執行// file.canRead() 返回布爾值 檔案是否可讀// file.canWrite() 返回布爾值 檔案是否可寫// file.delete() 刪除該檔案// file.exists() 檔案是否存在// file.getPath() 擷取檔案路徑// file.isFile() 是否是檔案// file.isDirectory() 是否是目錄// file.isHidden() 是否是隱藏檔案// file.length() 檔案大小 單位是位元組}} 使用java的File類建立檔案和檔案夾 File file = new File("C:\\java.txt");if (!file.exists()){// 如果不存在該檔案則建立,使用createNewFile方法建立// createNewFile 方法必須要捕獲異常try {ff.createNewFile();} catch (IOException e) {e.printStackTrace();}} // 使用File 類建立檔案夾File directory = new File("C:\\IO");// 建立檔案夾if (directory.isDirectory()){System.out.println("檔案夾已經存在"); }else {// 建立檔案夾。如果是mkdirs()則是遞迴建立目錄directory.mkdir();} 使用File類列出一個檔案夾下面的所有檔案(非遞迴)File all = new File("C:\\");// listFiles() 會返迴文件對象的數組File filelist[] = all.listFiles();for (File files: filelist){System.out.println(files);} // IO 編程 使用FileInputStream讀取檔案內容。因為File類沒有讀寫的功能。因此需要使用FileInputStream File binFile = new File("F:\\test.txt");FileInputStream inputStream=null;try {inputStream = new FileInputStream(binFile);// 定義一個位元組數組,相當於緩衝,一次讀取這麼多的資料,避免讀取超大檔案時記憶體被撐爆byte []bytes = new byte[1024];// 定義的end 代表如果檔案實際大小沒有我們定義的1024 則以實際大小為準int end=0;while ((end=inputStream.read(bytes))!=-1){String s = new String(bytes, 0, end);System.out.println(s);// System.out.write(bytes, 0, end);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {// 關閉檔案try {inputStream.close();} catch (IOException e) {e.printStackTrace();}} java IO 編程,從鍵盤接收使用者輸入的資料,寫入檔案。 要寫入檔案需要用到FileOutputStream從鍵盤接收y使用者輸入需要用到System.in 和 BufferedReader//File fileObj = new File("C:\\object.txt");FileOutputStream fileOutputStream=null;if (!fileObj.exists()){fileObj.createNewFile();}BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));String string = null;string = bufferedReader.readLine();try {// 位元組輸出資料流fileOutputStream = new FileOutputStream(fileObj);// byte []bytes = new byte[1024];fileOutputStream.write(string.getBytes());}catch (FileNotFoundException e){e.printStackTrace();}finally {if (fileOutputStream !=null){fileOutputStream.close();}}  java IO 編程 檔案複製。 檔案複製,是一個輸入資料流, 一個輸出資料流。都需要用到。需要先把檔案讀取到記憶體,然後寫入指定的位置 // 複製檔案FileInputStream fileInputStream=null;FileOutputStream fileOutputStream1=null;File cpFile = new File("D:\\sqlback\\all.sql");File pasteFile = new File("C:\\all.sql");try {if (cpFile.exists()){fileInputStream = new FileInputStream(cpFile);byte []readbyte = new byte[1024];int fileEnd=0;fileOutputStream1 = new FileOutputStream(pasteFile);while ((fileEnd=fileInputStream.read(readbyte))!=-1){// 讀取到檔案資料之後 寫入其他位置,完成複製fileOutputStream1.write(readbyte);} }}catch (Exception e){e.printStackTrace();}finally {if (fileInputStream !=null){fileInputStream.close();}if (fileOutputStream1 !=null){fileOutputStream1.close();}} Java io 編程,讀取一個文字檔的內容,寫入另一個檔案。 讀寫文字檔需要使用字元流FileReader, FileWriter FileReader fileReader=null;FileWriter fileWriter=null;try {char cache[]= new char[1024];int readchar=0;fileReader = new FileReader("F:\\test.txt");fileWriter = new FileWriter("C:\\cp-test.txt");while ((readchar=fileReader.read(cache))!=-1){// System.out.println(String.valueOf(cache));fileWriter.write(cache);}}catch (Exception e){e.printStackTrace();}finally {if (fileReader !=null){fileReader.close();}if (fileWriter !=null){fileWriter.close();}} Java IO 編程 緩衝字元流。 有時候當讀取一個檔案比較大的時候。我們不可能全部讀進記憶體,我們需要一次讀取一點緩衝到記憶體當中。因此就需要緩衝字元流。 緩衝字元流又分為BufferReader和BufferWriter。直接操作String // BufferReader BufferWriter 的使用BufferedReader bufferedReader1=null;BufferedWriter bufferedWriter=null;try{// BufferReader BufferWriterFileReader fr = new FileReader("F:\\test.txt");bufferedReader1 = new BufferedReader(fr);FileWriter fw = new FileWriter("C:\\okc.txt");bufferedWriter = new BufferedWriter(fw);// 迴圈讀取String s="";// BufferReader 讀取到的內容不包含任何行的終止符,比如分行符號// 因此當你讀取一個檔案寫入寫入另外一個檔案時,你會發現寫入的內容沒有分行符號,需要我們寫檔案時自己加上去while ((s=bufferedReader1.readLine())!=null){fw.write(s+"##$$");} }catch (Exception e){e.printStackTrace();}finally {if (bufferedReader1 !=null){bufferedReader1.close();}if (bufferedWriter != null){bufferedWriter.close();}}

java IO 學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.