ACAC FileReader和DataInput

由于博客内容为空,暂无法提供包含关键信息的摘要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 *
 * FileReader
 *    文件字符输入流,只能读取普通文本
 *    读取文本内容时,比较方便
 *
 */
public class FileReaderTest01 {
    public static void main(String[] args) {
        FileReader fr = null;
        try {
            //创建文件字符输入流
            fr = new FileReader("myfile");
            //开始读
            char[] chars = new char[4];//一次读取四个字符
            int readCount = 0;
            //往char数组中读
            fr.read(chars);
            for(char data : chars){
                System.out.println(data);
            }

           /* while ((readCount = fr.read(chars)) != -1){
                System.out.println(new String(chars,0,readCount));
            }*/


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

import java.io.FileWriter;
import java.io.IOException;

/**
 * FileWriter:
 *      文件字符输出流,写
 *      只能输出普通文本(Word文件不是普通文本)
 */
public class FileWriterTest01 {
    public static void main(String[] args) {
        FileWriter fw = null;
        try {
            //创建文件字符输出流对象
            //fw = new FileWriter("myfile02");
            fw = new FileWriter("myfile02",true);//追加字符
            //开始写
            char[] chars = {'我','是','中','国','人'};

            fw.write(chars);

            fw.write(chars,2,3);
            fw.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 *
 * DataOutputStream写的文件,只能使用DataInputStream去读,而且读的时候需要知道写入的顺序
 *
 * DataInputStream数据字节输入流
 *
 * 读的顺序和写的顺序一样,才能正常取出数据
 */
public class DataInputStreamTest01 {
    public static void main(String[] args) {
        DataInputStream dis = null;
        try {
            dis = new DataInputStream(new FileInputStream("jiamitext01"));

           byte b =  dis.readByte();
           short s = dis.readShort();
           int i = dis.readInt();
           long l = dis.readLong();
           float f = dis.readFloat();
           double d = dis.readDouble();
           boolean boo = dis.readBoolean();
           char c = dis.readChar();

            System.out.println(b);
            System.out.println(s);
            System.out.println(i);
            System.out.println(l);
            System.out.println(f);
            System.out.println(d);
            System.out.println(boo);
            System.out.println(c);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (dis != null) {
                try {
                    dis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

import java.io.*;

/**
 * java.io.DataOutputStream:
 *      这个流可以将数据连同数据的类型一并写入文件
 *      注意:这个文件不是普通文本文件,用记事本打不开
 */
public class DataOutputStreamTest01 {
    public static void main(String[] args) {
        DataOutputStream dop = null;
        try {
            //创建数据专属是字节输出流
            dop = new DataOutputStream(new FileOutputStream("jiamitext01"));
            byte b = 122;
            short s = 233;
            int i = 345;
            long l = 567;
            float f = 12.34f;
            double d = 234.234;
            boolean boo = true;
            char c = 'a';

            dop.writeByte(b);
            dop.writeShort(s);
            dop.writeInt(i);
            dop.writeLong(l);
            dop.writeFloat(f);
            dop.writeDouble(d);
            dop.writeBoolean(boo);
            dop.writeChar(c);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (dop != null) {
                try {
                    dop.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值