日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java 字节和字符流的读写+Buffered

發布時間:2023/12/10 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java 字节和字符流的读写+Buffered 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一個關于IO流的導圖

IO流字節的讀寫,實現復制

import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;public class TestCopy {public static void main(String[] args) throws IOException {copyThree(new File("F:\\joy\\1.mp4"), new File("F:\\1.mp4"));}// 適應小文件。public static void copyOne(File pathInput, File pathOutput) throws IOException {// 獲得輸入流FileInputStream fis = new FileInputStream(pathInput);// 輸出流(寫入流)FileOutputStream fos = new FileOutputStream(pathOutput);int a = 0;// 一個一個字節的讀取,并寫入。while ((a = fis.read()) != -1) {fos.write(a);}fos.close();fis.close();}// 針對文件中等大小的, 對于太大的裝不完, 內存溢出。public static void copyTwo(File pathInput, File pathOutput) throws IOException {FileInputStream fis = new FileInputStream(pathInput); FileOutputStream fos = new FileOutputStream(pathOutput);//用byte數組裝 字節 fis.available() 最大的字節數byte[] bb = new byte[fis.available()];fos.write(bb); fos.close();fis.close();}// 超大文件如mp4的復制, 多次裝。public static void copyThree(File pathInput, File pathOutput) throws IOException {FileInputStream fis = new FileInputStream(pathInput); FileOutputStream fos = new FileOutputStream(pathOutput);//可以定義一次裝的大小。緩沖區, 100MBbyte[] bb = new byte[1024 * 1024 * 100];int len;// len 實際讀取的字節數。while ((len = fis.read(bb)) != -1) {fos.write(bb, 0, len);}fos.close();fis.close(); }}

JavaIO字符流的讀寫

import java.io.FileReader; import java.io.FileWriter; import java.io.IOException;public class TestCharCopy {public static void main(String[] args) {// 復制 FileWriter 和 FileReader 讀取字符操作。// 把a.txt 里面的內容復制到 b.txt java 太強了不僅跨平臺 還支持復制各種文件。try {FileReader fr = new FileReader("a.txt");FileWriter fw = new FileWriter("b.txt");char[] ch = new char[10];int len;while ((len = fr.read(ch)) != -1) {fw.write(ch, 0, len);}fw.close();fr.close();} catch (IOException e) {e.printStackTrace(); }}}

JavaIO流buffered 類 的 讀寫。

package c12_24;import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException;public class TestBuffered {public static void main(String[] args) {// 相對路徑測試一下。//method1("a.txt", "b.txt");method2("a.txt", "b.txt");}// bufferedWriter bufferedReaderprivate static void method1(String path1, String path2) {try {BufferedReader br = new BufferedReader(new FileReader(path1));BufferedWriter bw = new BufferedWriter(new FileWriter(path2));//2 MB 緩沖區int len;char[] ch = new char[1024 * 1024];//最多 len 個字節長度讀入數組while ((len = br.read(ch)) != -1) {bw.write(ch, 0, len);bw.newLine();//換行}bw.close();br.close();} catch(IOException e) {}}// bufferedInputStream bufferedOutputStream private static void method2(String path1, String path2) {try {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path1));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path2));int len;// 1kb 緩沖區byte[] b = new byte[1024 * 1];while ((len = bis.read(b)) != -1) {bos.write(b, 0, len);}bos.close();bis.close();} catch(IOException e) {}}}

字節流轉換字符流 讀取

public static void method2(String path) {FileInputStream fis = null;InputStreamReader isr = null;try {// 字節流 轉換成 字符流fis = new FileInputStream(path);isr = new InputStreamReader(fis);int i;while ((i = isr.read()) != -1) {System.out.print((char)i);} } catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {isr.close(); fis.close();} catch (IOException e) {System.out.println(e);}}}

字節流轉換字符流 寫入

// 寫入文件public static void method3(String path) {FileOutputStream fos = null;OutputStreamWriter fsw = null;try {fos = new FileOutputStream(path);fsw = new OutputStreamWriter(fos);fsw.write("asshole");} catch (FileNotFoundException e) {System.out.println(e);} catch (IOException e) {System.out.println(e);} finally {try {fsw.close();fos.close();} catch (IOException e) {System.out.println(e);}}}

找出以.png … .jpg … .xml結束的文件

import java.io.File; import java.io.FileFilter; import java.io.IOException;public class testFile {public static void main(String[] args) throws IOException {//get endWith .jpg files File imgs = new File("C:\\Users\\admin\\Pictures\\Saved Pictures\\cartoon");//實現過濾器接口 去掉不合規則的 再加到數組File[] lf = imgs.listFiles(new myFilter());for (File file : lf) {System.out.println(file);}}}class myFilter implements FileFilter {@Overridepublic boolean accept(File pathname) {if (pathname.isFile() && pathname.getName().endsWith(".jpg"))return true;return false;}}

總結

以上是生活随笔為你收集整理的Java 字节和字符流的读写+Buffered的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。