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