本篇接上篇:Java中的字符流,流的读写的细节参考上篇
本篇讲述字节流相关话题,包括字节流的读取与写出,字节流转化为字符流
1.明确是不是纯文本:纯文本 ? 字符流: 字节流
2.明确数据来源
( 输入流 I )和数据流向
( 输出流 O )
3.I流和O流对接,数据传输
另外:须要字符编码转换,使用字节流转换字符流java
数据来源( 输入流 I ):内存、磁盘、网络、键盘 数据流向( 输出流 O ): 内存、磁盘、网络、控制台
FileOutputStream fos = null; try { String fileName = "I:\\Java\\Base\\Thinking\\src\\IOTest\\FileInputStream.txt"; fos = new FileOutputStream(fileName); fos.write("Line1 第一行".getBytes());//不须要刷新缓冲 } catch (IOException e) { e.printStackTrace(); } finally { try { if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } }
public static void main(String[] args) { try { String fileName = "I:\\Java\\Base\\Thinking\\src\\IOTest\\FileInputStream.txt"; FileInputStream fis = new FileInputStream(fileName); //readOneByOne(fis); //readByByteArrayByWhile(fis); } catch (IOException e) { e.printStackTrace(); } }
字节
读private static void readOneByOne(FileInputStream fis) throws IOException { int ch = 0; while ((ch = fis.read()) != -1) { System.out.println(ch + "=" + (char) ch); } }
一共输出了15个字节,和
Line1 第一行
有出入,缘由:
在utf-8编码下,一个中文字符占三个字节git
76=L 105=i 110=n 101=e 49=1 32= 231=ç 172=¬ 172=¬ 228=ä 184=¸ 128=� 232=è 161=¡ 140=�
private static void readByByteArrayByWhile(FileInputStream fis) throws IOException { //字符数组循环读取 byte[] buf = new byte[8]; int len = 0; while ((len = fis.read(buf)) != -1) { System.out.println(new String(buf, 0, len)); } }
可见buf一次装个字节,
第
由三个字节表示,被分红了两半,因此读出了乱码github
//Line1 � //�一行
文件字节数 System.out.println(fis.available());//15
视频大小:576M编程
耗时:6.162444569秒数组
private static void copy() { FileOutputStream fos = null; FileInputStream fis = null; try { fis = new FileInputStream("I:\\Java\\Base\\Thinking\\src\\IOTest\\video.avi"); fos = new FileOutputStream("F:\\javaTest\\IO\\video.avi"); byte[] buf = new byte[1024]; int len = 0; while ((len = fis.read(buf)) != -1) { fos.write(buf, 0, len); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (fis != null) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } }
方法耗时:33.478968503秒微信
private static void copyByBuf() { BufferedOutputStream bfos = null; BufferedInputStream bfis = null; try { FileInputStream fis = new FileInputStream("I:\\Java\\Base\\Thinking\\src\\IOTest\\video.avi"); bfis = new BufferedInputStream(fis); FileOutputStream fos = new FileOutputStream("F:\\javaTest\\IO\\video.avi"); bfos = new BufferedOutputStream(fos); int b = 0; while ((b = bfis.read()) != -1) { bfos.write(b); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (bfis != null) { bfis.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (bfos != null) { bfos.close(); } } catch (IOException e) { e.printStackTrace(); } } }
分析:键盘录入是一个字节输入流: InputStream in = System.in; 因为输入的都是字符,使用字符流将比较方便(固然字节流也能够,不过麻烦一点) 1.用字符流InputStreamReader将字节流转化 2.再用字符包装流BufferedReader包装一下(固然也能够不包装,不过麻烦一点)
public class Save2File { public static void main(String[] args) throws Exception { //数据源----键盘录入字节流转化为字符流后包装成BufferedReader BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in)); String path = "I:\\Java\\Base\\Thinking\\src\\IOTest\\键盘录入"; //数据流向 :"I:\\Java\\Base\\Thinking\\src\\IOTest\\键盘录入" 文件 BufferedWriter brw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path))); String line = null; while ((line=bfr.readLine())!=null){ if ("over".equals(line)) { break; } brw.write(line); brw.newLine(); brw.flush(); } bfr.close(); brw.close(); } }
默认字符编码为utf-8,这里使用GBK测试网络
public class Save2File { public static void main(String[] args) throws Exception { //数据源----键盘录入字节流转化为字符流后包装成BufferedReader BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in)); String path = "I:\\Java\\Base\\Thinking\\src\\IOTest\\键盘录入-GKB"; //数据流向 :"I:\\Java\\Base\\Thinking\\src\\IOTest\\键盘录入" 文件 BufferedWriter brw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),"GBK")); String line = null; while ((line=bfr.readLine())!=null){ if ("over".equals(line)) { break; } brw.write(line); brw.newLine(); brw.flush(); } bfr.close(); brw.close(); } }
可见二者的字节数不一样,一个GBK的汉字占2个字节
(2*5+\r\n=12)
,一个UTF-8汉字占3个字节(3*5+\r\n=17)
ide
分析:控制台输出是一个字节输出流:PrintStream out = System.out; 因为输出的都是字符,使用字符流将比较方便(固然字节流也能够,不过麻烦一点) 1.用字符流OutputStreamWriter将字节流转化 2.再用字符包装流BufferedWriter包装一下(固然也能够不包装,不过麻烦一点)
public class ReadFile2Console { public static void main(String[] args) throws Exception { //数据源----本地文件 String path = "I:\\Java\\Base\\Thinking\\src\\IOTest\\Activity.md"; BufferedReader bfr = new BufferedReader(new InputStreamReader(new FileInputStream(path))); //数据流向 :"I:\\Java\\Base\\Thinking\\src\\IOTest\\键盘录入" 文件 BufferedWriter brw = new BufferedWriter(new OutputStreamWriter(System.out)); String line = null; while ((line = bfr.readLine()) != null) { if ("over".equals(line)) { break; } brw.write(line); brw.newLine(); brw.flush(); } bfr.close(); brw.close(); } }
能够改变系统的录入流(数据来源),和控制台输出流(数据流向)测试
System.setIn(InputStream 输入流); 自定义System.in数据来源(默认键盘录入) System.setOut(PrintStream 输出流);自定义System.out数据流向(默认控制台)
public class ReadFile2Console { public static void main(String[] args) throws Exception { //数据源----本地文件 String path = "I:\\Java\\Base\\Thinking\\src\\IOTest\\Activity.md"; System.setIn(new FileInputStream(path));//将键盘源改成了磁盘文件源 System.setOut(new PrintStream("F:\\hell.txt"));//将流向控制台改成流向磁盘文件 BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in)); //数据流向 :"I:\\Java\\Base\\Thinking\\src\\IOTest\\键盘录入" 文件 BufferedWriter brw = new BufferedWriter(new OutputStreamWriter(System.out)); String line = null; while ((line = bfr.readLine()) != null) { if ("over".equals(line)) { break; } brw.write(line); brw.newLine(); brw.flush(); } bfr.close(); brw.close(); } }
项目源码 | 日期 | 备注 |
---|---|---|
V0.1--无 | 2018-10-10 | Java中的字节流与字符流转化 |
V0.2--无 | - | - |
笔名 | 微信 | 爱好 | |
---|---|---|---|
张风捷特烈 | 1981462002 | zdl1994328 | 语言 |
个人github | 个人简书 | 个人CSDN | 我的网站 |
1----本文由张风捷特烈原创,转载请注明
2----欢迎广大编程爱好者共同交流
3----我的能力有限,若有不正之处欢迎你们批评指证,一定虚心改正
4----看到这里,我在此感谢你的喜欢与支持网站