文件操作工具类 内部文件 外部文件
来源:互联网 发布:复杂网络 混沌 编辑:程序博客网 时间:2024/06/08 18:58
public class FileUtils { private static final String EXTERNAL_FILE_DIR = "/moliying_dir"; private static final String EXTERNAL_CACHE_DIR = "/moliying_cache"; public static boolean isExternalStorageWritable(){ String state = Environment.getExternalStorageState(); //判断是否存在外部存储设备(可写) if(Environment.MEDIA_MOUNTED.equals(state)){ return true; } return false; } public static boolean isExternalStorageReadable() { String state = Environment.getExternalStorageState(); //判断sdcard是否可读 if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { return true; } return false; } /** * 获取sdcard的根目录 * @return */ public static String getSDCardPath(){ return Environment.getExternalStorageDirectory().getPath(); } /** * 获取外部存储的私有目录(4.4) * @param context * @return */ public static File getExternalFilesDir(Context context){ File file = context.getExternalFilesDir(null); if(file==null){ file = new File(Environment.getExternalStorageDirectory()+EXTERNAL_FILE_DIR); } return file; } /** * 获取外部存储的私有缓存目录(4.4) * @param context * @return */ public static File getCacheDir(Context context){ File file = context.getExternalCacheDir(); if(file==null){ file = new File(Environment.getExternalStorageDirectory()+EXTERNAL_CACHE_DIR); } return file; } /** * 获取sdcard的可用空间大小 * @return */ public static long getFreeSpace(){ return Environment.getExternalStorageDirectory().getFreeSpace(); } /** * 获取sdcard的总空间大小 * @return */ public static long getTotalSpace(){ return Environment.getExternalStorageDirectory().getTotalSpace(); }}
布局文件中对应的 按钮布局
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="写入私有数据" android:id="@+id/button_write_private" android:onClick="writeClick" android:layout_alignParentTop="true" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读取私有数据" android:id="@+id/button2_read_private" android:onClick="readClick" android:layout_below="@+id/button_write_private" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读取RAW文件" android:onClick="readRAWClick" android:id="@+id/button" android:layout_below="@+id/button2_read_private" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="assert文件" android:onClick="assertClick" android:id="@+id/button2" android:layout_below="@+id/button" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="内部缓冲文件" android:onClick="innerCacheClick" android:id="@+id/button3" android:layout_below="@+id/button2" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="检查sdcard" android:onClick="checkSdCardClick" android:id="@+id/button4" android:layout_below="@+id/button3" android:layout_alignParentStart="true" /></RelativeLayout>
MainActivity中的代码
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void checkSdCardClick(View view){ Log.i("sdcard",FileUtils.isExternalStorageWritable()+"-可写"); Log.i("sdcard",FileUtils.isExternalStorageReadable()+"-可读"); //获取sdcard上指定的目录: Log.i("dir",Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+""); Log.i("dir",Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS)+""); Log.i("dir",Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+""); Log.i("dir",Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)+""); Log.i("dir",Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+""); Log.i("dir",Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)+""); Log.i("dir",Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+""); Log.i("dir",Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS)+""); Log.i("dir",Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PODCASTS)+""); Log.i("dir",Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES)+""); } /** * 内部缓存文件操作 * 系统在内存低的情况下,会自动清除缓存,但通常我们应该自己维护缓存 * 一般大小在1MB * @param view */ public void innerCacheClick(View view){ String rootpath = "http://www.baidu.com/image/"; Log.i("cache",getCacheDir().toString()); try { //创建一个临时文件(前缀,后缀,文件目录)// File file = File.createTempFile("url","tmp",getCacheDir());// File file1 = getCacheDir();// File aa = file1.listFiles(); File file2 = new File(getCacheDir()+"/xxxxxx.tmp"); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file2)); bos.write("今天下雨了".getBytes()); bos.close(); } catch (IOException e) { e.printStackTrace(); } } public void assertClick(View v){ //只读 AssetManager am = getAssets(); try { InputStream in = am.open("my.txt"); BufferedInputStream bis = new BufferedInputStream(in); StringBuilder buf = new StringBuilder(); byte[] bytes = new byte[1024]; int len = -1; try { while ((len = bis.read(bytes)) != -1) { buf.append(new String(bytes, 0, len)); } bis.close(); } catch (IOException e) { e.printStackTrace(); } Toast.makeText(MainActivity.this, buf, Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); } } /** * 读取RAW文件(只读) * * @param v */ public void readRAWClick(View v) { BufferedInputStream in = new BufferedInputStream(getResources().openRawResource(R.raw.my)); StringBuilder buf = new StringBuilder(); byte[] bytes = new byte[1024]; int len = -1; try { while ((len = in.read(bytes)) != -1) { buf.append(new String(bytes, 0, len)); } in.close(); } catch (IOException e) { e.printStackTrace(); } Toast.makeText(MainActivity.this, buf, Toast.LENGTH_SHORT).show(); } public void writeClick(View view) { String info = "程序员好帅"; try { BufferedOutputStream out = new BufferedOutputStream(openFileOutput("mly", Context.MODE_PRIVATE)); out.write(info.getBytes()); out.close(); Toast.makeText(MainActivity.this, "write success", Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void readClick(View view) { try { BufferedInputStream in = new BufferedInputStream(openFileInput("mly")); StringBuilder buf = new StringBuilder(); byte[] bytes = new byte[1024 * 100]; int len = -1; while ((len = in.read(bytes)) != -1) { buf.append(new String(bytes, 0, len)); } in.close(); Toast.makeText(MainActivity.this, buf, Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}
0 0
- 文件操作工具类 内部文件 外部文件
- Android文件读写操作(assets 文件、 raw文件、内部存储文件、外部存储文件)
- 文件操作工具类
- 文件操作工具类
- 文件操作工具类
- 文件操作工具类
- 文件操作工具类
- 文件操作工具类
- 文件操作工具类
- 文件操作工具类
- 文件操作工具类
- 文件操作工具类
- 文件操作工具类
- 文件操作工具类
- 【内部】【外部】打开文件VBA代码
- Android中外部,内部文件读写
- 文件管理 内部存储 外部存储 Sdcard
- cs文件,外部类操作窗体控件
- hdu 1078 FatMouse and Cheese
- hjr-c#实习日记(二):asp.net控制器登录注册验证
- java中double和float精度丢失问题及解决方法
- java异常处理
- iOS kvc和kvo简介
- 文件操作工具类 内部文件 外部文件
- Hadoop笔记四之Hdfs文件读写操作
- STL——stack栈
- pointerIndex out of range--IllegalArgumentException
- C++基础:new的三种形式详解
- 103.项目视图分析工具Reveal集成使用教程
- 关于攻读计算机研究生
- 算法学习(一):递归与分治策略(2)
- 剑指offer 42题 【知识迁移能力】翻转单词顺序列