Java編程中字節(jié)流與字符流IO操作示例
IO流基本概念
IO流用來處理設(shè)備之間的數(shù)據(jù)傳輸
Java對數(shù)據(jù)的操作是通過流的方式
Java用于操作流的對象都是在IO包上
流按操作數(shù)據(jù)分為兩種:字節(jié)流和字符流
流按流向分為:輸入流,輸出流。
字節(jié)流的抽象基類:InputStream,OutputStream
字符流的抽象基類:Reader,Writer
注:由這4個類派生出來的子類名稱都是以其父類名作為子類名的后綴。
如:InputStream的子類:FileInputStream
如:Reader的子類FileReader
如創(chuàng)建一個FileWriter對象,該對象一被初始化就必須要明確被操作的文件,而且該文件就會被創(chuàng)建到指定目錄下,如果該目錄下已有同名文件,將被覆蓋。
Demo :
package javase.day18; import java.io.FileWriter; import java.io.IOException; public class FileWriterDemo { public static void main(String[] args) { FileWriter fw=null; try { fw = new FileWriter("C:\\java_test\\FileWriterTest.txt"); //調(diào)用write 方法,將字符串寫入到流中 fw.write("alex test23"); //刷新流對象中的緩沖中的數(shù)據(jù) fw.flush(); } catch (IOException e) { e.printStackTrace(); } finally{ try { if(fw!=null){ //關(guān)閉流資源,但是關(guān)閉之前會刷新一次內(nèi)部的緩沖中的數(shù)據(jù) fw.close(); } }catch (IOException e) { e.printStackTrace(); } } } } package javase.day18; import java.io.FileWriter; import java.io.IOException; public class FileWriterDemo { public static void main(String[] args) { FileWriter fw=null; try { fw = new FileWriter("C:\\java_test\\FileWriterTest.txt"); //調(diào)用write 方法,將字符串寫入到流中 fw.write("alex test23"); //刷新流對象中的緩沖中的數(shù)據(jù) fw.flush(); } catch (IOException e) { e.printStackTrace(); } finally{ try { if(fw!=null){ //關(guān)閉流資源,但是關(guān)閉之前會刷新一次內(nèi)部的緩沖中的數(shù)據(jù) fw.close(); } }catch (IOException e) { e.printStackTrace(); } } } }
打印Java文件的源代碼Demo code:
package javase.day18; import java.io.FileReader; import java.io.IOException; public class FileReaderTest { public static void main(String[] args) { try { FileReader fr=new FileReader("C:\\java_test\\SystemDemo.java"); char[] buf=new char[1024]; int num=0; while((num=fr.read(buf))!=-1){ System.out.println(new String(buf,0,num)); } } catch (IOException e) { e.printStackTrace(); } } } package javase.day18; import java.io.FileReader; import java.io.IOException; public class FileReaderTest { public static void main(String[] args) { try { FileReader fr=new FileReader("C:\\java_test\\SystemDemo.java"); char[] buf=new char[1024]; int num=0; while((num=fr.read(buf))!=-1){ System.out.println(new String(buf,0,num)); } } catch (IOException e) { e.printStackTrace(); } } }
復(fù)制文件Demo code:
copy_1() 使用的方法是讀取一個字符則寫入一個字符。
copy_2()使用的方法是把字符一次性讀取到一個字符數(shù)組中,最后再一次寫入到目標(biāo)文件。
package javase.day18; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class CopyText { public static void main(String[] args) { try { copy_1(); } catch (IOException e) { e.printStackTrace(); } } public static void copy_1() throws IOException{ FileWriter fw = new FileWriter("C:\\java_test\\Copy_SystemDemo.java"); FileReader fr = new FileReader("C:\\java_test\\SystemDemo.java"); int num=0; while((num=fr.read())!=-1){ fw.write(num); } fw.close(); fr.close(); } public static void copy_2() throws IOException{ FileWriter fw = new FileWriter("C:\\java_test\\Copy_SystemDemo.java"); FileReader fr = new FileReader("C:\\java_test\\SystemDemo.java"); int num=0; char[] buf=new char[1024]; while((num=fr.read(buf))!=-1){ fw.write(buf,0,num); } fw.close(); fr.close(); } } package javase.day18; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class CopyText { public static void main(String[] args) { try { copy_1(); } catch (IOException e) { e.printStackTrace(); } } public static void copy_1() throws IOException{ FileWriter fw = new FileWriter("C:\\java_test\\Copy_SystemDemo.java"); FileReader fr = new FileReader("C:\\java_test\\SystemDemo.java"); int num=0; while((num=fr.read())!=-1){ fw.write(num); } fw.close(); fr.close(); } public static void copy_2() throws IOException{ FileWriter fw = new FileWriter("C:\\java_test\\Copy_SystemDemo.java"); FileReader fr = new FileReader("C:\\java_test\\SystemDemo.java"); int num=0; char[] buf=new char[1024]; while((num=fr.read(buf))!=-1){ fw.write(buf,0,num); } fw.close(); fr.close(); } }
字符流的緩沖區(qū):
緩沖區(qū)的出現(xiàn)提高了對數(shù)據(jù)的讀寫效率。
對應(yīng)類:BufferedWriter , BufferedReader .
緩沖區(qū)要結(jié)合流才可以使用。
在流的基礎(chǔ)上對流的功能進(jìn)行了增強(qiáng)。
IO流操作的基本規(guī)律:
1,明確源和目的:
源: 輸入流 InputStream , Reader
目的: 輸出流 OutputStream ,Writer
2,操作的數(shù)據(jù)是否是純文本:
是:字符流
否:字節(jié)流
即:(1) 當(dāng)為輸入字符流用Reader
(2) 當(dāng)為輸入字節(jié)流用InputStream
(3)當(dāng)為輸出字符流用Writer
(4)當(dāng)為輸出字節(jié)流用OutputStream
3,當(dāng)體系明確后,再明確要使用哪個具體的對象:
源設(shè)備:內(nèi)存,硬盤,鍵盤
目的設(shè)備:內(nèi)存,硬盤,控制臺
IO操作工具類
[1] String fileReaderStringHandle(String fileName)
將文件(由fileName指定)讀入到一個字符串;
[2] byte[] fileReaderByteHandle(String fileName)
將文件(由fileName指定)讀入到一個字節(jié)數(shù)組;
[3] void fileWriterHandle(String fileName, String text)
將字符串(由text指定)寫出到一個文件(由fileName指定)。
IOUtil.java
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; public class IOUtil { /** * 將文件讀入到一個String,利用FileReader+BufferedReader(提供readLine方法) * * @param fileName * @return String */ public static String fileReaderStringHandle(String fileName) { StringBuilder sb = new StringBuilder(); try { BufferedReader in = new BufferedReader(new FileReader(new File( fileName).getAbsoluteFile())); try { String s; while ((s = in.readLine()) != null) { sb.append(s); sb.append("\n"); } } finally { in.close(); } } catch (IOException e) { throw new RuntimeException(e); } return sb.toString(); } /** * 使用FileInputStream+BufferedInputStream以byte的方式處理文件 * * @param fileName * @return byte[] */ public static byte[] fileReaderByteHandle(String fileName) { byte[] data = null; try { BufferedInputStream bf = new BufferedInputStream( new FileInputStream(fileName)); try { data = new byte[bf.available()]; bf.read(data); } finally { bf.close(); } } catch (IOException e) { throw new RuntimeException(e); } return data == null ? new byte[] {} : data; } /** * 將指定的text寫入到文件名為fileName的文件中 * * @param fileName * @param text */ public static void fileWriterHandle(String fileName, String text) { try { PrintWriter out = new PrintWriter(new File(fileName) .getAbsoluteFile()); try { out.print(text); } finally { out.close(); } } catch (IOException e) { throw new RuntimeException(e); } } public static void main(String[] args) throws IOException { System.out.print(fileReaderStringHandle("src/IOUtil.java")); for (byte b : fileReaderByteHandle("src/IOUtil.java")) System.out.print(b); fileWriterHandle("zj.txt", fileReaderStringHandle("src/IOUtil.java")); } }
相關(guān)文章
一文帶你掌握J(rèn)ava8中函數(shù)式接口的使用和自定義
函數(shù)式接口是?Java?8?引入的一種接口,用于支持函數(shù)式編程,下面我們就來深入探討函數(shù)式接口的概念、用途以及如何創(chuàng)建和使用函數(shù)式接口吧2023-08-08Java源碼分析:Guava之不可變集合ImmutableMap的源碼分析
今天給大家?guī)淼氖顷P(guān)于Java源碼的相關(guān)知識,文章圍繞著Java ImmutableMap展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下,希望能給你帶來幫助2021-06-06Spring-data-redis操作redis知識總結(jié)
這篇文章主要介紹了Spring-data-redis操作redis知識總結(jié),spring-data-redis是spring-data模塊的一部分,專門用來支持在spring管理項目對redis的操作。2017-04-04解決異常:Invalid?keystore?format,springboot配置ssl證書格式不合法問題
這篇文章主要介紹了解決異常:Invalid?keystore?format,springboot配置ssl證書格式不合法問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03解決SpringBoot整合ElasticSearch遇到的連接問題
這篇文章主要介紹了解決SpringBoot整合ElasticSearch遇到的連接問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08java?環(huán)境配置(2023年詳細(xì)教程)
這篇文章首先為了完善我的知識體系,今后一些軟件的安裝教程也可能會用到想寫一個更加詳細(xì)的,因為這并不僅僅是寫給?IT?行業(yè)的,其它行業(yè)可能也需要配置java環(huán)境2023-06-06