近期学了IO文件输入流和输出流的内容,现在梳理一下
1、字节文件输入流(InputStream)--------从源文件中逐字节读取文件
package cn.sxt.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* 第一个IO程序,实现读取文件数据流的功能
* 1、创建源:这里选取的源头文件是文本文件
* 2、选择流:选择读取的类型流InputStream,该类是一个抽象类,不能实例化,所以需使用其子类
* FileInputSream进行实例化
* 3、操作:实现读取的操作(输入流.read()读取一个字节)
* 4、释放资源
* @author yin
*
*/
public class TestIO01 {
public static void main(String[] args){
//1、创建源:源头文件的路径使用了绝对路径
File file =new File("D:/java program/IO/src/cn/sxt/io/abc.txt");
//2、选择流,使用InputStream流,实现按字节逐字节的读取
InputStream is =null;
try {
is = new FileInputStream(file);
//3.实现读取操作
try {
int temp;
while((temp=is.read())!=-1){//文件的末尾读取到的不是数据而是-1
System.out.println((char)temp);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{//不论是否出现异常,最终都要执行释放资源的操作
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
2、字节文件输入流(InputStream)-----------一次读取多个字节
package cn.sxt.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* 实现一段一段地读取内容,即一次读取多个字节
* 1、创建源文件
* 2、选择流
* 3、操作:先定义byte数组,用于存放每次读取出来的内容
* 读取操作:使用到了 数据流.read(byte[]a) 函数
* 4、释放资源
* @author Administrator
*
*/
public class TestIO03 {
public static void main(String[] args){
//1、创建流
File file = new File("D:/java program/IO/src/cn/sxt/io/hello.txt");
//2、为文件选择流
InputStream is =null;
try {
is = new FileInputStream(file);
//3、操作:一段一段的读取文件,按字节读到缓冲数组flush中
//使用 int read(byte[] flush)
//byte[] flush = new byte[3];缓冲数组长度为3,每次至多读取3个字符
byte[] flush = new byte[1024*10];//计算机中常按照字节数来读取文件内容
int length = -1;//使用length记录每次实际读到的字节数,用于解码时做参数
try {
while((length=is.read(flush))!=-1){
//读到的是字节数组,需要转成字符串数组(解码)
//String(byte[]flush,int off,int length),length一定是最后读到的实际字节,因为最后一次
//读取可能不是三个字节
String str = new String(flush,0,length);
System.out.println(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{//不论最终是否出现异常,都要执行关闭输入流的操作,释放资源
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
二、字节文件输出流(OutputSream)
import java.io.OutputStream;
/**
* 测试字节输出流,用到的输出流OutputStream流
* 主要是用到了函数:void write(byte[] b,int off,int length )
* file.write(byte[]b,int off,int length)
*1、创建源
*2、选择流(OutputStream)
*3、操作:将要写的字符串==》字节数组,再将字节数组通过write函数写入到文件中
*4、释放资源
* @author Administrator
*
*/
public class TestIO05 {
public static void main(String[] args){
//1、创建源:OutputStream的源文件可以不存在,不存在时会自动进行创建
File dest = new File("D:/java program/IO/src/cn/sxt/io/des.txt");
//2、选择流:为文件选择输出流
OutputStream os = null;
try {
os = new FileOutputStream(dest);
//3、操作:定义要写的字符串,将其转化为字节数组,使用write函数写到文件中
String msg = "I am a programer";
byte[] datas = msg.getBytes();
try {
os.write(datas, 0, datas.length);
//写完后需要刷新,避免信息驻留在在内存中
os.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//4、释放资源:无论是否出现异常,都需要释放资源
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}