IO流用得到最多的是文件字节输入输出流,FileInputStream、FileOutputStream
public class IODemo {
/**
* FileReader文件字符读取流
* 读取文件,一个字符一个字符的读
*效率低
*/
@Test
public void testFileReader() {
//一个个读
FileReader fileReader=null;//外部定义,扩大范围
try {
fileReader=new FileReader("a.txt");
//Returns:The character read, or -1 if the end of the stream has beenreached
//返回读取的字符,到达流尾部返回-1
int ch1=fileReader.read();
System.out.println(ch1);//113
System.out.println((char)ch1);//q
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("文件为空!");
}
}
}
@Test
public void test2() {
//while读所有
FileReader fileReader=null;//外部定义,扩大范围
try {
fileReader=new FileReader("a.txt");
int ch=-1;
while ((ch=fileReader.read()) != -1) {//最后-1退出
System.out.println(ch);//97
System.out.println((char)ch);//a
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("文件为空!");
}
}
}
/**
* 字符流 char[] buffer
*/
@Test
public void test3() {
//读所有--数组
FileReader fileReader=null;//外部定义,扩大范围
FileWriter fileWriter=null;
try {
fileReader=new FileReader("a.txt");
fileWriter=new FileWriter("b.txt");
char[] buffer=new char[10];
int length=0;
while ((length=fileReader.read(buffer)) != -1) {//一次读一个数组10个数,最后-1退出
System.out.println(length);
System.out.println(Arrays.toString(buffer));
fileWriter.write(buffer, 0, length);//write(buffer,offset,length)数组、偏移量、长度
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fileReader != null) {//try中可能发生异常,所以要判断一下是否为空
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileWriter != null) {
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 字节输入流 byte[] buffer
*/
@Test
public void test4() {
FileInputStream fileInputStream=null;
FileOutputStream fileOutputStream=null;
try {
fileInputStream=new FileInputStream("a.jpg");
fileOutputStream=new FileOutputStream("b.jpg");
byte[] buffer=new byte[1024];
int length=-1;
while ((length=fileInputStream.read(buffer))!=-1) {
fileOutputStream.write(buffer, 0, length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
/**
* 对象流 要实现序列化接口
* 写入文件,文件输出流,对象输出流
*/
public void test5() {
Student student=new Student(1,"za", 13, "nan");
FileOutputStream fileOutputStream=null;
ObjectOutputStream objectOutputStream=null;
try {
fileOutputStream=new FileOutputStream("student.data");
objectOutputStream=new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(student);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (objectOutputStream != null) {//不判断一下,try中异常的话此处会报空指针
try {//A依赖于B,先关闭A
objectOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println(student);
}
@Test
/**
* 对象输入流
* 文件输入流,读取文件
*/
public void test6() {
FileInputStream fileInputStream=null;
ObjectInputStream objectInputStream=null;
try {
fileInputStream=new FileInputStream("student.data");
objectInputStream=new ObjectInputStream(fileInputStream);
Student student=(Student)objectInputStream.readObject();
System.out.println(student);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
if (objectInputStream !=null) {
try {
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}