博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
安卓学习-文件管理
阅读量:6601 次
发布时间:2019-06-24

本文共 5661 字,大约阅读时间需要 18 分钟。

hot3.png

 

 

 

案例:

activity_main.xml

MainActivity.java

package com.example.administrator.filemanager; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Environment; import android.support.v7.app.ActionBarActivity; import android.text.method.ScrollingMovementMethod; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView; import android.widget.Toast; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; public class MainActivity extends Activity {
private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView); textView.setTextSize(14); } /*写入私有文件*/ public void writePrivateFileClick(View view) {
try {
//Context.MODE.PRIVATE 创建私有文件 APPEND是增加内容 OutputStream out = openFileOutput("xfei.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(); } } /*读取私有文件*/ public void readPrivateFileClick(View view) {
try {
InputStream in = openFileInput("xfei.txt"); byte[] bytes = new byte[1024]; StringBuffer sb = new StringBuffer(); int len = -1; while ((len = in.read(bytes)) != -1) {
sb.append(new String(bytes, 0, len)); } in.close(); textView.setText(" " + sb); // Toast.makeText(this,sb,Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) {
e.printStackTrace(); } catch (IOException e) {
e.printStackTrace(); } } //读取目录raw下的文件 public void readRAWFileClick(View view) {
try {
InputStream in = getResources().openRawResource(R.raw.xfei); byte[] bytes = new byte[1024]; StringBuffer sb = new StringBuffer(); 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(); textView.setText(" " + sb); textView.setMovementMethod(ScrollingMovementMethod.getInstance()); } catch (FileNotFoundException e) {
e.printStackTrace(); } catch (IOException e) {
e.printStackTrace(); } } //写入一个内部私有缓存文件 public void writePrivateCacheDataClick(View view) {
//创建一个缓存的文件 //String temp= getCacheDir()+"/temp.tmp"; //System.out.println(); try {
File temp = File.createTempFile("temp", null, getCacheDir()); FileOutputStream out = new FileOutputStream(temp); PrintStream ps = new PrintStream(out); ps.print("我可是要成为编程王的男人"); ps.close(); out.close(); } catch (IOException e) {
e.printStackTrace(); } } public void isSDCardClick(View view) {
//判断是否有SD卡 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
Toast.makeText(this, "有SD卡", Toast.LENGTH_SHORT).show(); if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
Toast.makeText(this, "SDCard只读", Toast.LENGTH_SHORT).show(); } else {//sdcard路径 System.out.println(Environment.getExternalStorageDirectory().getPath()); //访问android内置的文件夹 System.out.println(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)); } } else {
Toast.makeText(this, "没有SD卡", Toast.LENGTH_SHORT).show(); } } //写SD卡私有文件 public void writeSDcardPrivateFileClick(View view) {
//获得路径 File file = getExternalFilesDir(null); if (file != null) {
try {
FileOutputStream out = new FileOutputStream(file + "/xfei.txt"); PrintStream ps = new PrintStream(out); ps.print("今天吃药没?"); ps.close(); out.close(); } catch (FileNotFoundException e) {
e.printStackTrace(); } catch (IOException e) {
e.printStackTrace(); } } } //写入SD卡 私有缓存文件 public void writeSDcardPrivateCacheFileClick(View v) {
try {
File temp= File.createTempFile("xfei",null,getExternalCacheDir()); if (temp != null) {
FileOutputStream out = new FileOutputStream(temp); PrintStream ps = new PrintStream(out); ps.print("今天吃药没?给你药吃"); ps.close(); out.close(); } } catch (IOException e) {
e.printStackTrace(); } } }

转载于:https://my.oschina.net/xiaofeiandroid/blog/651777

你可能感兴趣的文章
linux所有命令失效的解决办法
查看>>
力扣算法题—085最大矩阵
查看>>
svs 在创建的时候 上传文件夹 bin obj 这些不要提交
查看>>
mysql-用命令导出、导入表结构或数据
查看>>
Tinkphp
查看>>
EntityFrameworkCore 一对一 && 一对多 && 多对多配置
查看>>
How to temporally disable IDE tools (load manually)
查看>>
Vue.js学习 Item4 -- 数据双向绑定
查看>>
几种排序方式的java实现(01:插入排序,冒泡排序,选择排序,快速排序)
查看>>
test--构造函数写法
查看>>
server application unavailable
查看>>
浅谈尾递归的优化方式
查看>>
eclipse 的小技巧
查看>>
亲测安装php
查看>>
频率域滤波
查看>>
图片存储类型的种类、特点、区别
查看>>
GETTING UP AND RUNNING WITH NODE.JS, EXPRESS, JADE, AND MONGODB
查看>>
求二叉树第K层节点的个数
查看>>
关于cocos2d-x面试的问题
查看>>
MySQLs数据库建外键时自动跑到缩影处,真奇怪
查看>>