<Zhuuu_ZZ>使用高级流读写文件

1.使用BufferedReader和FileReader读文本文件

public static String readBuffer(String path){
File f=new File(path);
FileReader fr=null;
String str=null;
BufferedReader br=null;
try{
fr=new FileReader(f);
br=new BufferedReader(fr);
String s;
StringBuffer sb=new StringBuffer();
while((s=br.readLine())!=null){
sb.append(s+"\r\n");
}
str=sb.toString;
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStaceTrace();
}finally{
try{br.close();}catch(IOException e){
e.printStrackTrace();}
try{fr.close();}catch(IOException e){
e.printStackTrace();}
}return str;
}

2.使用BufferedWriter和FileWriter写文本文件

public static void write(String str,String path,boolean isAppend){
        File f=new File(path);
        if(!f.getParentFile().exists()){
            f.getParentFile().mkdirs();
        }
        FileWriter fw=null;
        BufferedWriter bw=null;
        try {
            fw=new FileWriter(f,isAppend);
            bw=new BufferedWriter(fw);
            bw.write(str);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

3.读写二进制文件

 public static void copyData(String fromPath,String toPath){
                FileInputStream fis=null;
                DataInputStream dis=null;
                FileOutputStream fos=null;
                DataOutputStream dos=null;
                    try {
                        fis=new FileInputStream(fromPath);
                        dis=new DataInputStream(fis);
                        fos=new FileOutputStream(toPath);
                        dos=new DataOutputStream(fos);
                        int tmp;
                        while((tmp=dis.read())!=-1){
                           dos.write(tmp); 
                        }
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        try {
                            dos.close();
                            fos.close();
                            dis.close();
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

4.使用对象流读写对象信息

public static void TestObject(String s,String toPath ,String fromPath){
        FileOutputStream fos=null;
        ObjectOutputStream oos=null;
        FileInputStream fis=null;
       ObjectInputStream ois=null;
        try {
            fos=new FileOutputStream(toPath);
            oos=new ObjectOutputStream(fos);
            oos.writeObject(s);
            fis=new FileInputStream(fromPath);
            ois=new ObjectInputStream(fis);
            Object o=ois.readObject();
            System.out.println(o);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                ois.close();
                fis.close();
                oos.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值