码迷,mamicode.com
首页 > 移动开发 > 详细

安卓如何将TXT文件写到特定路径

时间:2017-10-24 19:31:39      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:ted   rand   ace   blog   try   ram   bsp   dom   bytes   

其实就一个方法,就不贴所有代码了。

    /**
     * 写入文件方法
     * @param content
     */
    public static void write(String content) {
        try {
            //判断实际是否有SD卡,且应用程序是否有读写SD卡的能力,有则返回true
            if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                // 获取SD卡的目录
                File sdCardDir = Environment.getExternalStorageDirectory();
                String path = "/APP/";
                File dir = new File(path);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                File targetFile = new File(sdCardDir.getCanonicalPath() + path+"aaa.txt");
                //使用RandomAccessFile是在原有的文件基础之上追加内容,
                //而使用outputstream则是要先清空内容再写入
                RandomAccessFile raf = new RandomAccessFile(targetFile, "rw");
                //光标移到原始文件最后,再执行写入
                raf.seek(targetFile.length());
                raf.write(content.getBytes());
                raf.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

安卓如何将TXT文件写到特定路径

标签:ted   rand   ace   blog   try   ram   bsp   dom   bytes   

原文地址:http://www.cnblogs.com/linfenghp/p/7725132.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!