file(内部存储与外部存储)

android的文件编程与JAVA下的文件编程无太多区别,注意的是几点。

1、android的文件系统分为内部和外部两种,内部是指系统的指定目录:/data/data/Activity所在的包/files,外部通常是SD卡,如下代码:

textview.setText(getApplicationContext().getFilesDir() 
                + ":      " + Environment.getExternalStorageDirectory().getAbsolutePath());

执行结果分别为:/data/data/com.example.data02/files  以及mnt/sdcard。

2、对内部存储系统操作,Android提供了openFileOutput和openFileInput,代码如下:

复制代码
        FileOutputStream outputStream;
        try {
            /*         MODE_PRIVATE 私有(只能创建它的应用访问) 重复写入时会文件覆盖 
            *          MODE_APPEND  私有   重复写入时会在文件的末尾进行追加,而不是覆盖掉原来的文件 
            *          MODE_WORLD_READABLE 公用  可读 
            *          MODE_WORLD_WRITEABLE 公用 可读写*/
            outputStream = openFileOutput("test.txt",  
                    Activity.MODE_PRIVATE);
            outputStream.write(textview.getText().toString().getBytes());  
            outputStream.flush();  
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        
        try {  
            FileInputStream in = this.openFileInput("test.txt"); 
            byte[] buffer = new byte[1024];  
            in.read(buffer);  
            String str = EncodingUtils.getString(buffer, "UTF-8");  
            textview1.setText(str.toString());  
            in.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
复制代码

3、针对SD卡的文件系统操作,与JAVA文件编写一样,唯一要注意的是增加权限:

    <span style="white-space:pre">  </span><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>  
   <span style="white-space:pre">   </span><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

代码实例如下:

复制代码
        File sdcDir = Environment.getExternalStorageDirectory();  
        File file = new File(sdcDir,"info.txt");  
        
        try {
            FileOutputStream output = new FileOutputStream(file);
            output.write(textview.getText().toString().getBytes());  
            output.flush();  
            output.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        
        try {  
            FileInputStream inn = new FileInputStream(file);   
            byte[] buffer = new byte[1024];  
            inn.read(buffer);  
            String str = EncodingUtils.getString(buffer, "UTF-8");  
            textview2.setText(str.toString());  
            inn.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } 
复制代码

 

posted @   Fredric_2013  阅读(370)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示