輸入/輸出(Input/Output)是計算機系統中非常基礎的一部分,用於從計算機的外部讀取數據或將數據寫出到計算機的外部。在Java中,I/O操作被封裝在Java IO流中。Java IO流提供了非常靈活的方法來操作各種類型的數據,包括文件、網路連接、內存和其他設備等。
一、Java IO流類型
在Java中,IO流可以分為兩種類型:位元組流和字元流。位元組流主要用於讀寫二進位文件,如圖片和視頻等;字元流主要用於讀寫純文本文件,如TXT文件等。
1、位元組流
位元組流是IO操作的基礎,在Java中,位元組流主要有兩個抽象類:InputStream和OutputStream。InputStream是用於讀取數據的抽象類,而OutputStream則是用於寫出數據的抽象類。
//讀取文件並輸出文件內容 public void readFile(String fileName) throws IOException{ FileInputStream fis = new FileInputStream(fileName); byte[] buffer = new byte[1024]; int len = 0; while((len = fis.read(buffer)) != -1){ System.out.println(new String(buffer,0,len)); } fis.close(); } //將數據寫入到指定文件 public void writeFile(String fileName,String content) throws IOException{ FileOutputStream fos = new FileOutputStream(fileName); fos.write(content.getBytes()); fos.close(); }
2、字元流
字元流是針對純文本文件進行操作的IO流。在Java中,字元流主要有兩個抽象類:Reader和Writer。Reader是用於讀取數據的抽象類,而Writer則是用於寫出數據的抽象類。
//讀取文件並輸出文件內容 public void readFile(String fileName) throws IOException{ FileReader fr = new FileReader(fileName); char[] buffer = new char[1024]; int len = 0; while((len = fr.read(buffer)) != -1){ System.out.println(new String(buffer,0,len)); } fr.close(); } //將數據寫入到指定文件 public void writeFile(String fileName,String content) throws IOException{ FileWriter fw = new FileWriter(fileName); fw.write(content); fw.close(); }
二、Java IO流的讀寫方式
Java IO流提供了多種讀寫方式,我們可以根據需要來選擇適合的方式進行讀寫。下面將介紹Java IO流的常用讀寫方式。
1、位元組流的讀寫方式
位元組流提供了多種讀寫方式,其中常用的有FileInputStream和FileOutputStream。這兩個類可以分別向文件中讀寫位元組流。
//使用FileInputStream讀取文件內容並輸出 public void readFile(String fileName) throws IOException{ FileInputStream fis = new FileInputStream(fileName); byte[] buffer = new byte[fis.available()]; fis.read(buffer); System.out.println(new String(buffer)); fis.close(); } //使用FileOutputStream寫出文件內容到指定文件中 public void writeFile(String fileName,String content) throws IOException{ FileOutputStream fos = new FileOutputStream(fileName); fos.write(content.getBytes()); fos.close(); }
2、字元流的讀寫方式
字元流提供了多種讀寫方式,常用的有FileReader和FileWriter。這兩個類可以分別向文件中讀寫字元流。
//使用FileReader讀取文件內容並輸出 public void readFile(String fileName) throws IOException{ FileReader fr = new FileReader(fileName); char[] buffer = new char[fr.read()]; fr.read(buffer); System.out.println(new String(buffer)); fr.close(); } //使用FileWriter寫出文件內容到指定文件中 public void writeFile(String fileName,String content) throws IOException{ FileWriter fw = new FileWriter(fileName); fw.write(content); fw.close(); }
三、Java IO流的性能優化
在Java中,IO操作是非常耗時的一個過程。為了提高IO操作的效率,我們可以使用緩衝區或者NIO來進行優化。
1、緩衝區的使用
使用緩衝區可以大大提高讀寫效率,在Java中,InputStream和OutputStream都提供了BufferedReader和BufferedWriter用於緩衝讀寫。
//使用BufferedReader讀取文件內容並輸出 public void readFile(String fileName) throws IOException{ BufferedReader br = new BufferedReader(new FileReader(fileName)); String line = null; while((line = br.readLine()) != null){ System.out.println(line); } br.close(); } //使用BufferedWriter寫出文件內容到指定文件中 public void writeFile(String fileName,String content) throws IOException{ BufferedWriter bw = new BufferedWriter(new FileWriter(fileName)); bw.write(content); bw.close(); }
2、NIO的使用
NIO(Non-blocking IO)是Java 1.4引入的新特性,主要用於對IO操作進行優化。在NIO中,使用通道(Channel)來讀寫數據。
//使用NIO讀取文件內容並輸出 public void readFile(String fileName) throws IOException{ RandomAccessFile raf = new RandomAccessFile(fileName,"rw"); FileChannel channel = raf.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); while(channel.read(buffer) > 0){ buffer.flip(); System.out.println(new String(buffer.array())); buffer.clear(); } channel.close(); } //使用NIO寫出文件內容到指定文件中 public void writeFile(String fileName,String content) throws IOException{ RandomAccessFile raf = new RandomAccessFile(fileName,"rw"); FileChannel channel = raf.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); buffer.put(content.getBytes()); buffer.flip(); channel.write(buffer); channel.close(); }
四、Java IO流的異常處理
在進行IO操作的時候,可能會出現各種異常,例如文件不存在、沒有讀寫許可權等。為了確保程序的穩定性,在進行IO操作的時候一定要進行異常處理。
//讀取文件內容並輸出 public void readFile(String fileName) throws IOException{ BufferedReader br = null; try{ br = new BufferedReader(new FileReader(fileName)); String line = null; while((line = br.readLine()) != null){ System.out.println(line); } br.close(); }catch(FileNotFoundException e){ System.out.println("文件不存在!"); }catch(IOException e){ System.out.println("讀取文件出錯!"); }finally{ if(br != null){ br.close(); } } } //將數據寫入到指定文件 public void writeFile(String fileName,String content) throws IOException{ BufferedWriter bw = null; try{ bw = new BufferedWriter(new FileWriter(fileName)); bw.write(content); bw.close(); }catch(FileNotFoundException e){ System.out.println("文件不存在!"); }catch(IOException e){ System.out.println("寫出文件出錯!"); }finally{ if(bw != null){ bw.close(); } } }
總結
Java IO流提供了多種讀寫方式,並且還有緩衝區和NIO等優化方式來提高IO操作的效率。在進行IO操作的時候一定要進行異常處理,確保程序的穩定性。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/307299.html