您的位置:首页 > 移动开发 > Android开发

android中一些文件的操作方法

2012-10-15 23:46 190 查看
1.创建文件

File Directory=mContext.getDir("mydir", Context.MODE_PRIVATE);
File dest = new File(Directory, filename);


2.读取图片文件

public static Drawable loadImageFromFilesystem(Context context,
String filename, int originalDensity) {
File Directory = context.getDir("mydir", Context.MODE_PRIVATE);
File f = new File( Directory , filename );
InputStream is = null;
try {
is = new FileInputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

Bitmap bmp = BitmapFactory.decodeStream(is );
Drawable drawable = new BitmapDrawable(bmp);  ;

return drawable;
}


3.读取文本文件

public static String read( Context context, String fileName )
{
try
{
FileInputStream in = context.openFileInput(fileName);
return readInStream(in);
}
catch (Exception e)
{
e.printStackTrace();
}
return "";
}

private static String readInStream(FileInputStream inStream)
{
try
{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[512];
int length = -1;
while((length = inStream.read(buffer)) != -1 )
{
outStream.write(buffer, 0, length);
}

outStream.close();
inStream.close();
return outStream.toString();
}
catch (IOException e)
{
Log.i("FileTest", e.getMessage());
}
return null;
}


4.删除文件(目录)

public static void delFolder(String folderPath) {
try {
delAllFile(folderPath); // 删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); // 删除空文件夹
} catch (Exception e) {
System.out.println("删除文件夹操作出错");
e.printStackTrace();
}
}

public static void delAllFile(String path) {
File file = new File(path);
if (!file.exists()) {
return;
}
if (!file.isDirectory()) {
return;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
delFolder(path + "/" + tempList[i]);// 再删除空文件夹
}
}
}


5.计算SD卡的剩余空间

public static long getFreeDiskSpace() {
String status = Environment.getExternalStorageState();
long freeSpace = 0;
if (status.equals(Environment.MEDIA_MOUNTED)) {
try {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
freeSpace = availableBlocks * blockSize / 1024;
} catch (Exception e) {
e.printStackTrace();
}
} else {
return -1;
}
return (freeSpace);
}


6.向手机写图片
public static boolean writeFile( byte[] buffer, String folder, String fileName )
{
boolean writeSucc = false;

boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

String folderPath = "";
if( sdCardExist )
{
folderPath = Environment.getExternalStorageDirectory() + File.separator +  folder + File.separator;
}
else
{
writeSucc =false;
}

File fileDir = new File(folderPath);
if(!fileDir.exists())
{
fileDir.mkdirs();
}

File file = new File( folderPath + fileName );
FileOutputStream out = null;
try
{
out = new FileOutputStream( file );
out.write(buffer);
writeSucc = true;
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try {out.close();} catch (IOException e) {e.printStackTrace();}
}

return writeSucc;
}


7.将输入流转换为字符串
/**
* 将输入流转化为字符串
* @param inputStream
* @return
*/
public String inStream2string(InputStream inputStream) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
int len = -1;
while ((len = inputStream.read(buf)) != -1) {
baos.write(buf, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new String(baos.toByteArray());
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: