zoukankan      html  css  js  c++  java
  • java流概述以及文件读写示例

    1. 先分清楚是字节流还是字符流。

    字节流:InputStream OutputStream

    字符流:Reader Writer

    字符流与字节流的区别是读取的单位长度不同,字节流读8字节,字符流读16字节,所以有中文时,就得用字符流。

    2. 在字节/字符流基础上,又分为节点流和处理流

    节点流:直接和数据源相连。

    例如: FileInputStream  FileOutputStream(字节流)

        FileReader   FileWriter(字符流)

    处理流:顾名思义,就是处理在节点流上加了层处理,所以要有节点流才有处理流。

    例如: BufferedInputStream  BufferedOutputStream(字节流)

                BufferedReader  BufferedWriter(字符流)

    3. 文件读写简单示例

    import java.io.*;
    
    public class FileRead {
        public  static void main(String[] args) throws FileNotFoundException {
            String filename = "file.txt";
            String file_path = System.getProperty("user.dir")+ File.separator + "src" +  File.separator + filename;
            File file = new  File(file_path);
            if (!file.exists()){
                try {
                    //创建文件
                    file.createNewFile();
                    FileWriter fll = new FileWriter(file);
                    BufferedWriter bf = new BufferedWriter(fll);
                    String a = "hellohello";
                    bf.write(a+"
    "+"niuniu");
                    bf.flush();
                    bf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else {
            FileInputStream in = null;
                /*
                 * 字节流read
                 */
    //        try {
    //            in = new FileInputStream(file);
    //            int b = in.read();
    //            while (b != -1){
    //                System.out.println((char)b);
    //                b = in.read();
    //            }
    //            } catch (FileNotFoundException e) {
    //            e.printStackTrace();
    //            } catch (IOException e) {
    //            e.printStackTrace();
    //            }finally {
    //            try {
    //                in.close();
    //            } catch (IOException e) {
    //                e.printStackTrace();
    //            }
                /*
                 * 字符流read
                 */
                try {
                    //直接上字符流
                    FileReader rd = new FileReader(file);
                    BufferedReader br0 = new BufferedReader(rd);
                    //从字节流转为字符流
                    in = new FileInputStream(file);
                    InputStreamReader reader = new  InputStreamReader(in);
                    BufferedReader br = new BufferedReader(reader);
                    try {
                        String line;
                        line = br.readLine();
                        while (line != null){
                            System.out.println(line);
                            line = br.readLine();
                            System.out.println(br0.readLine());
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } finally {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            }
    }

     4. 随机存取

    /*
     * 将内容追加到文件末尾
     */
    try {
                // 打开一个随机访问文件流,按读写方式
               RandomAccessFile randomFile = new RandomAccessFile(System.getProperty("user.dir")  + File.separator + "file.txt", "rw");
                // 文件长度,字节数
               long fileLength = randomFile.length();
                //将写文件指针移到文件尾。
               randomFile.seek(fileLength);
               //在文件末尾加上Task_ID
                randomFile.writeBytes((","+Task_ID));
                randomFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }   
    /*
     * 随机访问文件
     */
     public static void readFileByRandomAccess(String fileName) {
            RandomAccessFile randomFile = null;
            try {
                // 打开一个随机访问文件流,按只读方式
                randomFile = new RandomAccessFile(fileName, "r");
                // 文件长度,字节数
                long fileLength = randomFile.length();
                // 读文件的起始位置
                int beginIndex = (fileLength > 4) ? 4 : 0;
                // 将读文件的开始位置移到beginIndex位置。
                randomFile.seek(beginIndex);
                byte[] bytes = new byte[10];
                int byteread = 0;
                // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
                while ((byteread = randomFile.read(bytes)) != -1) {
                    System.out.write(bytes, 0, byteread);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (randomFile != null) {
                    try {
                        randomFile.close();
                    } catch (IOException e1) {
                    }
                }
            }
        }
    
    
  • 相关阅读:
    MyBatis3-实现多表关联数据的查询
    MyBatis3-实现单表数据的增删查改
    MyBatis3-以接口方式编程
    如何识别人的技术能力和水平?
    评审的艺术——谈谈现实中的代码评审 专题
    Spring编程式和声明式事务实例讲解
    Spring Webflux: Kotlin DSL [片断]
    Spring3.1 对Bean Validation规范的新支持(方法级别验证)
    Android setImageResource与setImageBitmap的区别
    Android开发中,9-patch (九宫格)图片作为背景带来的问题
  • 原文地址:https://www.cnblogs.com/NoYone/p/8312815.html
Copyright © 2011-2022 走看看