本章内容:简单了解IO流的分类;文本字节输入输出流、文本字符输入输出流基本操作
io流:流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。
* 按方向:输入流:运用了解码,将字节转成字符,是将数据源输入到程序里
* 输出流;运用了编码,将字符转成字节,是将程序的内容输出到指定的地方去。
* 按功能:节点流:处于io操作的第一线。所有操作必须通过他们进行;
* 处理流:直接使用节点流读写不方便,于是有了处理流,提高效率和性能,有了节点流
* 才会有处理流。
* 按数据:字节流:就是0和1,这就是字节。底层还是基于字节流操作。能处理所有类型的
* 数据,是8位二进制。
* 两个抽象类:inputStream(输入):FileInputStream(文件字节流)
* outputStream(输出):
* 字符流:就是一些中文英文,我们看的懂的,只能处理字符。这就涉及到字符集了,
* 字符集一般有:GBK,UTF-8,UNICODE,字符集可以解决乱码的问题。
* 只能处理字符类型的数据,是16位二进制。
* 两个抽象类:reader(读入):
* writer(写出):
import java.io.*;
学习流之前首先我们要搞清楚,这个输入、输出是怎么来区分的。
我们需要以程序为中心,或者说基准(我表述的可能不是很准确,理解就行),我们平时需要拿到其他文件的内容,比如文本数据拿到程序中,这个叫输入,文本数据输入到程序中;反之,将程序中的数据输出到其他文件中;从程序出去叫输出,数据进来程序叫输入。
上面写道:分为字节流和字符流,下面又分很多类,我们今天先讲常用的文本字节流和文本字符流。
文本字节输入流:fileInPutStream
//模拟文件数据大的时候,分段获取数据,输入流
public static void fileInput2(){
File file = new File("fileTest.txt");
InputStream is = null;
try {
is = new FileInputStream(file);
//分段读取,实际开发中,一般是几K的读取,1024就是1K,10K=1024*10这样写
byte[] flush = new byte[6];//缓冲容器
int len = -1;//接收长度
while ((len = is.read(flush))!=-1){
//解码
String str = new String(flush,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(null!=is){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
fileTest.txt文件我是放在项目下的,写的相对路径,如果不清楚路径怎么写,写绝对路径就行了。内容:
总额和好菜市场abcde
结果:我是按6个字节来分段的,可以发现是两个字,这就涉及到字符集了。我项目的字符集是UTF-8的,所以这个也是按照项目来的。UTF-8的字符集是1-3,也就是英文字母是1个字节,中文是3个字节。
总额
和好
菜市
场abc
de
文本字节输出流:fileOutPutStream
//文本字节输出流
public static void fileOut(){
File file = new File("testFile.txt");
OutputStream os = null;
try {
//FileOutputStream(file,append),append:是否追加,默认是false
os = new FileOutputStream(file,true);
//操作(写出)
String values = "world\r\n";//换行
byte[] datas =values.getBytes();//编码 字符-->字节
os.write(datas,0,datas.length);
os.flush();//刷新,避免内容驻留
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(null!=os){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
执行前testFile.txt文件内容:
打印流
true
change
world
执行后内容:
打印流
true
change
world
world
文本字符读取
//字符流的读入,这个类的使用不会导致中英文乱码问题
public static void filesRead(){
File file = new File("fileTest.txt");
Reader reader = null;
try {
reader = new FileReader(file);
//分段读取,实际开发中,一般是几K的读取,1024就是1K,10K=1024*10这样写
char[] flush = new char[4];//缓冲容器
int len = -1;//接收长度
while ((len = reader.read(flush))!=-1){
String str = new String(flush,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(null!=reader){
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
结果:我是按4个来分段的,结果就是不管中英文都算一个。字符流就是对字符进行操作的,不用担心出现乱码。
总额和好
菜市场a
bcde
文本字符写出
public static void filesWriter(){
File file = new File("fileTest.txt");
Writer writer = null;
try {
writer = new FileWriter(file);
//分段读取,实际开发中,一般是几K的读取,1024就是1K,10K=1024*10这样写
String s = "你好fdsf";
writer.write(s,0,s.length());
writer.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(null!=writer){
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
执行前文本内容:
总额和好菜市场abcde
执行后:
你好fdsf