Java笔记杨枝11.26

其他IO流

数据流:
DataOutputStream(写)和DataInputStream(读)

DataInputStream dis = new DataInputStream(
                  new FileInputStream("dos.txt"));

        //读数据
        byte b = dis.readByte() ;
        int i = dis.readInt() ;
        short s = dis.readShort() ; 
        long l = dis.readLong() ;
        char ch = dis.readChar() ;
        boolean flag = dis.readBoolean() ;
        float f = dis.readFloat() ;
        double d = dis.readDouble() ;
DataOutputStream dos = new DataOutputStream(new FileOutputStream(
                "dos.txt"));

        //写数据
        dos.writeByte(100) ;
        dos.writeInt(1000) ;
        dos.writeShort(120) ;
        dos.writeLong(1000000L);
        dos.writeChar('A') ;
        dos.writeBoolean(true) ;
        dos.writeFloat(12.34F) ; 
        dos.writeDouble(12.56) ;

二 内存操作流
针对内存的数据进行操作的,程序一结束,这些内存中的数据就消失掉了!

package prac;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class Prac04 {
 public static void main(String[] args) throws IOException {
     ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
            baos.write(("love").getBytes()) ;


     byte[] buffer = baos.toByteArray() ;

     ByteArrayInputStream bais = new ByteArrayInputStream(buffer) ;

     int by = 0 ;
        while((by=bais.read())!=-1){
            System.out.print((char)by);
        }
}
}

三 打印流:
字节打印流:PrintStream
字符打印流:PrintWriter
特点:
1)复制文件时,只能操作目的源,只能输出数据。
2)可直接对文本文件操作

PrintWriter:有自动刷新功能:
public PrintWriter(Writer out,boolean autoFlush)
第二个参数指定为true,则启动自动刷新
PrintWriter pw = new PrintWriter(new FileWriter(“pw.txt”),true) ;
加入自动刷新功能并且在写数据的时候,使用println():换行
所以复制文件可以使用PrintWriter进行输出数据

public class CopyFileDemo {

    public static void main(String[] args) throws IOException {
//      1)封装数据源
        BufferedReader br = new BufferedReader(new FileReader("DataStreamDemo.java")) ;

        ////2)封装目的地
        //创建字符打印流对象,并且启动自动刷新功能
        PrintWriter pw = new PrintWriter(new FileWriter("Copy.java"), true) ;

        //读写操作
        String line = null ;
        while((line=br.readLine())!=null){
            //写数据
            pw.println(line) ;
        }

        //关闭资源
        pw.close() ;
        br.close() ;
    }
}

四 使用IO流进行录入数据

package prac;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Prac05 {
    public static void main(String[] args) throws IOException {
         BufferedReader br=  new BufferedReader(new InputStreamReader(System.in));

         System.out.println("请输入一个整数:");
        String a=br.readLine();

        int b=Integer.parseInt(a);
        System.out.println("您输入的整数是:"+b);
    }
}

五 随机访问流RandomAccessFile
不是实际意义上的流,因为它继承自Object类
常用的构造方法:
public RandomAccessFile(String name, String mode)
参数一:指定该文件的路径
参数二:指定的一种模式:常用的模式:”rw”,这种模式是可以读也可以写

RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw") ;

        //读数据

        byte b = raf.readByte() ;
        System.out.println(b);
        System.out.println("getFilePointer:"+raf.getFilePointer());

        char ch = raf.readChar() ;
        System.out.println(ch);

        String  str  = raf.readUTF() ;
        System.out.println(str);

        //关闭资源
        raf.close() ;
    RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw") ;

        //写数据
        raf.writeByte(100) ;
        raf.writeChar('a') ;
        raf.writeUTF("中国") ;

        //关闭资源
        raf.close() ;

六 合并流SequenceInputStream(读数据)
在复制文件的时候,只能操作数据源,不能操作目的地


    SequenceInputStream sis = new SequenceInputStream(new FileInputStream("a.txt"),new 
            FileInputStream("b.txt"));

    BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("c.txt"));

    byte[] b=new byte[1024];
    int len=0;
    while((len=sis.read(b))!=-1){
        bos.write(b, 0, len);
    }

    sis.close() ;
    bos.close() ;
}
}

合并流另一种构造方式:复制多个文件

Vector<InputStream> v = new Vector<InputStream>() ;

        //封装者三个java文件
InputStream s1 = new FileInputStream("a.txt") ;
InputStream s2 = new FileInputStream("b.txt") ;
InputStream s3 = new FileInputStream("c.txt") ;

        //添加到集合中
        v.add(s1) ;
        v.add(s2) ;
        v.add(s3) ;

        //调用特有功能:
        //public Enumeration<E> elements()
    Enumeration<InputStream> en = v.elements() ;
        //创建合并刘对象
    SequenceInputStream sis = new SequenceInputStream(en) ;
        //封装目的地
    BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream("c.txt"));

        //一次读取一个字节数组
        byte[] bys = new byte[1024] ;
        int len = 0 ;
        while((len=sis.read(bys))!=-1){
            //写数据
            bos.write(bys, 0, len) ;
            bos.flush() ;
        }

        //释放资源
        bos.close() ;
        sis.close() ;

