zoukankan      html  css  js  c++  java
  • java基础---->java输入输出流

      今天我们总结一下java中关于输入流和输出流的知识,博客的代码选自Thinking in java一书。我突然很想忘了你,就像从未遇见你。

    java中的输入流

    huhx.txt文件的内容如下: I love you, ch. 中文

    一、缓冲中输入文件

    public class BufferedInputFile {
        public static String read(String filename) {
            BufferedReader reader = null;
            StringBuilder builder = new StringBuilder();
            try {
                reader = new BufferedReader(new FileReader(filename));
                String line;
                while ((line = reader.readLine()) != null) {
                    builder.append(line + "
    ");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return builder.toString();
        }
    
        public static void main(String[] args) {
            String content = read("file/huhx.txt");
            System.out.println(content);
        }
    }

    二、从内存输入

    public class MemoryInput {
        public static void main(String[] args) {
            StringReader reader = new StringReader(BufferedInputFile.read("file/huhx.txt"));
            int c;
            try {
                while ((c = reader.read()) != -1) {
                    System.out.print((char) c);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    三、格式化的内存输入

    public class FormattedMemoryInput {
        public static void main(String[] args) {
            DataInputStream inputStream = new DataInputStream(
                    new ByteArrayInputStream(BufferedInputFile.read("file/huhx.txt").getBytes()));
            try {
                while (inputStream.available() != 0) {
                    System.out.print((char)inputStream.readByte());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    available()的工作方式会随着所读取的媒介类型的不同而有所不同。也就是说在没有阻塞的情况下所能读取的字节数。对于文件,这意味着整个文件。但是对于不同类型的流,可能就不是这样的。

    java中输出流

    一、基本的文件输出

    public class BasicFileOutput {
        static String file = "file/linux.txt";
        public static void main(String[] args) {
            BufferedReader reader = new BufferedReader(new StringReader(BufferedInputFile.read("file/huhx.txt")));
            PrintWriter out = null;
            try {
                out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
                String line;
                while ((line = reader.readLine()) != null) {
                    out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                out.close();
            }
            System.out.println(BufferedInputFile.read(file));
        }
    }

    二、文本文件输出的快捷方式

    public class FileOutputShortcut {
        static String file = "file/linux.out"; // 这个也和上述不一样
        public static void main(String[] args) {
            BufferedReader reader = new BufferedReader(new StringReader(BufferedInputFile.read("file/huhx.txt")));
            PrintWriter out = null;
            try {
                out = new PrintWriter(new FileWriter(file)); // 这里和上述不一样
                String line;
                while ((line = reader.readLine()) != null) {
                    out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                out.close();
            }
            System.out.println(BufferedInputFile.read(file));
        }
    }

    三、存储和恢复数据

    public class StoringAndRecoveringData {
        public static void main(String[] args) {
            try {
                DataOutputStream out = new DataOutputStream(
                        new BufferedOutputStream(new FileOutputStream("file/chenhui.txt")));
                out.writeDouble(3.1415926);
                out.writeUTF("中文可以吗?");
                out.writeInt(123);
                out.close();
    
                DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("file/chenhui.txt")));
                System.out.println(in.readDouble()); // 如果这里是readInt(),会导致后面的readUTF方法报错
                System.out.println(in.readUTF());
                System.out.println(in.readInt());
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    三、读写随机访问文件

    public class UsingRandomAccessFile {
        static String file = "file/huhx.txt";
    
        static void display() throws Exception {
            RandomAccessFile rf = new RandomAccessFile(file, "r");
            System.out.println(rf.readDouble());
            System.out.println(rf.readDouble());
            System.out.println(rf.readUTF());
            rf.close();
        }
    
        public static void main(String[] args) {
            RandomAccessFile rf;
            try {
                rf = new RandomAccessFile(file, "rw");
                rf.writeDouble(23.654);
                rf.writeDouble(3.14156);
                rf.writeUTF("我爱你!");
                rf.close();
                display();
    
                rf = new RandomAccessFile(file, "rw");
                rf.seek(1 * 8); 
                rf.writeDouble(2.366);
                rf.close();
                display();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    /*
    23.654
    3.14156
    我爱你!
    23.654
    2.366
    我爱你!
    */

    友情链接

  • 相关阅读:
    codeforces 图论题目集(持续更新)
    整数快速幂
    Codeforces Codeforces Global Round 5 C2
    POJ 1061
    扩展欧几里德算法(数论)
    Codeforces Round #592 (Div. 2) 1224D
    Codeforces Round #582 (Div. 3) G. Path Queries
    2019 kickstart A轮 B 题
    P3379 【模板】最近公共祖先(LCA)
    bzoj 2002 分块
  • 原文地址:https://www.cnblogs.com/huhx/p/baseusejavafile1.html
Copyright © 2011-2022 走看看