ACAC FileReader和DataInput

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();
                }
            }

        }

    }
}

猜你喜欢

转载自blog.csdn.net/qq_44707513/article/details/110483789