您好,登錄后才能下訂單哦!
在我們?nèi)粘5拈_(kāi)發(fā)中使用最多的Java知識(shí)點(diǎn)就是Java的IO流和Java集合兩大模塊,后續(xù)的文章我會(huì)接著介紹Java集合的知識(shí),本篇博文我主要講解Java的輸入流和輸出流。
我們知道任何的編程語(yǔ)言,首先要解決的就是如何與外部介質(zhì)交換數(shù)據(jù),這就包括了如何將數(shù)據(jù)寫(xiě)到外部介質(zhì)以及如何讀取外部介質(zhì)的數(shù)據(jù);Java的I/O流提供了讀寫(xiě)數(shù)據(jù)的標(biāo)準(zhǔn)方法,Java所有的I/O機(jī)制都是基于數(shù)據(jù)流進(jìn)行輸入輸出,這些數(shù)據(jù)流表示了字符或者字節(jié)數(shù)據(jù)的流動(dòng)序列。
Java的I/O流按讀寫(xiě)方式可以分為字節(jié)流和字符流兩種類(lèi)型;其實(shí)字符流的本質(zhì)還是字節(jié)流,只不過(guò)字符流對(duì)字節(jié)流做了一次封裝。需要注意的是:當(dāng)我們讀寫(xiě)二進(jìn)制文件時(shí)(如:圖片、視頻、音頻等),只能使用字節(jié)流,否則會(huì)操作失敗。
按照讀寫(xiě)介質(zhì)的不同,對(duì)于I/O流我們還有另一種劃分方法,分別是面向顯示器、鍵盤(pán)的操作、面向內(nèi)存的操作、面向磁盤(pán)的操作以及面向網(wǎng)絡(luò)的操作。
輸入流、輸出流中的輸入和輸出均是相對(duì)于Java程序來(lái)說(shuō),輸入即是向程序輸入數(shù)據(jù)(讀),輸出即是程序向外部輸出數(shù)據(jù)(寫(xiě))。
I/O流的相關(guān)類(lèi)如下圖所示:
結(jié)合我自身開(kāi)發(fā)的經(jīng)驗(yàn),我認(rèn)為按照讀寫(xiě)介質(zhì)來(lái)講解理解會(huì)更加直觀(guān),所以下面我會(huì)按照不同的讀寫(xiě)介質(zhì)來(lái)講解I/O流。
1、面向顯示器、鍵盤(pán)的讀寫(xiě)操作(標(biāo)準(zhǔn)輸入、輸出)
面向顯示器的寫(xiě)操作和面向鍵盤(pán)的讀操作,就是我們常用的System.out和System.in;使用System.out可以向我們的顯示器輸出信息,使用System.in可以從我們的鍵盤(pán)獲取信息,示例代碼如下:
/** * 標(biāo)準(zhǔn)輸入輸出 */ public class SystemIOTest { public static void main(String[] args) throws Exception{ System.out.println("請(qǐng)輸入姓名:"); Scanner scanner = new Scanner(System.in); System.out.println(scanner.nextLine()); } }
2、面向磁盤(pán)的讀寫(xiě)操作
面向磁盤(pán)的讀寫(xiě)操作,主要就是指對(duì)文件的讀寫(xiě)操作,它主要包括FileInputStream、FileOutputStream、FileReader、FileWriter,示例代碼如下:
/** * java io 測(cè)試類(lèi) */ public class JavaIOTest { /** * 字節(jié)流讀取二進(jìn)制文件(如圖片、音樂(lè)文件) */ @Test public void inputstreamPicTest() { try { InputStream inputStream = new FileInputStream("e:" + File.separator + "logo.png"); OutputStream outputStream = new FileOutputStream("e:" + File.separator + "copy.png"); byte[] bytes = new byte[1024]; int len = 0; while ((len = inputStream.read(bytes)) != -1) { outputStream.write(bytes, 0, len); } inputStream.close(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 字節(jié)流讀取文本 */ @Test public void inputStreamTxt() { try { InputStream inputStream = new FileInputStream("e:" + File.separator + "sql.txt"); OutputStream outputStream = new FileOutputStream("e:" + File.separator + "copy.txt"); byte[] chars = new byte[1024]; int len = 0; while ((len = inputStream.read(chars)) != -1) { outputStream.write(chars, 0, len); outputStream.flush(); } inputStream.close(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 字符流讀取文本 * 字符流直接讀取文本時(shí),中文可能會(huì)亂碼,所以通過(guò)InputStreamReader建字節(jié)流轉(zhuǎn)換為字符流 */ @Test public void inputReaderTxt() { try { InputStreamReader reader = new InputStreamReader(new FileInputStream("e:" + File.separator + "sql.txt"), "GBK"); Writer writer = new FileWriter("e:" + File.separator + "copy.txt"); int len = 0; char[] chars = new char[1024]; while ((len = reader.read(chars)) != -1) { writer.write(chars, 0, len); writer.flush(); } reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 通過(guò)字符串的方式讀取文本(可以提高效率) */ @Test public void stringTxt() { try { InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("e:" + File.separator + "sql.txt"),"GBK"); BufferedReader reader = new BufferedReader(inputStreamReader); String str = ""; while ((str = reader.readLine()) != null) { System.out.println("==" + str); } inputStreamReader.close(); reader.close(); } catch (Exception e) { e.printStackTrace(); } } }
3、面向內(nèi)存的讀寫(xiě)操作
面向內(nèi)存的讀寫(xiě)操作,主要用于中間數(shù)據(jù)的緩存,主要包括:ByteArrayInputStream、ByteArrayOutputStream、ObjecInputStream、ObjectOutputStream,示例代碼如下:
/** * java io 測(cè)試類(lèi) */ public class JavaIOTest { /** * 對(duì)象流(可用于對(duì)象的序列化和反序列化) * 僅測(cè)試使用,Java原生的序列化和反序列化效率較低,建議使用Hession * 對(duì)象序列化至文件 */ @Test public void testObjectInputstream() { try { File file = new File("e:" + File.separator + "seri.txt"); if(!file.exists()) { file.createNewFile(); } ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("e:" + File.separator + "seri.txt")); SeriEntity seriEntity = new SeriEntity("hello world"); outputStream.writeObject(seriEntity); outputStream.close(); outputStream.flush(); ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("e:" + File.separator + "seri.txt")); SeriEntity obj = (SeriEntity) inputStream.readObject(); inputStream.close(); System.out.println("========" + obj.getName()); } catch (Exception e) { e.printStackTrace(); } } /** * 對(duì)象流(可用于對(duì)象的序列化和反序列化) * 對(duì)象序列化至內(nèi)存 */ @Test public void testObjectInputStreamByte() { try { SeriEntity seriEntity = new SeriEntity("name name"); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream outputStream = new ObjectOutputStream(byteArrayOutputStream); outputStream.writeObject(seriEntity); outputStream.flush(); ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())); SeriEntity entity = (SeriEntity) inputStream.readObject(); inputStream.close(); System.out.println("======" + entity.getName()); } catch (Exception e) { e.printStackTrace(); } } }
4、面向網(wǎng)絡(luò)的的操作
面向網(wǎng)絡(luò)的操作即Java的網(wǎng)絡(luò)編程,主要包括Socket、ServerSocket,示例代碼如下:
/** * java 網(wǎng)絡(luò)編程--客戶(hù)端 */ public class NetClient { /** * 服務(wù)端IP地址 */ private static final String SERVERADDR = "127.0.0.1"; /** * 服務(wù)端提供的端口號(hào) */ private static final int SERVERPORT = 9090; public static void main(String[] args) { Socket socket = null; try { // 和服務(wù)端建立連接 socket = new Socket(SERVERADDR, SERVERPORT); System.out.println("==========客戶(hù)端已啟動(dòng)=============="); // 向服務(wù)端發(fā)送數(shù)據(jù) String data = "java網(wǎng)絡(luò)編程實(shí)例程序"; // 獲取網(wǎng)絡(luò)輸出流 OutputStream outputStream = socket.getOutputStream(); // 向網(wǎng)絡(luò)輸出流寫(xiě)入數(shù)據(jù)(即向服務(wù)端發(fā)送數(shù)據(jù)) outputStream.write(data.getBytes()); outputStream.flush(); //寫(xiě)關(guān)閉,不然會(huì)一直阻塞著,服務(wù)器會(huì)認(rèn)為客戶(hù)端還一直在寫(xiě)數(shù)據(jù) //由于從客戶(hù)端發(fā)送的消息長(zhǎng)度是任意的,客戶(hù)端需要關(guān)閉連接以通知服務(wù)器消息發(fā)送完畢,如果客戶(hù)端在發(fā)送完最后一個(gè)字節(jié)后 //關(guān)閉輸出流,此時(shí)服務(wù)端將知道"我已經(jīng)讀到了客戶(hù)端發(fā)送過(guò)來(lái)的數(shù)據(jù)的末尾了,即-1",就會(huì)讀取出數(shù)據(jù)并關(guān)閉服務(wù)端的讀數(shù)據(jù)流,在之后就可以 //自己(服務(wù)端)的輸出流了,往客戶(hù)端寫(xiě)數(shù)據(jù)了 socket.shutdownOutput(); // 接收服務(wù)端數(shù)據(jù) InputStream inputStream = socket.getInputStream(); byte[] bytes = new byte[1024]; int len = 1; while ((len = inputStream.read(bytes)) != -1) { System.out.println(new String(bytes, "UTF-8")); } } catch (Exception e) { e.printStackTrace(); } finally { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * java網(wǎng)絡(luò)編程 -- 服務(wù)端 */ public class NetServer { /** * 服務(wù)端提供的端口 */ private static final int SERVERPORT = 9090; public static void main(String[] args) { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(SERVERPORT); System.out.println("===========服務(wù)端已啟動(dòng)==========="); while (true) { // 接收客戶(hù)端請(qǐng)求 Socket socket = serverSocket.accept(); // 接收客戶(hù)端發(fā)送的信息 InputStream inputStream = socket.getInputStream(); byte[] bytes = new byte[1024]; int len = 1; System.out.println("客戶(hù)端發(fā)送消息:"); while ((len = inputStream.read(bytes)) != -1) { System.out.println(new String(bytes, "UTF-8")); } String data = "服務(wù)端已收到消息"; System.out.println(data); OutputStream outputStream = socket.getOutputStream(); outputStream.write(data.getBytes()); outputStream.flush(); //寫(xiě)關(guān)閉,不然會(huì)一直阻塞著,服務(wù)器會(huì)認(rèn)為客戶(hù)端還一直在寫(xiě)數(shù)據(jù) //由于從客戶(hù)端發(fā)送的消息長(zhǎng)度是任意的,客戶(hù)端需要關(guān)閉連接以通知服務(wù)器消息發(fā)送完畢,如果客戶(hù)端在發(fā)送完最后一個(gè)字節(jié)后 //關(guān)閉輸出流,此時(shí)服務(wù)端將知道"我已經(jīng)讀到了客戶(hù)端發(fā)送過(guò)來(lái)的數(shù)據(jù)的末尾了,即-1",就會(huì)讀取出數(shù)據(jù)并關(guān)閉服務(wù)端的讀數(shù)據(jù)流,在之后就可以 //自己(服務(wù)端)的輸出流了,往客戶(hù)端寫(xiě)數(shù)據(jù)了 socket.shutdownOutput(); } } catch (Exception e) { e.printStackTrace(); } finally { try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
由于篇幅有限文章中的代碼無(wú)法一一列出,如需獲取全部的源碼信息,可關(guān)注微信公眾號(hào) 布衣暖,回復(fù) java基礎(chǔ) 獲取全部的源碼信息
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。