public 不会被删除掉
Java代码
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void MyOnclick(View v){
switch (v.getId()) {
case R.id.btn6:
long l = SdCardUtils.getLength();
Log.i("==SSS==", l+"");
break;
case R.id.btn7:
long l2 = SdCardUtils.getFreeLength();
Log.i("==SSS=2222=", l2+"");
break;
case R.id.btn8:
boolean b = SdCardUtils.isDelete("info.txt");
if(b){
Log.i("AAAA", "删除成功");
}else{
Log.i("AAAA", "删除失败");
}
break;
default:
break;
}
}
public void OnclickBtn(View v) {
switch (v.getId()) {
case R.id.btn1:// 测试外部存储是否可用
boolean isAble = SdCardUtils.isExtraStorage();
if (isAble) {
Log.i("==AAA==", "外部存储可用");
} else {
Log.i("==AAA==", "外部存储不可用");
}
break;
case R.id.btn2:
boolean isWrite = SdCardUtils.WriteStoragePublic("aaaaaaaaaaaaa",
"info.txt");
if (isWrite) {
Log.i("==AAA==", "写入外部存储");
} else {
Log.i("==AAA==", "没有写入外部存储");
}
break;
case R.id.btn3:
String str = SdCardUtils.readStoragePublic("info.txt");
Log.i("==AAA=str=", str);
break;
case R.id.btn4:
boolean isWrite1 = SdCardUtils.WriteStoragePrivate(MainActivity.this, "ssssss", "text.txt");
if (isWrite1) {
Log.i("==AAA==", "写入外部存储");
} else {
Log.i("==AAA==", "没有写入外部存储");
}
break;
case R.id.btn5:
String str1 = SdCardUtils.readStoragePrivate(MainActivity.this, "text.txt");
Log.i("==AAA=str1=", str1);
break;
default:
break;
}
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import android.content.Context;
import android.os.Environment;
public class SdCardUtils {
public static boolean isExtraStorage() {
/**
* 获取外部存储状态 外部可读性状态比较 相等为true 不相等 false
*/
Boolean bl = false;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
bl = true;
}
return bl;
}
/**
* 写入数据到public 外部存储
*
* @param contents
* @param fileName
* @return
*/
public static boolean WriteStoragePublic(String contents, String fileName) {
/**
* 外部存储文件:Environment.DIRECTORY_DOWNLOADS
*
* 路径:mnt/sdcard/Download/fileName
*/
File file = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
fileName);
boolean isWrite = false;
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(contents.getBytes());
isWrite = true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return isWrite;
}
/**
* 读出外部public 数据
*
* @return
*/
public static String readStoragePublic(String fileName) {
File file = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
fileName);
StringBuffer stringBuffer = new StringBuffer();
FileInputStream fileInputStream = null;
BufferedReader br = null;
try {
fileInputStream = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(fileInputStream));
String line = null;
while ((line = br.readLine()) != null) {
stringBuffer.append(line);
}
return stringBuffer.toString();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(fileInputStream!=null){
try {
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(br!=null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
/**
* private 和public 存储的路径不一样 private存到自己的包名下
*
* 写入数据到private外部存储
*
* @param contents
* @param fileName
* @return
*/
public static boolean WriteStoragePrivate(Context context,String contents, String fileName) {
/**
* 外部存储文件:
*
* 路径:mnt/sdcard/Android/data/包名/files/fileName
*/
File file = new File(context.getExternalFilesDir(null),fileName);
boolean isWrite = false;
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(contents.getBytes());
isWrite = true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return isWrite;
}
/**
* 读出外部private 数据
*
* @return
*/
public static String readStoragePrivate(Context context,String fileName) {
File file = new File(context.getExternalFilesDir(null),fileName);
StringBuffer stringBuffer = new StringBuffer();
FileInputStream fileInputStream = null;
BufferedReader br = null;
try {
fileInputStream = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(fileInputStream));
String line = null;
while ((line = br.readLine()) != null) {
stringBuffer.append(line);
}
return stringBuffer.toString();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(fileInputStream!=null){
try {
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(br!=null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
/**
* 获取文件字节总长度
* @return
*/
public static long getLength(){
File file = new File(Environment.getExternalStorageDirectory(),"");
long l = file.getTotalSpace();
return l;
}
/**
* 获取文件剩余字节总长度
* @return
*/
public static long getFreeLength(){
File file = new File(Environment.getExternalStorageDirectory(),"");
long l = file.getFreeSpace();
return l;
}
/**
* 根据文件名删除指定的数据
* @return
*/
public static boolean isDelete(String fileName){
File file = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_DOWNLOADS), fileName);
boolean b =file.delete();
return b;
}
}
import android.R.bool;
import android.test.AndroidTestCase;
public class TestCase extends AndroidTestCase{
public void IsHas(){
boolean b = SdCardUtils.isExtraStorage();
System.out.println(b);
}
}