android---(文件管理)
来源:互联网 发布:手机大型网络枪战游戏 编辑:程序博客网 时间:2024/06/05 18:57
//以下文件均在 data/data/程序包名/public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 写入私有文件 会在当前应用程序包下创建一个 file 文件夹 并在此文件夹下创建一个zhang.txt文件 * * @param view */ public void writerPrivate(View view) { try { OutputStream out = openFileOutput("zhang.txt", Context.MODE_APPEND); String info = "中国人"; byte[] bytes = info.getBytes(); out.write(bytes, 0, bytes.length); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 读取私有文件内容 读取应用程序包下的 file文件夹下的指定文件 ,这里为 zhang.txt * * @param view */ public void readSDK(View view) { try { InputStream in = openFileInput("zhang.txt"); StringBuffer sb = new StringBuffer(); byte[] bytes = new byte[100]; int len = -1; while ((len = in.read(bytes)) != -1) { sb.append(new String(bytes, 0, len)); } in.close(); Toast.makeText(this, sb, Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /*** * 读取 raw 文件,自已在res 文件夹下 创建的 raw文件夹下的文件 名称为固定写法 * * @param view */ public void readRawFile(View view) { InputStream in = getResources().openRawResource(R.raw.zhang); StringBuffer sb = new StringBuffer(); byte[] bytes = new byte[100]; int len = -1; try { while ((len = in.read(bytes)) != -1) { sb.append(new String(bytes, 0, len)); } in.close(); } catch (IOException e1) { e1.printStackTrace(); } Toast.makeText(this, sb, Toast.LENGTH_SHORT).show(); } /** * 创建缓存文件 在当前应用程序包下的 cache 文件夹下创建一个以 .tmp结尾的文件 并写入缓存内容 * * @param view */ public void readCacheFile(View view) { //创建缓存文件路径 //String temp = getCacheDir()+"/temp.tmp"; try { //快速创建缓存文件 File tem = File.createTempFile("temp", null, getCacheDir()); //临时文件写入内容 FileOutputStream out = new FileOutputStream(tem); PrintStream ps = new PrintStream(out); ps.print("我是第一噢!"); ps.close(); } catch (IOException e) { e.printStackTrace(); } }}
//以下文件均在 sdcard 卡上public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /*** * 判断外部是否有sdCard 存储卡 * * @param view */ public void isExtSdk(View view) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { Toast.makeText(this, "sdk", Toast.LENGTH_SHORT).show(); if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY)) { Toast.makeText(this, "sd只读", Toast.LENGTH_SHORT).show(); } else { //获取外部存储sdk 的路径 Log.i("msg", Environment.getExternalStorageDirectory().getPath()); //访问 android内置的文件夹 也就是 storage 的文件夹下的文件或文件夹 ,这里访问下载文件夹的路径 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); } } else { Toast.makeText(this, " no sdk", Toast.LENGTH_SHORT).show(); } } /*** * 写入外部存储的私有文件 4.4以后才能使用 * <p/> * //在本应用程序在外部存储卡上的 storage/sdcard/android/data/应用程序包/file文件夹下写入文件, * //只有本应用程序才能访问这个 文件 * * @param view */ public void writerSDK(View view) { //本应用程序包下的 file 文件夹 File file = getExternalFilesDir(null); if (file != null) { try { FileOutputStream out = new FileOutputStream(file + "/zhangzhang.txt"); Log.i("msg", file.getPath()); PrintStream ps = new PrintStream(out); ps.print("zhang zhang"); ps.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } } /*** * 写入外部存储的私有缓存文件 4.4以后才能使用 * * 路径 storage/sdcard/android/data/应用程序包/cache文件夹下写入缓存文件, * * @param view */ public void readprivateSDKtmp(View view) { try { //创建缓存文件 File tempFile = File.createTempFile("zhan", null, getExternalCacheDir()); if (tempFile != null) { FileOutputStream out = new FileOutputStream(tempFile); PrintStream ps = new PrintStream(out); ps.print("good is good"); ps.close(); out.close(); } } catch (IOException e) { e.printStackTrace(); } }}
0 0
- android的文件管理
- Android文件管理
- Android 文件管理
- android 系统文件管理
- android---(文件管理)
- android文件管理
- Android文件管理
- Android之文件管理
- android文件管理
- android文件管理实现源码
- Android——文件管理
- Android资源文件的管理
- Android 简易文件管理模块
- Android文件管理常用的方法
- android webview downloadManager 文件下载管理
- android webview downloadManager文件下载管理
- android 基于ftp远程文件管理
- 关于Android Context的文件管理函数。
- HVM的中断与异常处理
- IOS中的关键帧动画
- 使用Bundle在Activity之间交换数据
- Linux虚拟机设置网络、实现主机名ssh访问
- LeetCode 26: Remove Duplicates from Sorted Array
- android---(文件管理)
- 关于数组面试小题
- Apprenticeship Pattern
- spoj7001. Visible Lattice Poin
- 【推荐】SpringMVC源码总结(一)HandlerMapping和HandlerAdapter入门
- 前置加加后置加加,易出错题目
- Banner图片都是全屏宽度在不同分辨率下都是显示图片的中间部分
- IOS开发系列--Objective-C之协议、代码块、分类
- Graph DataBase介绍