Java编程思想:I/O的典型使用方式

import java.io.*;

public class Test {
    public static void main(String[] args) {
//        BufferedInputFile.test();
//        MemoryInput.test();
//        FormattedMemoryInput.test();
//        TestEOF.test1();
//        TestEOF.test2();
//        BasicFileOutput.test();
//        FileOutputShotcut.test();
//        StoringAndRecoveringData.test();
        UsingRandomAccessFile.test();
    }

}

/*
    打开一个文件用于字符输入,为提高速度,需要用到缓冲
 */
class BufferedInputFile {
    public static String read(String file) {
        String result = null;
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            StringBuilder sb = new StringBuilder();
            String str;
            while ((str=reader.readLine()) != null) {
                sb.append(str+"\n");
            }
            reader.close();
            result=sb.toString();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void test() {
        System.out.println(read("./src/Test.java"));
    }
}

/*
    BufferedInputFile.read()读入的String结果被用来创造一个StringReader
    然后调用read()每次读取一个字符,并将它送到控制台
 */
class MemoryInput {
    public static void test() {
        StringReader sr = new StringReader(BufferedInputFile.read("./src/Test.java"));
        int c;
        try {
            while ((c = sr.read()) != -1) {
                System.out.println((char) c);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

/*
    要读取格式化数据,可以使用DataInputStream,它是一个面向字节的I/O类(不是面向
    字符的)。因此我们必须使用InputStream类而不是Reader类
 */
class FormattedMemoryInput {
    public static void test() {
        try{
            DataInputStream in = new DataInputStream(
                    new ByteArrayInputStream(
                            BufferedInputFile.read("./src/Test.java").getBytes()
                    )
            );

            while (true) {
                System.out.println((char)in.readByte());
            }
        } catch (IOException e) {
            System.out.println("End of stream");
            e.printStackTrace();
        }
    }
}

/*
    利用avaliable()来查看还有多少可供读取的字符,用于检测输入是否结束
 */
class TestEOF {
    //需要为ByteArrayInputStream提供字节数组作为构造参数
    public static void test1() {
        try{
            DataInputStream in = new DataInputStream(
                    new ByteArrayInputStream(
                            BufferedInputFile.read("./src/Test.java").getBytes()
                    )
            );

            while (in.available()!=0) {
                System.out.println((char)in.readByte());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //这儿DataInputStream和BufferedInputStream都是装饰器
    public static void test2() {
        try {

            DataInputStream in = new DataInputStream(
                    new BufferedInputStream(
                            new FileInputStream("./src/Test.java")
                    )
            );
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

/*
    为了提供格式化机制,FileWriter被装饰成了PrintWriter
 */
class BasicFileOutput {
    private static String file = "./src/file3";

    public static void test() {

        try {
            //创建文件输入流
            BufferedReader in = new BufferedReader(
                    new StringReader(
                            BufferedInputFile.read("./src/Test.java")));

            //创建文件输出流
            PrintWriter out = new PrintWriter(
                    new BufferedWriter(
                            new FileWriter(file)));

            //从输入流写到输出流
            int lineCount=1;
            String str;
            while ((str = in.readLine()) != null) {
                out.println(lineCount++ + " : "+str);
            }
            out.close();
            System.out.println(BufferedInputFile.read(file));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

/*
    PrintWriter的辅助构造器,可以减少装饰工作
 */
class FileOutputShotcut {
    private static String file = "./src/file4";

    public static void test() {

        try {
            //创建文件输入流
            BufferedReader in = new BufferedReader(
                    new StringReader(
                            BufferedInputFile.read("./src/Test.java")));

            //创建文件输出流
            PrintWriter out = new PrintWriter(file);

            //从输入流写到输出流
            int lineCount=1;
            String str;
            while ((str = in.readLine()) != null) {
                out.println(lineCount++ + " : "+str);
            }
            out.close();
            System.out.println(BufferedInputFile.read(file));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

/*
    DataOutputStream输出可供另一个流恢复的数据
 */
class StoringAndRecoveringData {
    public static void test() {

        try{
            //建立输出流,输出数据
            DataOutputStream out = new DataOutputStream(
                    new BufferedOutputStream(
                            new FileOutputStream("./src/file4")));

            //建立输入流,恢复数据
            DataInputStream in = new DataInputStream(
                    new BufferedInputStream(
                            new FileInputStream("./src/file4")));

            out.writeDouble(3.1415926);
            out.writeUTF("That was pi");
            out.writeDouble(1.41413);
            out.writeUTF("Square root of 2");
            out.close();


            System.out.println(in.readDouble());
            System.out.println(in.readUTF());
            System.out.println(in.readDouble());
            System.out.println(in.readUTF());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}

/*
    RandomAcccessFile拥有读取基本烈性和UTF-8字符串的各种具体的方法,同时seek()
    方法可以在文件中到处移动
 */
class UsingRandomAccessFile {
    private static String file = "./src/file5";

    private static void display(){
        try{
            RandomAccessFile rf = new RandomAccessFile(file,"r");
            for (int i = 0; i < 7; i++) {
                System.out.println("VALUE "+i+": "+rf.readDouble());
            }
            System.out.println(rf.readUTF());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void write() {
        try{
            RandomAccessFile rf = new RandomAccessFile(file,"rw");
            for (int i = 0; i < 7; i++) {
                rf.writeDouble(i*1.414);
            }
            rf.writeUTF("The end of the file");
            rf.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void test() {
        write();
        display();

        try{
            RandomAccessFile rf = new RandomAccessFile(file,"rw");
            rf.seek(5*8);

            rf.writeDouble(47.01);
            rf.close();

            display();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

 

转载于:https://www.cnblogs.com/junjie2019/p/10538003.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值