七 序列化和反序列化流

package prac;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Prac08 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
    //反序列化
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d.txt"));
    //使用反序列化:将流数据--->对象
    Object obj = ois.readObject() ;
    ois.close() ;
    System.out.println(obj);


    //序列化
    Person p = new Person("小明", 18) ;
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d.txt"));
    oos.writeObject(p) ;
    oos.close() ;

}
}
//如果启用序列化功能,那么必须实现一个接口:Serializable
class Person  implements Serializable{
    //序列化和反序列化生产的版本Id不一致的时候,会出现异常,所以使用生产随机ID或者固定ID解决
    private static final long serialVersionUID = 1L;

    private String name;
    //transient:不用被序列化的时候用它修饰
    private transient int age;


    public Person() {
        super();

    }

    public Person(String name, int age) {
        super();
        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;
    }

    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }


}

八 属性集合类
Properties:可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串

package prac;

import java.util.Properties;
import java.util.Set;

public class Prac09 {
      public static void main(String[] args) {
        Properties prop=new Properties();

        prop.setProperty("a", "b");
        //遍历属性列表:public Set<String> stringPropertyNames()
        Set<String> keySet = prop.stringPropertyNames() ;

        for(String key:keySet){
            String value=prop.getProperty(key);
            System.out.println(key+"......."+value);
        }

    }
}

Properties 可保存在流中或从流中加载。

package prac;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class Prac10 {
public static void main(String[] args) throws IOException {
        // 将文件中的数据加载到属性集合中:public void load(Reader reader)
        Properties prop = new Properties();
        FileReader file = new FileReader("d.txt");
        prop.load(file);
        file.close();
        System.out.println(prop);

        // 将属性集合中的数据保存到文件中:public void store(Writer writer,String comments)
        Properties prop1 = new Properties();

        // 给属性集合中添加数据
        prop1.setProperty("张三", "30");
        prop1.setProperty("李四", "40");
        prop1.setProperty("王五", "50");

        // 将属性列表中的数据保存到文件中
        FileWriter fw = new FileWriter("d.txt");
        prop1.store(fw, "names content");

        fw.close();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: The original text of the dream recorded by Jiangchengzi on the night of January 20th, translated and appreciated, is as follows:"On the night of January 20th, I dreamed that I was a butterfly, fluttering around in a beautiful garden, happy and content. I was surrounded by a variety of flowers, and the pleasant scent filled the air. The sun shone brightly, and the birds sang melodiously. I was truly in paradise." ### 回答2: 原文: 深宫月色残,银汉乱人言。 采樵归不见,相思在疏篱。 万里春风来,寸寸芳心碎。 翠华暗绿雨,尺素寒尘滑。 乙卯正月二十日夜,梦与博山先生相见。批书诀曰:“以直报怨,以德报德”。时平台飞路远,支雪采簪华乌。白池梧桐叶中素,青丘蓬莱问苍虚。辂晓雷鸣,玄云千里一江雨。骤兰香慢蕊俏,玉泉冰寒锦点珠。杨枝弄笛滋蔓草,秋水翻天风满庭。鸟兽尽经夜残雪,若开晴未报平生。明朝忽觉天游梦,冷艳云光空残红。 翻译: 深宫中,月色仍残,银河散乱人之言语。 采集早归,却见不到你,想念依然在深深篱笆间。 万里春风吹来,一寸一寸芳心碎裂。 翠色华丽的花朵暗淡,一寸寸素净的尘埃滑落。 乙卯正月二十日的夜晚,梦见了与博山先生相见。他给我留下了一段批书:“以直报怨,以德报德”。那时平台上的路途遥远,支雪采着华丽的乌鸦羽毛。池塘中白色梧桐的叶子素净,问清幽的青丘蓬莱仙穴处的苍虚。车轮在黎明时分响起雷声,玄云千里远,江河上下的雨水也如细丝般纷纷飘洒。茂盛的兰花香气慢慢散去,花蕊娇美,水泉冰寒上点缀着珍珠。杨枝弄笛,蔓草蔓延,秋水翻滚,天空充满风的香味。鸟兽经过夜晚的残雪,若是旷白晴朗,将不再记得以往的人生。清晨醒来,突然发觉身心在天空中遨游,寒冷美丽的云光仅余一点残红。 赏析: 这是唐代杜牧的《江城子·乙卯正月二十日夜记梦》。诗人通过描写梦境中的景象和情感表达了思念之情。诗中以深宫月夜为背景,描绘了月色残照、银河散乱的景象,与思念之情相呼应。诗人对于思念之人的期望和痛苦心情都交织在诗中,表达了内心的矛盾和苦楚。似乎诗人梦中遇见了博山先生,并从他那里得到了关于直报怨、德报德的短评,这为诗中的思念之情增添了一丝宽慰。通过描绘梦境中各种遥远而奇幻的景象,诗人巧妙地展现了自己内心的情感世界,使诗歌更具幽玄之美和意境深远之感。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值