IO流相关知识均在实例中,每个实例从下网上看
1.File文件(最基础)
package com.atguigu.java;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.junit.Test;
/*
* java.io.File类
* 1.凡是与输入、输出相关的类、接口等都定义在java.io包下
* 2.File是一个类,可以由构造器创建其对象.此对象对应着一个文件(.txt .avi .doc .jpg .mp3)或文件目录
* 3.File类对象是与平台无关的。
* 4.File中的方法,仅涉及到如何创建、删除、重命名等。
* 只要涉及文件内容的,File是无能为力的,必须有io流来完成。
* 5.File类的对象常作为io流的具体类的构造器的形参。
*
*/
public class TestFile {
/*
* 文件操作相关
createNewFile()
delete()
目录操作相关
mkDir():创建一个文件目录。只有在上层文件目录存在的情况下,才能返回true
mkDirs():创建一个文件目录,若上层文件目录不存在,一并创建
list()
listFiles()
*/
@Test
public void test3() throws IOException {
File file1 = new File("D:/file/helloworld.txt");
System.out.println(file1.delete());
if (!file1.exists()) {
boolean b = file1.createNewFile();
System.out.println(b);
}
File file2 = new File("D:\\file\\file2");
if (!file2.exists()) {
boolean b = file2.mkdir();
System.out.println(b);
}
File file3 = new File("D:\\v2_card");
String[] strs = file3.list();
for (int i = 0; i < strs.length; i++) {
System.out.println(strs[i]);
}
File[] files = file3.listFiles();
for (int i = 0; i < files.length; i++) {
System.out.println(files[i].getName());
}
}
/*
* 文件检测
exists()
canWrite()
canRead()
isFile()
isDirectory()
获取常规文件信息
lastModified()
length()
*/
/*@Test
public void test2(){
File file1 = new File("D:/file/helloworld.txt");
File file2 = new File("hello.txt");
File file3 = new File("D:\\file\\file1");
System.out.println(file1.exists());
System.out.println(file1.canWrite());
System.out.println(file1.canRead());
System.out.println(file1.isFile());
System.out.println(file1.isDirectory());
System.out.println(new Date(file1.lastModified()));
System.out.println(file1.length());
}*/
/*
* 路径:
* 绝对路径:包括盘符在内的完整的文件路径
* 相对路径:在当前文件目录下的文件路径
* 访问文件名:
getName()
getPath()
getAbsoluteFile()
getAbsolutePath()
getParent()
renameTo(File newName)
*/
/*@Test
public void test1(){
//File file1 = new File("D:\\file\\helloworld.txt");// "//"多一个/表示转义
File file1 = new File("D:/file/helloworld.txt");
File file2 = new File("hello.txt");
File file3 = new File("D:\\file\\file1");
File file4 = new File("D:\\file\\file1\\helloworld1.txt");
System.out.println(file1.getName());
System.out.println(file1.getPath());
System.out.println(file1.getAbsoluteFile());
System.out.println(file1.getAbsolutePath());
System.out.println(file1.getParent());
System.out.println();
System.out.println(file3.getName());
System.out.println(file3.getPath());
System.out.println(file3.getAbsoluteFile());
System.out.println(file3.getAbsolutePath());
System.out.println(file3.getParent());
//renameTo(File newName):重命名
//file1.renameTo(file2):file1重命名为file2,要求:file1必须存在,file2必须不存在
boolean b = file1.renameTo(file4);
System.out.println(b);
}*/
}
2.FileReaderWriter
package com.atguigu.java;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.junit.Test;
/*
* 使用FileReader、FileWriter可以实现文本文件的复制。
* 对于非文本文件(视频、音频、图片),只能使用字节流!
*/
public class TestFileReaderWriter {
//实现文本文件的复制
@Test
public void testFileReaderWriter() {
// 1.输入流对应的src一定要存在,否则抛异常;输出流对应的dest可以不存在
FileReader fr = null;
FileWriter fw = null;
try {
//不能实现非文本文件的复制
File src = new File("blog.txt");
File dest = new File("blog1.txt");
// 2.
fr = new FileReader(src);
fw = new FileWriter(dest);
char[] c = new char[24];
int len;
while ((len = fr.read(c)) != -1) {
fw.write(c, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fw != null) {
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//输入文本文件内容到控制台
@Test
public void testFileReader() {
FileReader fr = null;
try {
File file = new File("blog.txt");
fr = new FileReader(file);
char[] c = new char[24];
int len;
while ((len = fr.read(c)) != -1) {
String str = new String(c, 0, len);
System.out.println(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
3.FileInputOutputStream:输入、输出流
package com.atguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.Test;
/*
* 1.流的分类:
* 按照数据流向的不同:输入流 输出流
* 按照处理数据的单位的不同:字节流 字符流(处理的文本文件)
* 按照角色的不同:节点流(直接作用于文件的) 处理流
* 2.IO的体系
* 抽象基类 节点流(文件流) 缓冲流(处理流的一种,可以提升文件操作的效率)
* InputStream FileInputStream BufferedInputStream
* OutputStream FileOutputStream BufferedOutputStream (flush())
* Reader FileReader BufferedReader (readline())
* Writer FileWriter BufferedWriter (flush())
*
*/
public class TestFileInputOutputStream {
@Test
public void testCopyFile() {
long start = System.currentTimeMillis();
String src = "C:\\Users\\Libo\\Desktop\\1.flv";
String dest = "C:\\Users\\Libo\\Desktop\\2.flv";
copyFile(src, dest);
long end = System.currentTimeMillis();
System.out.println("花费时间:" + (end - start));//花费时间:26835
}
//实现文件复制的方法
public void copyFile(String src,String dest){
// 1.提供读入、写出的文件
File file1 = new File(src);
File file2 = new File(dest);
// 2.提供相应的流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
// 3.实现文件的复制
byte[] b = new byte[1024];//文件大时长度加大
int len;
while ((len = fis.read(b)) != -1) {
// fos.write(b);错误两种写法:fos.write(b,0,b.length);
fos.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
// 第二种写法
/*} finally {
if (fis != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}*/
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//从硬盘读取一个文件,并写入到另一个位置(相当于文件的复制)
@Test
public void testFileInputOutputStream() {
// 1.提供读入、写出的文件
File file1 = new File("C:\\Users\\Libo\\Desktop\\1.gif");
File file2 = new File("C:\\Users\\Libo\\Desktop\\2.gif");
// 2.提供相应的流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
// 3.实现文件的复制
byte[] b = new byte[20];
int len;
while ((len = fis.read(b)) != -1) {
// fos.write(b);错误两种写法:fos.write(b,0,b.length);
fos.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
// 第二种写法
/*} finally {
if (fis != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}*/
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//FileOutputStream
@Test
public void testFileOutputStream() {
// 1.创建一个File对象,表明要写入的文件位置
// 输出的物理文件可以不存在,当执行过程中若不存在,会自动的创建。若存在,则覆盖。
File file = new File("hello2.txt");
// 2.创建一个FileOutpuStream的对象,将file的对象作为形参传递给FileOutputStream的构造器中
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
// 3.写入的操作
fos.write(new String("aaaabbbdddbbdbbfjdkfdk").getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4.关闭输出流
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//重点:数组方式、String(b, 0, len)
@Test
public void testFileInputStream3() {
FileInputStream fis = null;
try {
File file = new File("hello.txt");
fis = new FileInputStream(file);
byte[] b = new byte[5];//读取到的数据要写入的数组。
int len;//每次读入到byte中的字节的长度
while ((len = fis.read(b)) != -1) {
// for (int i = 0; i < len; i++) {
// System.out.print((char) b[i]);
// }
String str = new String(b, 0, len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//使用try-catch的方式处理如下的异常更合理:保证流的关闭操作一定执行
@Test
public void testFileInputStream2() {
// 2.创建一个FileInputStream类的对象
FileInputStream fis = null;
try {
// 1.创建一个File类的对象
File file = new File("hello.txt");
fis = new FileInputStream(file);
// 3.调用FileInputStream的方法,实现file文件的读取
int b;
while ((b = fis.read()) != -1) {
System.out.print((char) b);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
// 4.关闭相应的流
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//从硬盘存在的一个文件中,读取其内容到程序中。使用FileOutputStream
//要读取的文件一定要存在。否则抛FileNotFountException
@Test
public void testFileInputStream1() throws Exception {
// 1.创建一个File类的对象
File file = new File("hello.txt");
// 2.创建一个FileInputStream类的对象
FileInputStream fis = new FileInputStream(file);
// 3.调用FileInputStream的方法,实现file文件的读取
/*
* read():读取文件的一个字节。当执行到文件结尾时,返回-1
*/
// int b = fis.read();
// while (b != -1) {
// System.out.print((char)b);
// b = fis.read();
// }
int b;
while ((b = fis.read()) != -1) {
System.out.print((char) b);
}
// 4.关闭相应的流
fis.close();
}
}
4.BufferedInputOutputStream:缓冲流 ----重点重点
package com.atguigu.java;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.junit.Test;
/*
* 抽象基类 节点流(文件流) 缓冲流(处理流的一种,可以提升文件操作的效率)
* InputStream FileInputStream BufferedInputStream
* OutputStream FileOutputStream BufferedOutputStream (flush())
* Reader FileReader BufferedReader (readline())
* Writer FileWriter BufferedWriter (flush())
*
*/
public class TestBuffered {
@Test
public void testBufferedReader() {
BufferedReader br = null;
BufferedWriter bw = null;
try {
File file = new File("hello.txt");
File file1 = new File("hello4.txt");
FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter(file1);
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
// char[] c = new char[1024];
// int len;
// while((len = br.read(c)) != -1){
// String str = new String(c, 0, len);
// System.out.println(str);
// }
String str;
while ((str = br.readLine()) != null) {
// System.out.println(str);
bw.write(str + "\n");
// bw.newLine();//换行
bw.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Test
public void testCopyFile(){
long start = System.currentTimeMillis();
String src = "C:\\Users\\Libo\\Desktop\\1.flv";
String dest = "C:\\Users\\Libo\\Desktop\\3.flv";
copyFile(src, dest);
long end = System.currentTimeMillis();
System.out.println("花费时间:" + (end - start));//花费时间:7253
}
public void copyFile(String src,String dest){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// 1.提供读入、写出的文件
File file1 = new File(src);
File file2 = new File(dest);
// 2.创建相应的节点流:FileInputStream、FileOutputStream
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
// 3.将创建的节点流的对象作为形参传递给缓冲流的构造器中
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
// 4.具体的实现文件复制的操作
byte[] b = new byte[1024];
int len;
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
bos.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 5.关闭相应的流
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//使用BufferedInputStream和BufferedOutputStream实现非文本文件的复制
@Test
public void testBufferedInputOutputStream() {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// 1.提供读入、写出的文件
File file1 = new File("C:\\Users\\Libo\\Desktop\\1.jpg");
File file2 = new File("C:\\Users\\Libo\\Desktop\\2.jpg");
// 2.创建相应的节点流:FileInputStream、FileOutputStream
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
// 3.将创建的节点流的对象作为形参传递给缓冲流的构造器中
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
// 4.具体的实现文件复制的操作
byte[] b = new byte[1024];
int len;
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
bos.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 5.关闭相应的流
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
5.OtherStream(其它流:转换流、输入输出流、打印流)----了解
package com.atguigu.java;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import org.junit.Test;
public class TestOtherStream {
@Test
public void testData1(){
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream(new File("data.txt")));
String str = dis.readUTF();
System.out.println(str);
boolean b = dis.readBoolean();
System.out.println(b);
long l = dis.readLong();
System.out.println(l);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(dis != null){
try {
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//数据流:用来处理基本数据类型、String、字节数组的数据DataInputStream DataOutputStream
@Test
public void testData(){
DataOutputStream dos = null;
try {
FileOutputStream fos = new FileOutputStream(new File("data.txt"));
dos = new DataOutputStream(fos);
dos.writeUTF("今天是2014年12月12号!");
dos.writeBoolean(true);
dos.writeLong(123131321);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(dos != null){
try {
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// 打印流:字节流:PrintStream 字符流:PrintWriter
@Test
public void printStreamWriter() {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File("print.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}// 创建打印输出流,设置为自动刷新模式(写入换行符或字节 '\n' 时都会刷新输出缓冲区)
PrintStream ps = new PrintStream(fos, true);
if (ps != null) { // 把标准输出流(控制台输出)改成文件
System.setOut(ps);
}
for (int i = 0; i <= 255; i++) { // 输出ASCII字符
System.out.print((char) i);
if (i % 50 == 0) { // 每50个数据一行
System.out.println(); // 换行
}
}
ps.close();
}
/*
* 标准的输入输出流:
* 标准的输出流:System.out
* 标准的输入流:System.in
*
* 题目:
* 从键盘输入字符串,要求将读取到的整行字符串转成大写输出。
* 然后继续进行输入操作,直至当输入“e”或者“exit”时,退出程序。
*
*/
@Test
public void test2() {
BufferedReader br = null;
try {
InputStream is = System.in;
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String str;
while (true) {
System.out.println("请输入字符串:");
str = br.readLine();
if (str.equalsIgnoreCase("e") || str.equalsIgnoreCase("exit")) {
break;
}
String str1 = str.toUpperCase();
System.out.println(str1);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/*
* 如何实现字节流与字符流之间的转换
* 转换流:InputStreamReader OutputStreamWriter
* 编码:字符串 --->字节数组
* 解码:字节数组 --->字符串
*/
@Test
public void test1() {
BufferedReader br = null;
BufferedWriter bw = null;
try {
// 解码
File file = new File("hello.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "GBK");
br = new BufferedReader(isr);
// 编码
File file1 = new File("hello5.txt");
FileOutputStream fos = new FileOutputStream(file1);
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
bw = new BufferedWriter(osw);
String str;
while ((str = br.readLine()) != null) {
bw.write(str);
bw.newLine();
bw.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
6.ObjectInputOutputStream(对象流)----难点
package com.atguigu.java;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import org.junit.Test;
public class TestObjectInputOutputStream {
// 对象的反序列化过程:将硬盘中的文件通过ObjectInputStream转换为相应的对象
@Test
public void testObjectInputStream() {
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream(
"person.txt"));
Person p1 = (Person)ois.readObject();
System.out.println(p1);
Person p2 = (Person)ois.readObject();
System.out.println(p2);
}catch (Exception e) {
e.printStackTrace();
}finally{
if(ois != null){
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// 对象的序列化过程:将内存中的对象通过ObjectOutputStream转换为二进制流,存储在硬盘文件中
@Test
public void testObjectOutputStream() {
Person p1 = new Person("小米", 23,new Pet("花花"));
Person p2 = new Person("红米", 21,new Pet("小花"));
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("person.txt"));
oos.writeObject(p1);
oos.flush();
oos.writeObject(p2);
oos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
/*
* 要实现序列化的类: 1.要求此类是可序列化的:实现Serializable接口
* 2.要求类的属性同样的要实现Serializable接口
* 3.提供一个版本号:private static final long serialVersionUID
* 4.使用static或transient修饰的属性,不可实现序列化
*/
class Person implements Serializable {
private static final long serialVersionUID = 23425124521L;
static String name;
transient Integer age;
Pet pet;
public Person(String name, Integer age,Pet pet) {
this.name = name;
this.age = age;
this.pet = pet;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", pet=" + pet + "]";
}
}
class Pet implements Serializable{
String name;
public Pet(String name){
this.name = name;
}
@Override
public String toString() {
return "Pet [name=" + name + "]";
}
}
7.RandomAccessFile(随机)
package com.atguigu.java;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import org.junit.Test;
/*
* RandomAccessFile:支持随机访问
* 1.既可以充当一个输入流,又可以充当一个输出流
* 2.支持从文件的开头读取、写入
* 3.支持从任意位置的读取、写入(插入)
*/
public class TestRandomAccessFile {
//通用插入
@Test
public void test3(){
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(new File("hello7.txt"), "rw");
raf.seek(3);//从第三个位置开始
byte[] b = new byte[10];
int len;
StringBuffer sb = new StringBuffer();
while((len = raf.read(b)) != -1){
sb.append(new String(b,0,len));
}
raf.seek(3);
raf.write("xy".getBytes());
raf.write(sb.toString().getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(raf != null){
try {
raf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//实现的实际上是覆盖的效果
@Test
public void test2(){
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(new File("hello6.txt"), "rw");
raf.seek(3);//从第三个位置开始
raf.write("xy".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(raf != null){
try {
raf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//进行文件的读、写
@Test
public void test1(){
RandomAccessFile raf1 = null;
RandomAccessFile raf2 = null;
try {
raf1 = new RandomAccessFile(new File("hello.txt"), "r");
raf2 = new RandomAccessFile(new File("hello6.txt"), "rw");
byte[] b = new byte[20];
int len ;
while((len = raf1.read(b)) != -1){
raf2.write(b, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(raf2 != null){
try {
raf2.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(raf1 != null){
try {
raf1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}