------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
一、概述
IO流用来处理设备之间的数据传输。按操作的数据分为:字节流和字符流;按流向分为:输入流和输出流。
在JAVA中字节流的抽象基类为:OutputStream和InputStream;字符流的抽象基类为:Reader和Writer。
二、Reader字符读取流,读取文字类文件。
示例一:
import java.io.FileReader;
import java.io.IOException;
public class FileReaderDemo {
public static void main(String[] args) throws IOException {
//创建一个文件读取流对象, 并指定要读取的文件
//要保证文件的存在, 否则会报FileNotFindExcption
FileReader fr = new FileReader("D:/dome2.txt");
//read()读取字符, 每次读取一个, 读完了返回-1
int ch = 0;
while (-1 != (ch = fr.read())) {
System.out.println((char)ch);
}
fr.close();
}
}
示例二:
import java.io.FileReader;
import java.io.IOException;
/*
* 通过字符数组进行读取
*/
public class FileReaderDemo2 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("D:/dome2.txt");
// 定义数组, 用于存储读到的字符
char[] buffer = new char[512];
// int num = fr.read(buffer); 返回读取的个数
StringBuffer sb = new StringBuffer();
int num = 0;
while (-1 != (num = fr.read(buffer))) {
sb.append(new String(buffer, 0, num));
}
fr.close();
System.out.println(sb.toString());
}
}
示例三:
//将一个.java文件读取出来, 并打印到控制台
public class FileReaderTest {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("D:/CalendarDemo.java");
char[] buffer = new char[5];
int num =0 ;
StringBuffer sb = new StringBuffer();
while(-1 != (num = fr.read(buffer))){
sb.append(new String(buffer,0,num));
}
System.out.println(sb.toString());
fr.close();
}
}
三、Writer字符输出流。
示例一:
public class FileWriterDemo {
public static void main(String[] args) throws IOException {
//创建一个FileWriter对象, 该对象一被初始化就必须明确被操作的文件
//该文件被创建在指定的目录下, 如果该目录下已经有同名的文件, 将会被覆盖
FileWriter fw = new FileWriter("D:/dome.txt");
//将数据写入到流中
fw.write("313111");
//刷新流对象换缓冲数据, 将数据刷到目的地中
fw.flush();
//刷新缓冲数据, 并且将流关闭
fw.close();
}
}
示例二:
/*
* 文件续写
*/
public class FileWriterDemo3 {
public static void main(String[] args) {
FileWriter fw = null;
try {
//传递true参数, 表示不会覆盖已有的文件, 并且在文件的后面续写数据
fw = new FileWriter("D:/dome2.txt", true);
//windows下换行
fw.write("121\r\nmmmmm");
fw.flush();
} catch (Exception e) {
System.out.println(e.toString());
} finally {
try {
if (null != fw)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
四、为了提高文件输入和输出流的效率, Java提供了一个增强的功能,那就是流缓冲区, BufferedReader和BufferedWriter。
示例一:
//字符读取流缓冲区
public class BufferedReaderDemo {
public static void main(String[] args) throws IOException {
//创建一个读取流对象
FileReader fr = new FileReader("D:/buffer.txt");
//将字符读取流对象作为参数传给缓冲区对象
BufferedReader br = new BufferedReader(fr);
String line = null;
StringBuffer sb = new StringBuffer();
//readLine() //读取一行文本, 只会读取回车之前的字符, 不会返回回车
//无论是读取一行还是多个字符, 最终还是使用的read()方法, 一个一个的读取
while (null != (line = br.readLine())) {
sb.append(line);
}
System.out.println(sb.toString());
br.close();
}
}
示例二:
//缓冲区是为了提高流的操作效率
//所以在创建缓冲区之前必须要有流对象
public class BufferedWriterDemo {
public static void main(String[] args) throws IOException {
//创建一个字符写入流
FileWriter fw = new FileWriter("D:/buffer.txt");
//为了提高写入流的效率, 加入缓冲区
//把流对像作为参数传给缓冲区构造函数即可
BufferedWriter bw = new BufferedWriter(fw);
bw.write("1aad");
for(int i=0;i<3;i++){
bw.write("adad" + i);
bw.newLine(); //换行, 兼容各个系统
bw.flush();
}
bw.flush();
//关闭缓冲区, 就是在关闭缓冲区中的流对象
bw.close();
fw.close();
}
}
示例三:
/*
* 从C盘拷贝一个文件到D盘
* 运用BufferedReader和BufferedWriter
*/
public class CopyTextByBuffered {
public static void main(String[] args) {
String srcPath = "C:/FaceProv.log";
String descPath = "D:/FaceProv.log";
CopyTextByBuffered.copy(srcPath, descPath);
}
public static void copy(String srcPath, String descPath) {
FileReader fr = null;
FileWriter fw = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
fr = new FileReader(srcPath);
fw = new FileWriter(descPath);
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
int len = 0;
char[] buffer = new char[20];
while (-1 != (len = br.read(buffer))) {
bw.write(buffer, 0, len);
bw.flush();
}
} catch (Exception e) {
throw new RuntimeException("拷贝失败 !");
} finally {
if (null != br)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
if (null != bw)
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
五、FileOutputStream字节输出流。
示例:
//将一个字符串写入到D盘文件中
public static void writer() throws IOException{
FileOutputStream fos = new FileOutputStream("D:/fos.txt");
fos.write("aaad".getBytes());
fos.close();
}
六、FileInputStream字节输入流。
示例:
//一个一个读
public static void reader() throws IOException{
FileInputStream fis = new FileInputStream("D:/fos.txt");
int ch =0;
while(-1 != (ch = fis.read())){
System.out.println((char)ch);
}
fis.close();
}
public static void reader2() throws IOException{
FileInputStream fis = new FileInputStream("D:/fos.txt");
byte[] b = new byte[20];
int len = 0;
while(-1 != (len = fis.read(b))){
System.out.println(new String(b,0,len));
}
fis.close();
}
public static void reader3() throws IOException
{
FileInputStream fis = new FileInputStream("D:/fos.txt");
int size = fis.available(); //返回文件中字节数大小
byte[] b = new byte[size]; //定义一个刚好大小的缓冲区, 不用在循环, 此方式不适合读取大文件
fis.read(b);
System.out.println(new String(b));
fis.close();
}
七、同样的字节流也有缓冲区的,就是BufferedOutputStream和BufferedInputStream,下面的示例就是利用字节流的缓冲区拷贝一个
mp3文件。
示例:
//拷贝一个MP3文件
public static void copyMp3(String srcPath, String descPath){
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(descPath));
bis = new BufferedInputStream(new FileInputStream(srcPath));
int b = 0;
while(-1 != (b = bis.read())){
bos.write(b);
}
} catch (Exception e) {
throw new RuntimeException("拷贝失败");
}finally {
if(null != bis)
try {
bis.close();
} catch (IOException e) {
throw new RuntimeException("关闭读取流失败");
}
if(null != bos)
try {
bos.close();
} catch (IOException e) {
throw new RuntimeException("关闭写入流失败");
}
}
}
八、InputStreamReader和OutputStreamWriter转换流
示例:
//readLine()是字符流BufferedReader类中的方法, 键盘录入的read()则是字节流InputStream类中的方法
//可以通过InputStreamReader类来转换
//需要注意的是, 只有转换流才可以指定字符的编码集
public class TransStreamDemo {
public static void main(String[] args) throws IOException {
toPrint2();
}
public static void myPrint() throws IOException {
// 获取键盘录入对象
InputStream in = System.in;
// 将字节流转换成字符流, 需要使用转换流
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
OutputStream os = System.out;
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String line = null;
while (null != (line = br.readLine())) {
if ("over".equals(line))
break;
// System.out.println(line);
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
// 将控制台的录入, 写入到文件中
public static void toFile() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:/out.txt")));
String line = null;
while (null != (line = br.readLine())) {
if ("over".equals(line))
break;
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
// 将文件中的数据, 打印到控制台
public static void toPrint() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("D:/out.txt")));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String line = null;
while (null != (line = br.readLine())) {
if ("over".equals(line))
break;
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
// 将文件中的数据, 打印到控制台, 通过改变源
public static void toPrint2() throws IOException {
//设置标准输入设备
System.setIn(new FileInputStream("D:/out.txt"));
//设置输出设备
//System.setOut(new PrintStream("D:/setout.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String line = null;
while (null != (line = br.readLine())) {
if ("over".equals(line))
break;
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
}