zoukankan      html  css  js  c++  java
  • Android 追加写入文件的三种方法

    一、使用FileOutputStream

    使用FileOutputStream,在构造FileOutputStream时,把第二个参数设为true

        public static void method1(String file, String conent) {  
            BufferedWriter out = null;  
            try {  
                out = new BufferedWriter(new OutputStreamWriter(  
                        new FileOutputStream(file, true)));  
                out.write(conent);  
            } catch (Exception e) {  
                e.printStackTrace();  
            } finally {  
                try {  
                    out.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  

    二、使用FileWriter

    打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件

        public static void method2(String fileName, String content) {  
            try {  
                // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件  
                FileWriter writer = new FileWriter(fileName, true);  
                writer.write(content);  
                writer.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  

    三、使用RandomAccessFile

    打开一个随机访问文件流,按读写方式写入

        public static void method3(String fileName, String content) {  
            try {  
                // 打开一个随机访问文件流,按读写方式  
                RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");  
                // 文件长度,字节数  
                long fileLength = randomFile.length();  
                // 将写文件指针移到文件尾。  
                randomFile.seek(fileLength);  
                randomFile.writeBytes(content);  
                randomFile.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
  • 相关阅读:
    select详解
    java Map及Map.Entry详解
    Java 基本类型
    java 获取String出现最多次数的字段
    java 居民身份证的校验
    java 删除文件
    Java 导出excel进行换行
    获取文件及其文件路径
    List<Map<String,Object>> 中文排序
    Java ----单个list 删除元素
  • 原文地址:https://www.cnblogs.com/renhui/p/8656586.html
Copyright © 2011-2022 走看看