android 数据存储操作3:文件读写

文件读写在android中使用不是很经常,但是也是会用的的,比如对程序运行错误日志输出到文件里再上传的服务器等都可以使用文件来存储,整理了一些通用的操作,方便以后使用

 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;

/**
 * 
 * @author Administrator
 * @date 2015年1月29日 下午2:01:06
 */
public class FileUtil
{
	/**
	 * 拷贝文件
	 * @param from 原始文件
	 * @param to 目标文件
	 * @return
	 * @author Administrator
	 * @date 2015年1月29日 下午2:05:22
	 */
	public static final boolean copyFile(File from, File to)
	{
		boolean ret = true;

		InputStream is = null;
		OutputStream os = null;
		int len = 0;
		byte[] buf = new byte[1024];
		
		if(!from.exists()){
			//文件不存在
			return false;
		}
		if(!to.exists()){
			
		}
		try
		{
			is = new FileInputStream(from);
			os = new FileOutputStream(to);

			while ((len = is.read(buf)) != -1)
			{
				os.write(buf, 0, len);
			}

		}
		catch (Exception e)
		{
			e.printStackTrace();

		}
		finally
		{
			if (os != null)
			{
				try
				{
					os.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
				os = null;
			}

			if (is != null)
			{
				try
				{
					is.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
				is = null;
			}
		}
		return ret;
	}

	/**
	 * 根据路径拷贝文件
	 * @param source 原始文件路径
	 * @param dest 目标文件路径
	 * @return
	 * @author Administrator
	 * @date 2015年1月29日 下午2:06:13
	 */
	public static boolean copyFile(String source, String dest)
	{
		File in = new File(source);
		File out = new File(dest);
		return FileUtil.copyFile(in, out);
	}

	/**
	 * 创建目录
	 * @param path
	 * @author Administrator
	 * @date 2015年1月29日 下午2:03:12
	 */
	public static void makeDir(String path)
	{
		File file = new File(path);
		if (file.getParentFile().exists())
		{
			file.mkdir();
		}
		else
		{
			makeDir(file.getParent());
			file.mkdir();
		}
	}
	/**创建新文件
     * @param path 目录
     * @param filename 文件名
     * @throws IOException
     */
    public boolean createFile(String path){
        File file=new File(path);
        if(!file.exists()){
        	if (!file.getParentFile().exists())
        		makeDir(file.getParent());
        	try
			{
				file.createNewFile();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}finally{
				if(file.exists())
					return true;
			}
        }
		return false;	
    } 

	/**
	 * 删除文件
	 * @param filePath
	 * @return
	 * @author Administrator
	 * @date 2015年1月29日 下午2:03:01
	 */
	public static boolean deleteFile(String filePath)
	{
		File file = new File(filePath);
		if (file.exists())
		{
			return file.delete();
		}
		return false;
	}

	/**
	 * 递归删除文件夹
	 * @param path
	 */
	public static void delDir(String path)
	{
		File dir = new File(path);
		if (dir.exists())
		{
			File[] tmp = dir.listFiles();
			for (int i = 0; i < tmp.length; i++)
			{
				if (tmp[i].isDirectory())
				{
					delDir(path + "/" + tmp[i].getName());
				}
				else
				{
					tmp[i].delete();
				}
			}
			dir.delete();
		}
	}

	/**
	 * 根据路径判断文件是否存在
	 * @param filePath 文件路径
	 * @return
	 * @author Administrator
	 * @date 2015年1月29日 下午2:01:38
	 */
	public static boolean isExists(String filePath)
	{
		File file = new File(filePath);
		if (file.exists())
		{
			return true;
		}
		return false;
	}

	/**
	 * 转移文件目录
	 * @param filename 文件名
	 * @param oldpath 旧目录
	 * @param newpath 新目录
	 * @param cover 若新目录下存在和转移文件具有相同文件名的文件时,是否覆盖新目录下文件,cover=true将会覆盖原文件,否则不操作
	 */
	public void changeDirectory(String filename, String oldpath,
			String newpath, boolean cover)
	{
		if (!oldpath.equals(newpath))
		{
			File oldfile = new File(oldpath + "/" + filename);
			File newfile = new File(newpath + "/" + filename);
			// 若在待转移目录下,已经存在待转移文件
			if (newfile.exists())
			{
				if (cover)// 覆盖
					oldfile.renameTo(newfile);
				else
					System.out.println("在新目录下已经存在:" + filename);
			}
			else
			{
				oldfile.renameTo(newfile);
			}
		}
	}

	/**
	 * 获取文件的文本信息 在IO操作中,利用BufferedReader和BufferedWriter会比FileInputStream
	 * 和OutputStream效率会更高一点
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public String FileBufferedReader(String path) throws IOException
	{
		File file = new File(path);
		if (!file.exists() || file.isDirectory())
			throw null;
		BufferedReader br = new BufferedReader(new FileReader(file));
		StringBuffer sb = new StringBuffer();
		String temp = null;
		try
		{
			temp = br.readLine();
			while (temp != null)
			{
				sb.append(temp + "\n");
				temp = br.readLine();
			}
			br.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (br != null)
			{
				try
				{
					br.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
				br = null;
			}
		}
		return sb.toString();
	}

	/**
	 * 利用PrintStream写文件
	 * @param str  需要写的文本
	 * @param path 文件路径
	 * @author Administrator
	 * @date 2015年1月29日 下午3:30:10
	 */
	public void PrintStreamDemo(String str,String path)
	{
		String[] ss = str.split("\n");
		FileOutputStream out = null;
		PrintStream p = null;
		try
		{
			out = new FileOutputStream(path);
			p = new PrintStream(out);
			for (int i = 0; i < ss.length; i++)
				p.println(ss[i]);
			
			out.close();
			p.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}finally{
			if (out != null)
			{
				try
				{
					out.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
				out = null;
			}
			if (p != null)
			{
				p.close();
				p = null;
			}
		}
	}

	/**
	 * 利用StringBuffer写文件
	 * @param str
	 * @param path
	 * @author Administrator
	 * @date 2015年1月29日 下午3:45:57
	 */
	public void StringBufferDemo(String str,String path)
	{
		File file = new File(path);
		String[] ss = str.split("\n");
		FileOutputStream out = null;
		try
		{
			out = new FileOutputStream(file);
			for (int i = 0; i < ss.length; i++)
			{
				StringBuffer sb = new StringBuffer();
				sb.append(ss[i]);
				out.write(sb.toString().getBytes("utf-8"));
			}
			out.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}finally{
			if (out != null)
			{
				try
				{
					out.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
				out = null;
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值