java File操作

/**
	 * 创建文件夹目录
	 * @param pathName 文件夹路径
	 */
	public  Boolean CreateFloder (String pathName) {
		File myFolder = new File(pathName);
		try {
			if (!myFolder.exists()) {
				myFolder.mkdirs();  //mkdir()只能创建一级目录,mkdirs()可以创建多级目录
			}
			return true;
		} catch (Exception e) {
			System.out.println("创建文件夹目录操作失败!");
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 删除目录
	 * @param pathName
	 */
	public  Boolean DelFolder (String pathName) {
		File delFolderPath = new File(pathName);
		try {
			delFolderPath.delete();  //则此目录必须为空才能删除
			return true;
		} catch (Exception e) {
			System.out.println("删除文件夹目录操作失败!");
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 创建文件
	 * @param pathName 路径+文件名
	 * @param content 要写入文件的内容
	 */
	public  Boolean CreateFile (String pathName,String content) {
		File myFilePath = new File(pathName);
		try {
			if (!myFilePath.exists()) {
				myFilePath.createNewFile();
			}
			FileWriter resultFile = new FileWriter(myFilePath);
			PrintWriter myFile = new PrintWriter(resultFile);
			myFile.println(content);
			//myFile.write(content);
			resultFile.close();
			return true;
		} catch (Exception e) {
			System.out.println("创建文件操作失败");
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 删除文件
	 * @param pathName 路径+文件名
	 */
	public  Boolean DelFile (String pathName) {
		File myDelFile = new File(pathName);
		try {
			myDelFile.delete();
			return true;
		} catch (Exception e) {
			System.out.println("删除文件操作失败");
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 删除该目录下的所有文件夹
	 * @param pathName
	 * @return
	 */
	public  Boolean DelFileFromFolder (String pathName) {
		File delFile = new File(pathName);
		try {
			File[] files = delFile.listFiles();
			for (File file : files) {
				if (file.isDirectory()){
					file.delete();
				}
			}
			return true;
		} catch (Exception e) {
			System.out.println("删除文件目录操作失败!");
			e.printStackTrace();
			return false;
		}
		
	}
/**
	 * 读取文件内容
	 * @param fileName
	 * @return
	 */
	public String ReadFile (File fileName) {
		try {
			FileReader fr = new FileReader(fileName);
			BufferedReader br = new BufferedReader(fr);
			String content = new String();
			String str = br.readLine();
			while (str != null) {
				System.out.println(str);
				content += str;
				str = br.readLine();
			}
			br.close();
			fr.close();
			return content;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return new String();
		} catch (IOException e) {
			e.printStackTrace();
			return new String();
		}
	}
	
	/**
	 * 写文件(删除原来的内容在写入)
	 * @param fileName
	 * @param content
	 * @return
	 */
	public Boolean WriteFile (File fileName,String content) {
		try {
			FileWriter fw = new FileWriter(fileName);
			fw.write(content);
			fw.flush();
			fw.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值