Java 字节和字符流的读写+Buffered
生活随笔
收集整理的這篇文章主要介紹了
Java 字节和字符流的读写+Buffered
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一個關于IO流的導圖
IO流字節的讀寫,實現復制
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python爬虫框架怎么安装_celer
- 下一篇: JavaScript编程用法——Java