Java IO编程

//----源文件FileHelper.java
package mypackage;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.nio.charset.Charset;


/**
 * 操作文件或文件夹的助手类
 * @author dream
 *
 */
public class FileHelper 
{
	
	/**
	 * 删除文件夹下的所有子文件夹和文件
	 * @param path
	 * @return True删除成功 False 删除失败
	 */
	public static boolean DeleteDirecotry(String path)
	{
		File files=null;
		try
		{
			files=new File(path);
		}
		catch(Exception ex)
		{
			//直接返回False
			return false;
		}
		File[] filelist=files.listFiles();
		for(int i=0;i<filelist.length;i++)
		{
			if(filelist[i].isDirectory())
			{
				DeleteDirecotry(filelist[i].toString());
			}
			else
			{
				if(!filelist[i].delete())
				{
					return false;
				}
			}
		}
		if(!files.delete())
		{
			return false;
		}
		return true;
	}
	
	/**
	 * 读取文本文件的内容并返回
	 * @param filepath 要读取的文本文件路径
	 * @return 文件的字符串内容,若文件为空或者不存在则返回null
	 */
	public static String ReadAllText(String filepath)
	{
		try
		{
			//检查文件是否存在
			File file=new File(filepath);
			if(!file.exists())
			{
				return null;
			}
			//读取文件内容
			StringBuffer sb=new StringBuffer();
			BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file),Charset.defaultCharset()));
			String line=null;
			while((line=br.readLine())!=null)
			{
				sb.append(line+"\n");
			}
			br.close();
			return sb.toString();
		}
		catch(Exception ex)
		{
			//不处理
			return null;
		}
	}
	
	/**
	 * 将文本写入指定的文件中,若文件不存在则创建否则添加写入
	 * @param content 要写入的数据
	 * @param filepath 要写入数据的文件路径
	 * @param codeing 编码格式
	 * @param style 写入方式
	 * @return True成功 False失败
	 */
	public static boolean WriteAllText(String content,String filepath,WriteStyle style)
	{
		//检查文件是否存在(创建新文件则不检查)
		if(style!=WriteStyle.create)
		{
			if(!FileExists(filepath))
			{
				return false;
			}
		}
		
		//追加写入
		if(style==WriteStyle.append)
		{
			try
			{
				FileOutputStream fos=new FileOutputStream(filepath,true);
			    byte[] buffer=content.getBytes(Charset.defaultCharset());
			    
			    int n=0;
			    while(n<buffer.length)
			    {
			    	fos.write(buffer[n]);
			    	n++;
			    }
			    //关闭流
			    fos.close();
			    return true;
			}
			catch(Exception ex)
			{
				return false;
			}
		}
		
		//覆盖写入
		if(style==WriteStyle.overlaid)
		{
			try
			{
				FileOutputStream fos=new FileOutputStream(filepath);
				byte[] buffer=content.getBytes(Charset.defaultCharset());
				int n=0;
				while(n<buffer.length)
				{
				    fos.write(buffer[n]);
				    n++;
				}
				//关闭流
				fos.close();
				return true;
			}
			catch(Exception ex)
			{
				return false;
			}
		}
		
		//创建新文件写入
		if(style==WriteStyle.create)
		{
			if(FileExists(filepath))
			{
				return false;
			}

			try
			{
				//创建文件
				File file=new File(filepath);
				if(!file.createNewFile())
				{
					return false;
				}
				
				//写入数据
				byte[] buffer=content.getBytes(Charset.defaultCharset());
				FileOutputStream fos=new FileOutputStream(file);
				int n=0;
				while(n<buffer.length)
				{
					fos.write(buffer[n]);
					n++;
				}
				fos.close();
				return true;
			}
			catch(Exception ex)
			{
				return false;
			}
		}
		return false;
	}
	
	/**
	 * 检查指定文件是否存在
	 * @param file 文件路径
	 * @return true存在 false 不存在
	 */
	public static boolean FileExists(String filepath)

	{
		try
		{
			File file=new File(filepath);
			if(file.exists())
			{
				return true;
			}
			return false;
		}
		catch(Exception ex)
		{
			return false;
		}
	}
	
	public static boolean WriteBytes(byte[] data,String filepath)
	{
		/*if(!FileExists(filepath))
		{
			return false;
		}*/
		
		try
		{
			//建立输出流
			DataOutputStream dos=new DataOutputStream(new FileOutputStream(filepath));
			dos.write(data);
			dos.close();
			return true;
		}
		catch(Exception ex)
		{
			return false;
		}
		
	}

}


//-----源文件WriteStyle.java

package mypackage;

/**  * 写入文本内容方式的枚举  * @author dream  *  */ public enum WriteStyle {  append,  overlaid,  create }


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值