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 java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* 文件工具类
* @author stu
*
*/
public class FileUtil {
/**
* 拷贝文件的方法
* @param from 从哪儿复制
* @param to 复制到哪儿去
*/
public void copyFile(String from,String to){
FileInputStream fis = null;//声明流对象
FileOutputStream fos = null;
try {
fis = new FileInputStream(from);//实例化流对象
fos = new FileOutputStream(to);
byte [] bytes = new byte[1024];//缓冲字节数组
int len = 0;
//循环写到输出流中去
while((len = fis.read(bytes))!=-1){
fos.write(bytes, 0, len);//写数据出去,bytes数组中从0开始到len结束
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//关闭流,一定要关闭,遵循原则先开后关,后开先关
if(fos !=null)
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
if(fis !=null)
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 读取与写出去文本文件
* @param from 读取文本文件的路径
* @param to 写出去文本文件的路径
*/
public void readWriteFile(String from,String to){
FileReader fr = null;//声明字符输入流
BufferedReader br = null;//声明缓冲输入流
FileWriter fw = null;//声明字符输出流
BufferedWriter bw = null;//声明缓冲输出流
StringBuilder sb = new StringBuilder();//
try {
fr = new FileReader(from);//实例化各种流对象
br = new BufferedReader(fr);
fw = new FileWriter(to);
bw = new BufferedWriter(fw);
String temp = null;
while((temp = br.readLine())!=null){
sb.append(temp+"\n");
}
bw.write(sb.toString());//使用缓冲输出流写出字符串
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//关闭流
if(bw !=null)
try {
bw.close();
} catch (IOException e3) {
e3.printStackTrace();
}
if(fw !=null)
try {
fw.close();
} catch (IOException e2) {
e2.printStackTrace();
}
if(br !=null)
try {
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
if(fr !=null)
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
*
* @param path 对象写到哪儿去
* @param obj 写出去的对象,必须进行序列化
*/
public void writeObject(String path ,Object obj){
ObjectOutputStream oos = null;//声明对象输出流对象
FileOutputStream fos = null;
try {
fos = new FileOutputStream(path);
oos = new ObjectOutputStream(fos);
oos.writeObject(obj);//对象输出流写出去对象
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//关闭流
if(oos !=null)
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
if(fos !=null)
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 读取对象
* @param path 对象所在的文件路径
* @return 读取出来的对象
*/
public Object readObject(String path){
FileInputStream fis = null;//声明流对象
ObjectInputStream ois = null;
Object obj = null;
try {
fis = new FileInputStream(path);//实例化流对象
ois = new ObjectInputStream(fis);
obj = ois.readObject();//使用对象输入流读取对象
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
//关闭流
if(ois !=null)
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
if(fis !=null)
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return obj;
}
/**
*
* @author stu
* 序列化对象就是实现Serializable接口
*/
class Goods implements Serializable{
private String name ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
private double price;
}
}
每天一次IO流复习
最新推荐文章于 2024-08-06 16:07:16 发布