IO 流主要是用来处理设备之间的数据传输,Java对于数据的操作都是通过流实现,而java用于操作流的对象都在IO包中。
IO流常用的基类:
* InputStream OutputStream
字符流的抽象基类:
* Reader Writer
由上面四个类派生的子类名称都是以其父类名作为子类的后缀:
如:FileReader、FileWriter、FileInputStream、FileOutputStream
当然还有一些缓冲流:
如:BufferedReader、BufferedWriter、BufferedInputStream 、BufferedOutputStream
缓冲的作用都是为了提高效率,拓展功能!
IO 流常用的操作方法:
// 输入字节流
public static void readTest4() throws IOException{
File file = new File("F:\\a.txt"); //找到目标文件
FileInputStream fis = new FileInputStream(file); //建立数据输入通道
int length = 0; //为什么要用length ?下面会提到
byte[] buf = new byte[4]; //建立缓冲数组,一次读取4个长度,最后如不够4个长度,只读取剩下的长度内容
while((length = fis.read(buf))!=-1){
System.out.print(new String(buf,0,length));
}
fis.close();
}
// 输入字符流
public static void readTest1() throws IOException{
File file = new File("E:\\a.txt");
FileReader fr = new FileReader(file);
int length=0;
char[] buf = new char[1024];
while((length=fr.read(buf))!=-1){
System.out.print(new String(buf,0,length));
}
//缓冲输入字节流
public static void readTest1() throws IOException{
File file = new File("F:\\a.txt"); //找到目标文件
FileInputStream fis = new FileInputStream(file); //建立数据输入通道
BufferedInputStream bis = new BufferedInputStream(fis); //建立缓冲输入字节流
int content = 0;
while((content=bis.read())!=-1){
System.out.print((char)content);
}
bis.close();
}
//缓冲输入字符流
public static void readTest1() throws IOException{
File file = new File("E:\\a.txt"); //找到目标文件
FileReader fr = new FileReader(file); //建立数据输入通道
BufferedReader br = new BufferedReader(fr);
String line = null;
while((line=br.readLine())!=null){ //readline一次读取一行
System.out.print(line);
}
br.close();
}
//输出字节流
public static void writeTest2() throws IOException{
File file = new File("F:\\a.txt"); //找到目标文件,如果不存在,自动创建,如果存在,先清空数据再写入
FileOutputStream fos = new FileOutputStream(file,true); //建立数据输出通道,加true表示不清空,在原文件后面添加
String data="hello word";
fos.write(data.getBytes());
fos.close();
}
//输出字符流
public static void writerTest1() throws IOException{
File file = new File("E:\\a.txt");
FileWriter fw = new FileWriter(file);
String data="今天很好!";
fw.write(data);
fw.close();
}
//缓冲输出字节流
public static void writeTest1() throws IOException{
File file = new File("F:\\a.txt"); //找到目标文件
FileOutputStream fos = new FileOutputStream(file); //建立数据输入通道
BufferedOutputStream bos = new BufferedOutputStream(fos); //建立缓冲输入字节流
bos.write("hello world".getbytes());
bos.close(); //只有调用close方法才会写到硬盘,否则只是写到内部的字节数组中
}
//缓冲输出字符流
public static void writerTest1() throws IOException{
File file = new File("E:\\a.txt"); //找到目标文件
FileWriter fw = new FileWriter(file); //建立数据输出通道
BufferedWriter bw = new BufferedWriter(fw);
bw.write("大家好!");
bw.close();
}
至于为什么要用length ?
public static void runio() throws IOException{
File file=new File("E:\\a.txt");
FileInputStream fis=new FileInputStream(file);
byte[] bytes= new byte[4];
int length=-1;
while ((length=fis.read(bytes,0,2))!=-1){
//虽然数组长度为4,但是这里我们设置了2所以每次输出2
System.out.println(length);
//因为每次得到的是新的数组,所以每次都是新数组的"0-length"
System.out.println(new String(bytes,0,length));
}
}
//上述只是读数据,当写数据时:
public static void runio() throws IOException{
File file=new File("E:\\a.txt");
FileInputStream fis=new FileInputStream(file);
FileOutputStream fos = new FileOutputStream("E:\\b.txt");
byte[] bytes= new byte[4];
int length=-1;
while ((length=fis.read(bytes,0,2))!=-1){
//往流里边写入缓冲字节数组中的所有内容,,如果不使用length,不满整个数组长度的”空余内容”也会加入
fos.write(bytes,0,length);
}
}