一.IO流概述
IO(input/output):输入和输出指的是某个设备或环境进行数据的输入或者输出,例如键盘的输入,例如显示器就是一种输出设备,输出图像

IO在Java从输入输出角度分类:
输入流:从文件输入或者键盘输入都是输入流
输出流:向文件写入数据
IO在Java从数据的角度来分类:
字符流:
文本,我们能读懂的都可以认为是字符流
字符输入流的超类:
Reader 子类:FileReader,BufferedReader
字符输出流的超类:
Writer 子类:FileWriter,BufferedWriter
字节流:
二进制的数据,这种数据一般我们都读不懂
字节输入流的超类:
InputStream 子类:FileInputStream
OutputStream 子类:FileOutputStream
1. 字符流
使用字符流向文件输入helloworld
分析:
步骤:
1 创建文件
2 创建输出流对象
3 把流对象指向文件
4 释放资源
1.输出流
FileWriter构造器

package com.rl.io.cha;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class IOCharDemo {
public static void main(String[] args) {}
public static void writerFile(){
//创建文件
File file = new File("a.txt");
FileWriter fw = null;
try {
//创建输出流的对象
fw = new FileWriter(file);
//把内容写入文件
fw.write("helloworld");
//清空缓冲区 把内容写入到文件中
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fw != null){
try {
//释放资源
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void writerFile1(){
FileWriter fw = null;
try {
//创建输出流的对象
fw = new FileWriter("b.txt");
//把内容写入文件
fw.write("helloworld");
//清空缓冲区 把内容写入到文件中
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fw != null){
try {
//释放资源
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
向文件中写入100个helloworld
package com.rl.io.cha;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class IOCharDemo1 {
public static void main(String[] args) {
writerFile1();
}
public static void writerFile1(){
FileWriter fw = null;
try {
//创建输出流的对象
fw = new FileWriter("b.txt");
for(int i=0;i<100;i++){
//把内容写入文件
fw.write("helloworld");
if(i%10 == 0){
//清空缓冲区 把内容写入到文件中
fw.flush();
}
}
//把缓冲区的残留内容写入到文件中
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fw != null){
try {
//释放资源
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
FileWriter的追加构造器

package com.rl.io.cha;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class IOCharDemo2 {
public static void main(String[] args) {
writerFile1();
}
public static void writerFile1(){
FileWriter fw = null;
try {
//创建输出流的对象 追加
fw = new FileWriter("b.txt",true);
// fw = new FileWriter(new File("b.txt"),true);
for(int i=0;i<100;i++){
//把内容写入文件
fw.write("helloworld");
if(i%10 == 0){
//清空缓冲区 把内容写入到文件中
fw.flush();
}
}
//把缓冲区的残留内容写入到文件中
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fw != null){
try {
//释放资源
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
输出换行
把文本写入文件中 \n代表换行
问题是不同环境下换行的方式也不一样
在windows下换行需要使用\r \n
Linux下换行需要使用\n
MACOS下换行使用\r
package com.rl.io.cha;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class IOCharDemo3 {
public static void main(String[] args) {
writerFile1();
}
public static void writerFile1(){
FileWriter fw = null;
try {
//创建输出流的对象
fw = new FileWriter("b.txt");
for(int i=0;i<100;i++){
//把内容写入文件 为了兼容记事本,我们建议使用\r\n
fw.write("helloworld" + i + "\r\n");
if(i%10 == 0){
//清空缓冲区 把内容写入到文件中
fw.flush();
}
}
//把缓冲区的残留内容写入到文件中
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fw != null){
try {
//释放资源
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
FileWriter的写入方法

package com.rl.io.cha;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class IOCharDemo4 {
public static void main(String[] args) {
writerFile1();
}
public static void writerFile1(){
FileWriter fw = null;
try {
//创建输出流的对象
fw = new FileWriter("test.txt");
char[] chs = {'a','b','c','d'};
fw.write(chs);
//把一部分的字符的数组写入文件中 第一个参数是字符数组,第二个是开始索引,第三个是从开始索引开始取得字符串的长度
fw.write(chs,1,3);
//通过int值写入相应的字符值
fw.write(100);
fw.write("100");
//把一部分的字符的数组写入文件中 第一个参数是字符数组,第二个是开始索引,第三个是从开始索引开始取得字符串的长度
fw.write("helloworld",2,2);
//把缓冲区的残留内容写入到文件中
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fw != null){
try {
//释放资源
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
2.输出流
1.输入流构造器

范例:读取文件helloworld.txt的文件内容并输入至控制台
分析:
创建输入流对象
读取数据
关闭输入流对象
代码:
package com.rl.io.cha.reader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class ReaderDemo {
public static void main(String[] args) {
readFile();
}
public static void readFile(){
FileReader fr = null;
try {
//创建文件读取对象
fr = new FileReader("helloworld.txt");
//返回的是字符的ascii码
/*int read = fr.read();
System.out.println((char)read);*/
int read;
//如果文本读取完毕,最后没有字符会返回-1
while((read = fr.read()) != -1){
System.out.println((char)read);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(fr != null){
try {
//释放资源
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2.FilterReader的read方法

package com.rl.io.cha.reader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class ReaderDemo1 {
public static void main(String[] args) {
readFile();
}
public static void readFile(){
FileReader fr = null;
try {
//创建文件读取对象
fr = new FileReader("helloworld.txt");
//返回的是字符的ascii码
/*int read = fr.read();
System.out.println((char)read);*/
/* //创建一个字符数组
char[] chs = new char[5];
//读取5个字符放到字符数组中 返回的数值为读取到的字符数量
int num = fr.read(chs);
String str = new String(chs);
System.out.println(num);
System.out.println(str);
System.out.println("------------------");
num = fr.read(chs);
str = new String(chs,0,num);
System.out.println(num);
System.out.println(str);
System.out.println("------------------");
num = fr.read(chs);
str = new String(chs,0,num);
System.out.println(num);
System.out.println(str);*/
// 如果num1为-1的话 表示文件已经读完
int num1 = -1;
//循环条件的判断的边界是fr.read(chs1) 如果返回-1表示文件已读完
char[] chs1 = new char[1024];
while((num1 = fr.read(chs1)) != -1){
System.out.println(new String(chs1,0,num1));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(fr != null){
try {
//释放资源
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
3.使用字符流来进行文本文件的拷贝
把一个Java文件拷贝到项目的根目录
分析:
1创建字符输入流对象
2创建字符输出流对象
3把输入流输入的数据写入输出流中
4关闭资源
代码:
package com.rl.io.cha.copyFile;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
public class CopyFile {
public static void main(String[] args) {
copy1();
}
/**
* copy一个Java文件方式一
*/
public static void copy(){
Reader fr = null;
Writer wr = null;
try {
//创建文件读取对象
fr = new FileReader("src/com/rl/io/cha/reader/ReaderDemo.java");
//创建文件写入对象
wr = new FileWriter("ReaderDemo.java");
int num = -1;
//一个字符一个字符的读取
while((num = fr.read()) != -1){
wr.write(num);
}
System.out.println("done");
} catch (Exception e) {
e.printStackTrace();
}finally {
//释放资源
if(wr != null){
try {
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fr != null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* copy一个Java文件方式二
*/
public static void copy1(){
Reader fr = null;
Writer wr = null;
try {
//创建文件读取对象
fr = new FileReader("src/com/rl/io/cha/reader/ReaderDemo.java");
//创建文件写入对象
wr = new FileWriter("ReaderDemo.java");
//定义每次读取长度的对象
int len = -1;
//定义存储读取内容的数组
char[] chs = new char[1024];
//当len不等于-1就一直读取
while((len = fr.read(chs)) != -1){
//写入文件
wr.write(chs,0,len);
}
System.out.println("done");
} catch (Exception e) {
e.printStackTrace();
}finally {
//释放资源
if(wr != null){
try {
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fr != null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
3.高效缓存区输入输出流BufferedReader/BufferWriter
将写入或者写出的文本缓存至缓存当中,实现高效的读写。
BufferedReader
从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
构造器:
BufferReader(Reader in)
创建一个使用默认大小输入缓冲区的缓冲字符输入流。
BufferedWriter
将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
构造器:
BufferWriter(Writer out)
创建一个使用默认大小输出缓冲区的缓冲字符输出流。
1. 使用高效缓冲区流来向文件写入内容
注意BufferedWriter没有追加构造器
package com.rl.io.cha.buffer;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterDemo {
public static void main(String[] args) {
write();
}
/**
* 使用高效缓冲区流向d.txt文件中写入一个 java太好学了真简单
*/
public static void write(){
//创建文件输出流
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter("d.txt");
//创建高效缓冲区流对象
bw = new BufferedWriter(fw);
//写入内容
bw.write(" java太好学了真简单");
//刷新流的缓冲
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭缓冲区流 自动会关闭fileWriter
if(bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2.高效缓冲区输出新特性newLine()方法换行
package com.rl.io.cha.buffer;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterDemo1 {
public static void main(String[] args) {
write();
}
/**
* 使用高效缓冲区流向d.txt文件中写入一个 java太好学了真简单
*/
public static void write(){
//创建文件输出流
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter("d.txt");
//创建高效缓冲区流对象
bw = new BufferedWriter(fw);
for(int i=0;i<10;i++){
//写入内容
bw.write(" java太好学了真简单");
//换行符 相当于\r\n
bw.newLine();
}
//刷新流的缓冲
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭缓冲区流 自动会关闭fileWriter
if(bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
3. 使用高效缓冲区输入流读取文件内容
package com.rl.io.cha.buffer;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedReaderDemo {
public static void main(String[] args) {
read1();
}
/**
* 使用高效缓冲区流读取文件
*/
public static void read(){
//创建文件输出流
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader("d.txt");
//创建高效缓冲区流对象
br = new BufferedReader(fr);
int num = -1;
while((num = br.read()) != -1){
System.out.print((char)num);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭缓冲区流 自动会关闭fileWriter
if(br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 使用高效缓冲区流读取文件第二种方式
* 建议使用这个方式读取文件
*/
public static void read1(){
//创建文件输出流
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader("d.txt");
//创建高效缓冲区流对象
br = new BufferedReader(fr);
char[] chs = new char[1024];
int num = -1;
while((num = br.read(chs)) != -1){
System.out.print(new String(chs,0,num));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭缓冲区流 自动会关闭fileWriter
if(br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
4.高效缓冲区输入流新特性,读取一行readLine()
package com.rl.io.cha.buffer;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedReaderDemo1 {
public static void main(String[] args) {
read1();
}
/**
* 使用高效缓冲区流读取文件第二种方式
* 建议使用这个方式读取文件
*/
public static void read1(){
//创建文件输出流
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader("d.txt");
//创建高效缓冲区流对象
br = new BufferedReader(fr);
//使用高效输入流可以直接读取一行的数据
String line = null;
while((line = br.readLine()) != null){
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭缓冲区流 自动会关闭fileWriter
if(br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
5.使用高效缓冲区复制文件
package com.rl.io.cha.copyFile;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
public class CopyFile1 {
public static void main(String[] args) {
copy1();
}
/**
* copy一个Java文件方式一
*/
public static void copy(){
BufferedWriter bw = null;
BufferedReader br = null;
try {
//创建文件读取对象
br = new BufferedReader(new FileReader("src/com/rl/io/cha/reader/ReaderDemo.java"));
//创建文件写入对象
bw = new BufferedWriter(new FileWriter("ReaderDemo.java"));
String line = null;
while((line = br.readLine()) != null){
bw.write(line);
}
System.out.println("done");
} catch (Exception e) {
e.printStackTrace();
}finally {
//释放资源
if(bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* copy一个Java文件方式二
*/
public static void copy1(){
Reader fr = null;
Writer wr = null;
try {
//创建文件读取对象
fr = new FileReader("src/com/rl/io/cha/reader/ReaderDemo.java");
//创建文件写入对象
wr = new FileWriter("ReaderDemo.java");
//定义每次读取长度的对象
int len = -1;
//定义存储读取内容的数组
char[] chs = new char[1024];
//当len不等于-1就一直读取
while((len = fr.read(chs)) != -1){
//写入文件
wr.write(chs,0,len);
}
System.out.println("done");
} catch (Exception e) {
e.printStackTrace();
}finally {
//释放资源
if(wr != null){
try {
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fr != null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
5.总结:
FileReader:
构造器:
FileReader(File file)
FileReader(String fileName)
方法:read()读一个字符
read(char [] bchs)读出bchs.length的字符
read(char [],int off,int len)读取字符数组长度的一部分
********************************************************************
FileWriter:
构造器:
FileWriter(File file)
FileWriter(String fileName)
FileWriter(File file,Boolean append)
FileWriter(String fileName,Boolean append)
方法:writer(int a):写入一个字符
writer(char [] bchs) 写入字符数组长度的字符
writer(char [],int off,int len)写入指定字符长度的数组
writer(String str)写入字符串
writer(String ,int off,int len)写入指定字符串数组的一部分
********************************************************************
BufferReader:
构造器:
BufferReader(Reader in)
方法:read()读一个字符
read(char [] bchs)读出bchs.length的字符
read(char [],int off,int len)读取字符数组长度的一部分
readLine():读取一行
********************************************************************
BufferWriter:
构造器:
BufferWriter(Writer out)
方法:writer(int a):写入一个字符
writer(char [] bchs) 写入字符数组长度的字符
writer(char [],int off,int len)写入指定字符长度的数组
writer(String str)写入字符串
writer(String ,int off,int len)写入指定字符串数组的一部分len)读取字符数组长度的一部分
newLine():换行,相当于\r\n
网友评论