一、流的分类
1.操作数据单位:字节流、字符流
2.数据的流向:输入流、输出流
3.流的角色:节点流、处理流
二、流的体系结构
抽象基类 | 节点流(或文件流) | 缓冲流(处理流的一种) |
---|---|---|
InputStream | FileInputStream(read(byte[] buffer)) | BufferedInputStream |
OutputStream | FileOutputStream(write(byte[] buffer,0,len)) | BufferedOutputStream |
Reader | FileReader(read(char[] cbuf)) | BufferedReader |
Writer | FileWriter(write(char[] cbuf,0,len)) | BufferedWriter |
读文件
import java.io.*;
public class test {
public static void main(String[] args) {
}
public void testFileReader() {
//异常的处理:为了保证流资源一定可以执行关闭操作。需要使用try-catch-finally处理。
//读入的文件一定要存在,否则就会报FileNotFoundException。
FileReader fr = null;
try {
//1.实例化File类的对象,指明要操作的文件
File file = new File("hello.txt"); //该工程下的目录
//2.提供具体的流
fr = new FileReader(file);
//3.数据的读入
//read()返回读入的一个字符,如果达到文件末尾,返回-1
//方式一
int data = fr.read();
while (data != -1) {
System.out.print((char) data);
data = fr.read();
}
/*
方式二
int data;
while((data=fr.read())!=-1){
System.out.print((char) data);
}
*/
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null) {
try {
//4.流的关闭
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//对read()操作升级:使用read的重载方法
public void testFileReader2() {
FileReader fr = null;
try {
//1.实例化File类的对象,指明要操作的文件
File file = new File("hello.txt");
//2.FileReader流的实例化
fr = new FileReader(file);
//3.读入的操作
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf)) != -1) {
for (int i = 0; i < len; i++) {//等同于fr.read(cbuf,0,len)
System.out.print(cbuf[i]);
}
}
//4.资源的关闭
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
写文件
1.输出操作,对应的File可以不存在的。不会报异常
如果不存在,在输出的过程中,会自动创建此文件。
如果存在
如果流使用的构造器是:FileWriter(file,false)对原有文件的覆盖
如果流使用的构造器是:FileWriter(file,true)不会对原有文件的覆盖,在原有文件下继续输出
*/
public void testFileWriter() throws IOException {
FileWriter fw = null;
try {
//1.实例化File类的对象,指明要操作的文件
File file = new File("hello.txt");
//2.提供FileWriter的对象,用于数据的写出
fw = new FileWriter(file);
//3.写出操作
fw.write("I have a dream!\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
//4.流资源的关闭
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
文件的复制
*/
public void testFileReaderWriter() {
FileReader fr = null;
FileWriter fw = null;
try {
File srcFile = new File("hello.txt");
File destFile = new File("hello1.txt");
fr = new FileReader(srcFile);
fw = new FileWriter(destFile);
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf)) != -1) {
fw.write(cbuf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fw != null)
fw.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fr != null)
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/*
非文本的复制
*/
public void testFileInputOutputStream() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File srcfile = new File("hello.*");
File destfile = new File("hello1.*");
fis = new FileInputStream(srcfile);
fos = new FileOutputStream(destfile);
byte[] buffer = new byte[5];
int len;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/*
指定路径下的文件复制
*/
public void testCopy(String srcPath, String destPath) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File srcfile = new File(srcPath);
File destfile = new File(destPath);
fis = new FileInputStream(srcfile);
fos = new FileOutputStream(destfile);
byte[] buffer = new byte[5];
int len;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/*
缓冲流实现非文本文件的复制
*/
public void BufferedStreamTest() {
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
File srcfile = new File("hello.*");
File destfile = new File("hello1.*");
fis = new FileInputStream(srcfile);
fos = new FileOutputStream(destfile);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] buffer = new byte[10];
int len;
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bos != null)
bos.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bis != null)
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/*
缓冲流实现指定路径非文本文件复制
*/
public void testCopyBuffer(String srcPath, String destPath) {
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
File srcfile = new File(srcPath);
File destfile = new File(destPath);
fis = new FileInputStream(srcfile);
fos = new FileOutputStream(destfile);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] buffer = new byte[10];
int len;
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bos != null)
bos.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bis != null)
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/*
缓冲流实现文本文件的复制
*/
public void testCopyBuffer2() {
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(new File("hello.txt")));
bw = new BufferedWriter(new FileWriter(new File("hello.txt")));
//方式一
char[] cbuf = new char[1024];
int len;
while ((len = br.read()) != -1) {
bw.write(cbuf, 0, len);
}
//方式二
String data;
while ((data = br.readLine()) != null) {
bw.write(data);
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
转换流
InputStreamReader:将一个字节的输入流转换为字符的输入流
OutputStreamWriter:将一个字符的输出流转换为字节的输出流
*/
public void test1() throws IOException {
FileInputStream fis = new FileInputStream("hello.txt");
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
char[] cbuf = new char[20];
int len;
while ((len = isr.read(cbuf)) != -1) {
String str = new String(cbuf, 0, len);
System.out.print(str);
}
isr.close();
}
/*
综合使用InputStreamReader和OutputStreamWriter
编码:字符流-->字节流
解码:字节流-->字符流
*/
public void test2() throws IOException {
FileInputStream fis = new FileInputStream(new File("hello.txt"));
FileOutputStream fos = new FileOutputStream(new File("hello.txt"));
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
OutputStreamWriter osw = new OutputStreamWriter(fos, "gbk");
char[] cbuf = new char[20];
int len;
while ((len = isr.read(cbuf)) != -1) {
osw.write(cbuf, 0, len);
}
isr.close();
osw.close();
}
}
图片加密
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class test {
//加密
public void test() throws IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("*.jpg");
fos = new FileOutputStream("*.jpg");
byte[] buffer = new byte[20];
int len;
while ((len = fis.read(buffer)) != -1) {
for (int i = 0; i < len; i++) {
buffer[i] = (byte) (buffer[i] ^ 5);
}
fos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//解密
public void test1() throws IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("*.jpg");
fos = new FileOutputStream("*.jpg");
byte[] buffer = new byte[20];
int len;
while ((len = fis.read(buffer)) != -1) {
for (int i = 0; i < len; i++) {
buffer[i] = (byte) (buffer[i] ^ 5);
}
fos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
统计文件中每个字符出现的次数
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class test {
public void test() {
FileReader fr = null;
BufferedWriter bw = null;
try {
//创建Map数组
Map<Character, Integer> map = new HashMap<>();
//遍历每一个字符,每一个字符出现的次数放到map中
fr = new FileReader("hello.txt");
int count = 0;
while ((count = fr.read()) != -1) {
char ch = (char) count;
if (map.get(ch) == null)
map.put(ch, 1);
else
map.put(ch, map.get(ch) + 1);
}
//把map中的数据存在文件test.txt中
bw = new BufferedWriter(new FileWriter("test.txt"));
//遍历map,再写入数据
Set<Map.Entry<Character, Integer>> entry = map.entrySet();
for (Map.Entry<Character, Integer> entry : entrySet) {
switch (entry.getKey()) {
case ' ':
bw.write("空格" + entry.getValue());
break;
case '\t':
bw.write("tab" + entry.getValue());
break;
case '\r':
bw.write("回车" + entry.getValue());
break;
case '\n':
bw.write("换行" + entry.getValue());
break;
default:
bw.write(entry.getKey() + "=" + entry.getValue());
break;
}
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}