搜索
您的当前位置:首页正文

字符流使用

来源:独旅网

使用字符流读写文件

1、使用字符流类 BufferedReader 和 FileReader 读文本文件
基本步骤

  • 引入相关类
  • 构造一个 BufferedReader 对象
  • 利用 BufferedReader 类的方法读取文本文件数据
  • 关闭相关的流对象

代码演示

public static void main(String[] args) {
        String s=readBuffer("E:/work/text.txt");
        System.out.println(s);
    }
    public static String readBuffer(String path){
        FileReader fr=null;
        String str=null;
        StringBuffer sb=null;
        BufferedReader br=null;
        try {
            fr=new FileReader(path);
            br=new BufferedReader(fr);
            String  s ;
            sb=new StringBuffer();
            while ((s=br.readLine())!=null){
                sb.append(s+"\r\n");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return String.valueOf(sb);
    }

2、使用字符流类 BufferedWriter 和 FileWriter 写文本文件
基本步骤

  • 引入相关类
  • 构造 BufferedWrite 对象
  • 利用 BufferedWriter 类的方法写文本文件
  • 相关流对象的清空和关闭

代码演示

public static void main(String[] args) {
        String s="I love you;嫁给我好吗?\r\n不好,快滚!!!";
        write(s,"E:/m.txt",false);
    }

    public static void write(String str,String path,boolean isAppend){
        File f=new File(path);
        char[]c=str.toCharArray();
        FileWriter fw=null;
        try {
            fw=new FileWriter(f,isAppend);
            fw.write(str);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

运行结果如下

读写二进制文件

读写二进制文件一般是对图片进行操作
现在使用字节流读写二进制文件,进行图片复制

public static void main(String[] args) {
        copyDate("a.jpg","E:/work/4.jpg");
    }

    public static void copyDate(String fromPath, String targetPath){
        FileInputStream fis=null;
        DataInputStream dis=null;
        FileOutputStream fos=null;
        DataOutputStream dos=null;
        try {
            fis=new FileInputStream(fromPath);
            dis=new DataInputStream(fis);
            fos= new FileOutputStream(targetPath);
            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();
            }
        }
    }

运行结果

序列化

序列化就是将对象的状态存储到特定的储存介质中的过程,也就是将对象状态转换为可保持或可传输格式的过程。
在序列化过程中,会将对象的共有成员、私有成员以及类名,转换为字节流,然后再把字节流写入数据流,存储到存储介质中。
序列化的意义在于将 Java 对象序列化后,可以将其转换为字节序列,这些字节序列可以被保存到磁盘上,也可以进行网络传输;同时序列化后的对象保存的是二进制状态,实现了平台无关性

序列化保存对象信息

Java 中只有实现了 java.io.Serializable 接口的类的对象才能被序列化
实现序列化步骤
(1)创建一个对象输出流
(2)通过对象输出流的 writeObject() 方法写对象,即输出可序列化对象
代码示例
实现 java.io.Serializable 接口的类

public class Student implements Serializable {
    private String name;
    private String gender;
    public Student(String name, String gender) {
        this.name = name;
        this.gender = gender;
    }
    //省略getter/setter方法
    @Override
    public String toString() {
        return "Student{" + "name='" + name + '\'' + ", gender='" + gender + '\'' + '}';
    }
}

测试类

public static void main(String[] args) throws IOException, ClassNotFoundException {
        Student s=new Student("小张","男");
        FileOutputStream fos= new FileOutputStream("obj.txt");
        ObjectOutputStream oos=new ObjectOutputStream(fos);
        oos.writeObject(s);
        oos.close();
        fos.close();
        System.out.println(s);
        FileInputStream fis = new FileInputStream("obj.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object o = ois.readObject();
        System.out.println(o);
    }

这就是一个简单的使用序列化保存对象信息

因篇幅问题不能全部显示,请点此查看更多更全内容

热门图文

Top