io流详解
io流详解
参考文献
流:数据在两设备间的传输称为流,流的本质是数据传输
IO流的分类
根据处理数据类型的不同分为:字符流和字节流
根据数据流向不同分为:输入流和输出流
字符流和字节流(字符流本质其实是基于字节流读取时,去查了指定的码表)
字节流和字符流的区别:
(1)读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。
(2)处理对象不同:字节流能处理所有类型的数据(如图片、avi等),而字符流只能处理字符类型的数据。
(3)字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的;而字符流在操作的时候下后是会用到缓冲区的,是通过缓冲区来操作文件
结论:优先选用字节流。
输入流和输出流
对输入流只能进行读操作,对输出流只能进行写操作,程序中需要根据待传输数据的不同特性而使用不同的流。
java流类图:
输入字节流InputStream
InputStream 是所有的输入字节流的父类,它是一个抽象类。
ByteArrayInputStream
StringBufferInputStream
FileInputStream 是三种基本的介质流,它们分别从Byte 数组、StringBuffer、和本地文件中读取数据
【案例】字节流读取文件终极版
/**
* 字节流
*读文件
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
/**
*比如说要在temp目录下建立一个test.txt文件,在Windows下应该这么写:
*File file1 = new File ("C:\tmp\test.txt");
*在Linux下则是这样的:
*File file2 = new File ("/tmp/test.txt");
*如果要考虑跨平台,则最好是这么写:
*File myFile = new File("C:" + File.separator + "tmp" + File.separator, "test.txt");
**/
File f=new File(fileName);//创建文件对象
InputStream in=new FileInputStream(f);//创建输入流对象
byte[] b=new byte[1024];//设定了字节数组大小
int count =0;
int temp=0;
while((temp=in.read())!=(-1)){//输入流对象读取不到对象的时候会返回-1
b[count++]=(byte)temp;
}
in.close();//关闭输入流
System.out.println(new String(b));//控制台输出文件内容
}
}
输出字节流OutputStream
定义和结构说明:
IO 中输出字节流的继承图可见上图,可以看出:
OutputStream 是所有的输出字节流的父类,它是一个抽象类。
ByteArrayOutputStream、FileOutputStream是两种基本的介质流,它们分别向Byte 数组、和本地文件中写入数据。
【案例】向文件中写入字符串
/**
* 字节流
* 向文件中追加新内容:
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
OutputStream out =new FileOutputStream(f,true);//true表示追加模式,否则为覆盖
String str="Rollen";
//String str="\r\nRollen"; 可以换行
byte[] b=str.getBytes();
for (int i = 0; i < b.length; i++) {
out.write(b[i]);
}
out.close();
}
}
【复制文件,in和out结合】
/**
* 文件的复制
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
if(args.length!=2){
System.out.println("命令行参数输入有误,请检查");
System.exit(1);
}
File file1=new File(args[0]);
File file2=new File(args[1]);
if(!file1.exists()){
System.out.println("被复制的文件不存在");
System.exit(1);
}
InputStream input=new FileInputStream(file1);
OutputStream output=new FileOutputStream(file2);
if((input!=null)&&(output!=null)){
int temp=0;
while((temp=input.read())!=(-1)){
output.write(temp);
}
}
input.close();
output.close();
}
}
字符输入流Reader
定义和说明:
Reader 是所有的输入字符流的父类,它是一个抽象类。
CharReader、StringReader是两种基本的介质流,它们分别将Char 数组、String中读取数据
BufferedReader是一个装饰器,它和其子类负责装饰其它Reader 对象。
InputStreamReader 是一个连接字节流和字符流的桥梁,它将字节流转变为字符流。
FileReader可以说是一个达到此功能、常用的工具类,在其源代码中明显使用了将FileInputStream 转变为Reader 的方法。我们可以从这个类中得到一定的技巧。Reader 中各个类的用途和使用方法基本和InputStream 中的类使用一致。
【案例BufferedReader】
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 使用缓冲区从键盘上读入内容
* */
public class BufferedReaderDemo{
public static void main(String[] args){
BufferedReader buf = new BufferedReader(
newInputStreamReader(System.in));
String str = null;
System.out.println("请输入内容");
try{
str = buf.readLine();
}catch(IOException e){
e.printStackTrace();
}
System.out.println("你输入的内容是:" + str);
}
}
【案例Scanner】
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
*Scanner的小例子,从文件中读内容
* */
public class ScannerDemo{
public static void main(String[] args){
File file = new File("d:" + File.separator +"hello.txt");
Scanner sca = null;
try{
sca = new Scanner(file);
}catch(FileNotFoundException e){
e.printStackTrace();
}
String str = sca.next();
System.out.println("从文件中读取的内容是:" + str);
}
}
字符输出流writer
Writer 是所有的输出字符流的父类,它是一个抽象类。
CharArrayWriter、StringWriter 是两种基本的介质流,它们分别向Char 数组、String 中写入数据。
OutputStreamWriter 是OutputStream 到Writer 转换的桥梁,它的子类FileWriter 其实就是一个实现此功能的具体类
【案例Scanner】向文件中写入数据
/**
* 字符流
* 写入数据
* */
import java.io.*;
class hello{
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
Writer out =new FileWriter(f);
String str="hello";
out.write(str);
out.close();
}
}