Android 操作文件类

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;


import android.os.Environment;


public class FileUtil
{


private String SDCardRoot;


public FileUtil()
{
// 得到当前外部存储设备目录
SDCardRoot = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
}


/**
* 在SDCard上创建文件
* */
public File CreateFileInSDCard(String fileName, String dir) throws Exception
{


// 如果文件存在,先删除
if (IsFileExist(fileName, SDCardRoot + dir + File.separator) == true)
{
DeleteFile(fileName, SDCardRoot + dir + File.separator);
}
// 创建文件
File file = new File(SDCardRoot + dir + File.separator + fileName);
file.createNewFile();
return file;
}


/**
* 在SDCard上创建目录
* */
public File CreateSDDir(String dir)
{
File file = new File(SDCardRoot + dir + File.separator);


return file;
}


/**
* 判断SDCard上的文件是否存在
* */
public boolean IsFileExist(String fileName, String path)
{
File file = new File(SDCardRoot + path + File.separator + fileName);
return file.exists();
}


/**
* 创建文件
*/
public static boolean CreateFile(String path)
{
try
{
File dir = new File(path);
if (dir.exists() == true)
dir.delete();


dir.getParentFile().mkdirs();
return true;


}
catch (Exception e)
{
return false;
}
}


/**
* 删除文件

* @param fileName
* @param path
*/
public void DeleteFile(String fileName, String path)
{


File file = new File(SDCardRoot + path + File.separator + fileName);
file.delete();
}


/**
* 删除文件夹下的所有文件

* @param path
*            文件夹路径
*/
public static void DeleteDirectory(String path)
{
File root = new File(path);
File[] currentFiles = root.listFiles();
if (currentFiles != null)
{
for (int n = 0; n < currentFiles.length; n++)
{
File file = currentFiles[n];
if (file.exists())
file.delete();
}
}
}


/**
* 递归删除文件和文件夹

* @param path
*            要删除的根目录
*/
public static void RecursionDeleteFile(String path)
{
File file = new File(path);
if (file.isFile())
{
file.delete();
return;
}
if (file.isDirectory())
{
File[] childFile = file.listFiles();
if (childFile == null)
{
file.delete();
return;
}
for (File f : childFile)
{
RecursionDeleteFile(f.getPath());
}
file.delete();
}
}


/**
* 删除文件

* @param filePath
*            文件路径
*/
public void DeleteFile(String filePath)
{
File file = new File(filePath);
if (file.exists())
file.delete();
}


/**
* 将一个InputStream写入SDCard
* */
public File WriteToSDCardFromInput(String path, String fileName, InputStream inputStream)
{
File file = null;
OutputStream outputStream = null;


try
{
CreateSDDir(path);
file = CreateFileInSDCard(fileName, path);


outputStream = new FileOutputStream(file);
byte data[] = new byte[4 * 1024];
int tmp;
while ((tmp = inputStream.read(data)) != -1)
{
outputStream.write(data, 0, tmp);
}
outputStream.flush();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
outputStream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
return file;
}


/**
* @param file
* @return
*/
static public byte[] GetBytesFromFile(File file)
{
if (file == null)
{
return null;
}
try
{


FileInputStream inStream = new FileInputStream(file);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;


while ((len = inStream.read(buffer)) != -1)
{
// 写入数据
outStream.write(buffer, 0, len);
}
// 得到文件的二进制数据


inStream.close();
outStream.close();
return outStream.toByteArray();
}
catch (IOException e)
{


}
return null;
}


/**
* 复制文件

* @param oldPath
* @param newPath
*/
public static boolean CopyFile(String oldPath, String newPath)
{
try
{
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists())
{
// 文件存在时
InputStream inStream = new FileInputStream(oldPath); // 读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1024];


while ((byteread = inStream.read(buffer)) != -1)
{
bytesum += byteread; // 字节数 文件大小
fs.write(buffer, 0, byteread);
}
inStream.close();


return true;
}
}
catch (Exception e)
{
System.out.println("复制单个文件操作出错");
e.printStackTrace();


}


return false;
}


/**
* 将文本写入文件

* @param path
* @param strContext
* @return
*/
public static boolean WriteTextToFile(String path, String strContext)
{
FileOutputStream fis = null;
try
{
File file = new File(path);


if (file.exists() == false)
file.getParentFile().mkdirs();


fis = new FileOutputStream(path);
byte[] bytes = strContext.getBytes();
fis.write(bytes, 0, bytes.length);
}
catch (IOException e)
{
return false;
}
catch (Exception e)
{
return false;
}
finally
{
try
{
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e2)
{
}
}


return true;
}


/**
* 从文件中获取文本

* @param path
* @return
*/
public static String ReadTextFromFile(String path)
{
File file = new File(path);


if (!file.exists())
{
try
{
file.createNewFile();
}
catch (IOException e)
{
e.printStackTrace();
}
return "";
}
StringBuffer buffer = new StringBuffer();
FileInputStream fis = null;
InputStreamReader isr = null;
Reader in = null;
try
{


fis = new FileInputStream(file);
isr = new InputStreamReader(fis);// 文件编码Unicode,UTF-8,ASCII,GB2312,Big5
in = new BufferedReader(isr);
int ch;
while ((ch = in.read()) > -1)
{
buffer.append((char) ch);
}
fis.close();
isr.close();
in.close();


return buffer.toString();


}
catch (IOException e)
{
return "";
}
}


/**
* 若strPath目录不存在,则创建

* @param strPath
*/
public static boolean CreateDirectory(String dirPath)
{
try
{
File dir = new File(dirPath);
if (dir.exists() && dir.isDirectory())
return true;
else
{
dir.getParentFile().mkdirs();
return true;
}
}
catch (Exception e)
{
return false;
}
}


/**
* 创建文件

* @param filePath
*            文件目录
* @param fileName
*            文件名
* @return
*/
public static File NewFile(String filePath, String fileName)
{
File file = null;
try
{
file = new File(filePath, fileName);
file.delete();
file.createNewFile();
}
catch (IOException e)
{
e.printStackTrace();
}
return file;
}


/**
* 把文件数据写入文件

* @param file
*            要写入文件
* @param data
*            文件数据
* @param offset
*            写入位置
* @param count
*            写入长度
* @throws IOException
*/
public static void WriteFile(File file, byte[] data, int offset, int count) throws IOException
{
FileOutputStream fos = new FileOutputStream(file, true);
fos.write(data, offset, count);
fos.flush();
fos.close();
}


/**
* 获取文件的字节数组

* @param filePath
*            文件目录
* @param fileName
*            文件名
* @throws IOException
*/
public static byte[] ReadFile(String filePath, String fileName) throws IOException
{
File file = new File(filePath, fileName);
file.createNewFile();
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
int leng = bis.available();
byte[] b = new byte[leng];
bis.read(b, 0, leng);
bis.close();
return b;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值