美文网首页
使用其他子类读取写入文本

使用其他子类读取写入文本

作者: 老林_ | 来源:发表于2021-04-23 09:20 被阅读0次

    使用PrintWriter写入读取文本

    public static void main(String[] args) throws IOException {
            //写入文本
            String fileUrl = BaseWriteFile.getFileUrl("G:/java_test/git/jBase/build/resources/main/file4WriteData.txt");
            try (
                    PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(
                            new FileOutputStream(fileUrl, false), "UTF-8"
                    ), true)) {
                printWriter.print("MyAge is ");
                printWriter.print(12);
                printWriter.println(" age.");
            }
            //读取文本输入
            Path path = Paths.get(fileUrl);
            if(path!=null) {
                String s = new String(Files.readAllBytes(path), "UTF-8");
                System.out.println(s);
    
                try(Stream<String> lines = Files.lines(path)){
                   lines.forEach(System.out::print);
                }
            }
        }
    

    使用DataOutputStream和DataInputStream读写文本

    结果如下,将文件用二进制格式打开


    image.png

    其中char是2字节,int是4字节

    public static void main(String[] args) {
            try {
                String fileUrl = BaseWriteFile.getFileUrl("G:/java_test/git/jBase/build/resources/main/file4WriteData.txt");
                DataOutputStream outputStream=new DataOutputStream(
                        new FileOutputStream(fileUrl)
                );
                outputStream.writeInt(33);
                outputStream.writeChar('b');
    
                //按顺序读出来
                DataInputStream inputStream=new DataInputStream(
                        new FileInputStream(fileUrl)
                );
                System.out.print(inputStream.readInt());
                System.out.print(inputStream.readChar());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

    对象输入/输出流与序列化

    public static void main(String[] args) {
            try {
                String fileUrl = BaseWriteFile.getFileUrl("G:/java_test/git/jBase/build/resources/main/file4WriteData.txt");
                try(ObjectOutputStream objectOutputStream=new ObjectOutputStream(
                        new FileOutputStream(fileUrl)
                );ObjectInputStream objectInputStream=new ObjectInputStream(
                        new FileInputStream(fileUrl)
                )) {
                    Teacher teacher=new Teacher("小老师",11);
                    objectOutputStream.writeObject(teacher);
                    teacher=new Teacher("老老师",33);
                    objectOutputStream.writeObject(teacher);
                    //由于readObject()无法判断是否还有对象可读,所以自己放入一个null用来结束循环
                    objectOutputStream.writeObject(null );
                    Object o=objectInputStream.readObject();
                    while (o!=null){
                        System.out.println(o);
                        o=objectInputStream.readObject();
                    }
                }
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    

    注意,这里Teacher类需要进行序列化

    public class Teacher implements Serializable {
        private String name;//姓名
        private int age;//年龄
    
        public Teacher(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Teacher{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    

    相关文章

      网友评论

          本文标题:使用其他子类读取写入文本

          本文链接:https://www.haomeiwen.com/subject/sfaalltx.html