JAVA学习历程记录(七)
IO流
传输数据的一套机制
基本概念
输入输出参考系是程序内存
input 输入流:从硬盘中读取数据到内存
output 输出流:从内存中写入数据到硬盘
分类
字节流(InputStream,OutputStream):什么文件都可以处理
字符流:只能处理文本类型文件
FileWriter
常用API
FileWriter fw = new FileWriter("D:\\",true)
//默认提供一个缓冲区
//第二个参数代表追加写入,传入true就可以在原内容后面加内容
fw.write("")
//写入数据
fw.flush()
//冲刷缓冲区
fw.close
//关闭资源,关闭资源时会自动冲刷一次缓冲区
FileWriter 异常处理
package iopratice;
import java.io.FileWriter;
import java.io.IOException;
public class FileExceptionDemo {
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("G://111.txt");
fw.write("异常检测");
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fw != null) {
try {
fw.close();
}
catch (IOException e) {
e.printStackTrace();
}finally {
fw = null;
}
}}
}
}
jdk1.7特性
在try中的对象,可以被自动关闭
public class TryWith {
public static void main(String[] args) throws IOException {
demo();
}
public static void demo() throws IOException {
//try括号中的必须是创建对象,不能是方法的传递
try(FileWriter fw = new FileWriter("E://222.txt")){
fw.write("trywith测试");
}
}
}
FileReader
常用API
//创建对象
FileReader fr = new FileReader(E://111.txt)
//遍历读取
int c;
while((c = fr.read()) ! = -1){
System.out.println((char)c);
//定义数组作为缓冲区
char[] chars = new Char[1024];
//带缓冲区的遍历读取
int c;
char[] chars = new Char[1024];
while((c = fr.read(chars)) ! = -1){
System.out.println(new String(chars,0,c);
练习
复制文件
public class CopyDemo {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("E://487.txt");
FileWriter fw = new FileWriter("E://神雕.txt");
int len = 0;
char[] chars = new char[1024];
while ((len = fr.read(chars)) != -1){
fw.write(new String(chars,0,len));
}
fr.close();
fw.close();
}
}
BufferedReader和BufferedWriter
默认添加了缓冲区,效率更高
BufferedReader特有方法:readLine()读取一行字符串,不读取换行符
BufferedWriter特有方法:newLine()换行
构建方法使用了装饰设计模式
练习
统计空间中的代码行数
public class Calcute {
static int count = 0;
public static void main(String[] args) throws IOException {
File file = new File("E:\\实训");
get(file);
System.out.println(count);
}
public static void get(File file) throws IOException {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
get(f);
}
} else if (file.getName().endsWith(".java")) {
BufferedReader br = new BufferedReader(new FileReader(file));
int len;
while (br.readLine() != null) {
count++;
}
br.close();
}
字节流
练习
用字节流复制文件
public class StreamP {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("E:\\实训\\JDK 8.0 EN API.chm");
FileOutputStream fos = new FileOutputStream("E:\\实训\\啦啦啦.chm");
int len;
byte[] by = new byte[1024];
while ((len = fis.read(by)) != -1){
fos.write(by);
}
fis.close();
}
}
读取内存中的流
StringReader
StringWriter
CharArrayReader
CharArray
转换流
转换输出流OutputStreamWriter
转换输入流InputStreamReader
public class Trans {
public static void main(String[] args) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("E:\\实训\\day17\\视频\\index.cfg"));
osw.write("asdf");
osw.close();
InputStreamReader isr = new InputStreamReader(new FileInputStream("E:\\实训\\day17\\视频\\index.cfg"));
char[] c = new char[5];
int len = 0;
while ((len = isr.read(c)) != -1){
System.out.println(new String(c,0,len));
}
isr.close();
}
}
系统流
System.out 标准输出流
System.in 标准输入流(字节流)
System.err 标准错误流
练习
用已知的流从控制台获取一行数据
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
bf.readLine();
打印流
只有输出流,没有输入流
PrintStream ps = new PrintStream("E:\\487.txt");
ps.write("abc".getBytes());
// ps.print("asd");
ps.println("afaasfasgga");
ps.close();
合并流
将两个或者多个流合并成一个流,只有字节输入流
合并数据的时候格式要求一致,要求合并的数据编码一致
public static void main(String[] args) throws IOException {
Vector<FileInputStream> v = new Vector<>();
FileInputStream fis1 = new FileInputStream("E:\\a.txt");
FileInputStream fis2 = new FileInputStream("E:\\b.txt");
FileInputStream fis3 = new FileInputStream("E:\\c.txt");
v.add(fis1);
v.add(fis2);
v.add(fis3);
Enumeration<FileInputStream> e = v.elements();
SequenceInputStream sis = new SequenceInputStream(e);
FileOutputStream fos = new FileOutputStream("E:\\合并.txt");
byte[] bytes = new byte[10];
int len;
while ((len = sis.read(bytes)) != -1){
fos.write(bytes,0,len);
}
fos.close();
sis.close();
}
随机获取流
操作模式
R | 读 |
---|---|
RW | 读写 |
RWS | 读写并写入硬盘 |
RWD | 读写并写入硬盘,同步保存 |