安卓学习-文件管理

安卓学习-文件管理_第1张图片

安卓学习-文件管理_第2张图片

安卓学习-文件管理_第3张图片

安卓学习-文件管理_第4张图片

 

 

 

安卓学习-文件管理_第5张图片

安卓学习-文件管理_第6张图片

安卓学习-文件管理_第7张图片

安卓学习-文件管理_第8张图片

案例:安卓学习-文件管理_第9张图片安卓学习-文件管理_第10张图片

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context="com.example.administrator.filemanager.MainActivity"
android:id="@+id/读取私有文件">


<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="写入私有文件"
android:onClick="writePrivateFileClick"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取私有文件"
android:onClick="readPrivateFileClick"
android:id="@+id/button2"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取res/raw文件"
android:id="@+id/button3"
android:onClick="readRAWFileClick"
android:layout_below="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="match_parent"
android:layout_height="150dp"
android:text=""
android:id="@+id/textView"
android:scrollbars = "vertical"
android:layout_below="@+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="写入私有缓存数据"
android:onClick="writePrivateCacheDataClick"
android:id="@+id/button4"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="判断是否有外部存储设备"
android:id="@+id/button5"
android:onClick="isSDCardClick"
android:layout_below="@+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="写入外部存储的私有文件"
android:onClick="writeSDcardPrivateFileClick"
android:id="@+id/button6"
android:layout_below="@+id/button5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="写入外部存储私有缓存文件"
android:id="@+id/button7"
android:onClick="writeSDcardPrivateCacheFileClick"
android:layout_below="@+id/button6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

</RelativeLayout>

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();
}
}

}

你可能感兴趣的:(安卓学习-文件管理)