JAVA 字节流 INPUTSTREAM OUTPUTSTREAM
InputStream字节输入流
OutputStream字节输出流
用于以字节的形式读取和写入数据
步骤 1 : ASCII码 概念
所有的数据存放在计算机中都是以数字的形式存放的。 所以字母就需要转换为数字才能够存放。
比如A就对应的数字65,a对应的数字97. 不同的字母和符号对应不同的数字,就是一张码表。
ASCII是这样的一种码表。 只包含简单的英文字母,符号,数字等等。 不包含中文,德文,俄语等复杂的。
示例中列出了可见的ASCII码以及对应的十进制和十六进制数字,不可见的暂未列出
字符 | 十进制数字 | 十六进制数字 |
! | 33 | 21 |
" | 34 | 22 |
# | 35 | 23 |
$ | 36 | 24 |
% | 37 | 25 |
& | 38 | 26 |
’ | 39 | 27 |
( | 40 | 28 |
) | 41 | 29 |
* | 42 | 2A |
+ | 43 | 2B |
, | 44 | 2C |
- | 45 | 2D |
. | 46 | 2E |
/ | 47 | 2F |
0 | 48 | 30 |
1 | 49 | 31 |
2 | 50 |
3 | 51 | 33 |
4 | 52 | 34 |
5 | 53 | 35 |
6 | 54 | 36 |
7 | 55 | 37 |
8 | 56 | 38 |
9 | 57 | 39 |
: | 58 | 3A |
; | 59 | 3B |
< | 60 | 3C |
= | 61 | 3D |
大于号 | 62 | 3E |
@ | 64 | 40 |
A | 65 | 41 |
B | 66 | 42 |
C | 67 | 43 |
D | 68 | 44 |
E | 69 | 45 |
F | 70 | 46 |
G | 71 |
H | 72 | 48 |
I | 73 | 49 |
J | 74 | 4A |
K | 75 | 4B |
L | 76 | 4C |
M | 77 | 4D |
N | 78 | 4E |
O | 79 | 4F |
P | 80 | 50 |
Q | 81 | 51 |
R | 82 | 52 |
S | 83 | 53 |
T | 84 | 54 |
U | 85 | 55 |
V | 86 | 56 |
W | 87 | 57 |
X | 88 | 58 |
Y | 89 | 59 |
Z | 90 | 5A |
[ | 91 | 5B |
\ | 92 | 5C |
] | 93 | 5D |
^ |
_ | 95 | 5F |
` | 96 | 60 |
a | 97 | 61 |
b | 98 | 62 |
c | 99 | 63 |
d | 100 | 64 |
e | 101 | 65 |
f | 102 | 66 |
g | 103 | 67 |
h | 104 | 68 |
i | 105 | 69 |
j | 106 | 6A |
k | 107 |
l | 108 | 6C |
m | 109 | 6D |
n | 110 | 6E |
o | 111 | 6F |
p | 112 | 70 |
q | 113 | 71 |
r | 114 | 72 |
s | 115 | 73 |
t | 116 | 74 |
u | 117 | 75 |
v | 118 | 76 |
w | 119 | 77 |
x | 120 | 78 |
y | 121 | 79 |
z | 122 | 7A |
{ | 123 | 7B |
竖杠 | 124 | 7C |
} | 125 | 7D |
~ | 126 | 7E |
步骤 2 : 以字节流的形式读取文件内容
InputStream是字节输入流,同时也是抽象类,只提供方法声明,不提供方法的具体实现。 FileInputStream
是InputStream子类,以FileInputStream 为例进行文件读取
InputStream是字节输入流,同时也是抽象类,只提供方法声明,不提供方法的具体实现。
FileInputStream 是InputStream子类,以FileInputStream 为例进行文件读取
package com.example.IO; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Arrays; import java.util.Date; public class Test1 { public static void main(String[] args) { try { //准备文件file.txt其中的内容是AB,对应的ASCII分别是65 66 File file = new File("G:\\File\\file.txt"); //创建基于文件的输入流 FileInputStream fis = new FileInputStream(file); //创建字节数组,其长度就是文件的长度(强转) byte[] all = new byte[(int) file.length()]; //以字节流的形式读取文件所有内容 fis.read(all); for (byte b : all) { //打印出来是65 66 System.out.println(b); } //每次使用完关闭流不然占用内存会比较大 fis.close(); } catch (IOException e) { e.printStackTrace(); } } }
运行结果
65 66
步骤 3 : 以字节流的形式向文件写入数据
OutputStream是字节输出流,同时也是抽象类,只提供方法声明,不提供方法的具体实现。
FileOutputStream 是OutputStream子类,以FileOutputStream 为例向文件写出数据
注: 如果文件d:/lol2.txt不存在,写出操作会自动创建该文件。
但是如果是文件 d:/xyz/lol2.txt,而目录xyz又不存在,会抛出异常
package stream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class TestStream { public static void main(String[] args) { try { // 准备文件lol2.txt其中的内容是空的 File f = new File("d:/lol2.txt"); // 准备长度是2的字节数组,用88,89初始化,其对应的字符分别是X,Y byte data[] = { 88, 89 }; // 创建基于文件的输出流 FileOutputStream fos = new FileOutputStream(f); // 把数据写入到输出流 fos.write(data); // 关闭输出流 fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
关闭流的方式
在try中关闭
在try的作用域里关闭文件输入流,在前面的示例中都是使用这种方式,这样做有一个弊端;
如果文件不存在,或者读取的时候出现问题而抛出异常,那么就不会执行这一行关闭流的代码,存在巨大的资源占用隐患。 不推荐使用
package stream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class TestStream { public static void main(String[] args) { try { File f = new File("d:/lol.txt"); FileInputStream fis = new FileInputStream(f); byte[] all = new byte[(int) f.length()]; fis.read(all); for (byte b : all) { System.out.println(b); } // 在try 里关闭流 fis.close(); } catch (IOException e) { e.printStackTrace(); } } }
在finally中关闭
这是标准的关闭流的方式
首先把流的引用声明在try的外面,如果声明在try里面,其作用域无法抵达finally.
在finally关闭之前,要先判断该引用是否为空
关闭的时候,需要再一次进行try catch处理
这是标准的严谨的关闭流的方式,但是看上去很繁琐,所以写不重要的或者测试代码的时候,都会采用上面的有隐患try的方式,因为不麻烦~
package stream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class TestStream { public static void main(String[] args) { File f = new File("d:/lol.txt"); FileInputStream fis = null; try { fis = new FileInputStream(f); byte[] all = new byte[(int) f.length()]; fis.read(all); for (byte b : all) { System.out.println(b); } } catch (IOException e) { e.printStackTrace(); } finally { // 在finally 里关闭流 if (null != fis) try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
使用try()的方式
把流定义在try()里,try,catch或者finally结束的时候,会自动关闭
这种编写代码的方式叫做 try-with-resources, 这是从JDK7开始支持的技术
所有的流,都实现了一个接口叫做 AutoCloseable,任何类实现了这个接口,都可以在try()中进行实例化。 并且在try, catch, finally结束的时候自动关闭,回收相关资源。
package stream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class TestStream { public static void main(String[] args) { File f = new File("d:/lol.txt"); //把流定义在try()里,try,catch或者finally结束的时候,会自动关闭 try (FileInputStream fis = new FileInputStream(f)) { byte[] all = new byte[(int) f.length()]; fis.read(all); for (byte b : all) { System.out.println(b); } } catch (IOException e) { e.printStackTrace(); } } } **在finally中关闭**
这是标准的关闭流的方式
首先把流的引用声明在try的外面,如果声明在try里面,其作用域无法抵达finally.
在finally关闭之前,要先判断该引用是否为空
关闭的时候,需要再一次进行try catch处理
这是标准的严谨的关闭流的方式,但是看上去很繁琐,所以写不重要的或者测试代码的时候,都会采用上面的有隐患try的方式,因为不麻烦~
package com.example.IO; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class TestStream { public static void main(String[] args) { //文件中字母 随意 字节码转化 File file = new File("F:\\IDEA_gulimall\\Login_Register\\yanwc\\src\\main\\java\\com\\example\\IO\\file.txt"); FileInputStream fis = null; try{ fis=new FileInputStream(file); byte[] all=new byte[(int) file.length()]; fis.read(all); for(byte n:all){ System.out.println(n); } }catch (Exception e){ e.printStackTrace(); }finally { // 在finally 里关闭流 if (null!=fis) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
运行结果:
57 54
使用try()的方式
把流定义在try()里,try,catch或者finally结束的时候,会自动关闭
这种编写代码的方式叫做 try-with-resources, 这是从JDK7开始支持的技术
所有的流,都实现了一个接口叫做 AutoCloseable,任何类实现了这个接口,都可以在try()中进行实例化。 并且在try, catch, finally结束的时候自动关闭,回收相关资源
package com.example.IO; import java.io.File; import java.io.FileInputStream; public class TestStream { /*public static void main(String[] args) { //文件中字母 随意 字节码转化 File file = new File("F:\\IDEA_gulimall\\Login_Register\\yanwc\\src\\main\\java\\com\\example\\IO\\file.txt"); FileInputStream fis = null; try{ fis=new FileInputStream(file); byte[] all=new byte[(int) file.length()]; fis.read(all); for(byte n:all){ System.out.println(n); } }catch (Exception e){ e.printStackTrace(); }finally { // 在finally 里关闭流 if (null!=fis) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } */ public static void main(String[] args) { //文件中字母 随意 字节码转化 File file = new File("F:\\IDEA_gulimall\\Login_Register\\yanwc\\src\\main\\java\\com\\example\\IO\\file.txt"); try{ //把流定义在try()里,try,catch或者finally结束的时候,会自动关闭 FileInputStream fis = null; fis=new FileInputStream(file); byte[] all=new byte[(int) file.length()]; fis.read(all); for(byte n:all){ System.out.println(n); } }catch (Exception e) { e.printStackTrace(); } } }
结果
57 54
IO字符流
Reader字符输入流
Writer字符输出流
专门用于字符的形式读取和写入数据
使用字符流读取文件
FileReader 是Reader子类,以FileReader 为例进行文件读取
package com.example.IO; import java.io.File; import java.io.FileReader; public class TestStream1 { public static void main(String[] args) { // 准备文件lol.txt其中的内容是56 File file = new File("F:\\IDEA_gulimall\\Login_Register\\yanwc\\src\\main\\java\\com\\example\\IO\\file.txt"); // 创建基于文件的Reader try(FileReader reader=new FileReader(file)) { // 创建字符数组,其长度就是文件的长度 char[] le=new char[(int) file.length()]; reader.read(le); for (char ne:le){ // 打印出来是56 System.out.println(ne); } }catch (Exception e){ e.printStackTrace(); } } }
运行结果是
9 6
使用字符流把字符串写入到文件
FileWriter 是Writer的子类,以FileWriter 为例把字符串写入到文件
public static void main(String[] args) { File file = new File("F:\\IDEA_gulimall\\Login_Register\\yanwc\\src\\main\\java\\com\\example\\IO\\file.txt"); try(FileWriter writer=new FileWriter(file)){ String ate="asdcderwerwew"; char[] ss=ate.toCharArray(); writer.write(ss); System.out.println("执行完成"); }catch (Exception e){ e.printStackTrace(); } }
运行结果是
执行完成
更新中,未完成;