【学习笔记】【JAVA】创建目录或文件,达到能是否重命名,是否追加内容,是否覆盖。

205 阅读2分钟

废话不多说,直接上代码。

一、新写/重命名 文件

	/**
	 * 新写/重命名 文件
	 * @Method: writerData
	 * @author: nanfangzhe_xsz
	 * @date: 2021年8月2日
	 * @param path 文件目录
	 * @param txtName 文件名
	 * @param txt 文件内容
	 * @param isRename 文件是否重命名旧的,创建新的
	 * @param isAdd 是否内容追加(不追加就覆盖)
	 * @return boolean
	 */
	public static boolean writerData(String path, String txtName, String txt, boolean isRename, boolean isAdd) {
		String foldPath = path;
		try {
			System.out.println("-----------正在创建" + foldPath + "/" + txtName + "-----------");
			File textFile = new File(foldPath + "/" + txtName);
			File dirFile = new File(foldPath + "");
			if (!dirFile.exists()) {
				System.out.println("-----------目录不存在,正在创建" + foldPath + "-----------");
				dirFile.mkdirs();
			}
			// 不存在则创建
			if (!textFile.exists()) {
				textFile.createNewFile();
			} else {
				if (isRename) {
					SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
					String date = df.format(new Date());// new Date()为获取当前系统时间,也可使用当前时间戳
					String filePath = textFile.getAbsolutePath() + date;
					filePath = filePath.substring(0, filePath.lastIndexOf("."));
					filePath = filePath + date + ".js";
					boolean renameTo = textFile.renameTo(new File(filePath));
					if (!renameTo) {
						System.out.println("-----------重命名文件出错了!" + foldPath + "/" + txtName + "-----------");
						throw new RuntimeException();
					}
					System.out.println("-----------重命名文件完成!" + filePath + "-----------");
					boolean mkFile = textFile.createNewFile();
					if (!mkFile) {
						System.out.println("-----------重新创建文件出错了!" + foldPath + "/" + txtName + "-----------");
						throw new RuntimeException();
					}
				}
			}
			BufferedWriter writer = new BufferedWriter(
					new OutputStreamWriter(new FileOutputStream(textFile, isAdd), "utf-8"));
			// 写数据
			writer.write(txt);
			// 刷新流
			writer.flush();
			// 关闭资源
			writer.close();
			System.out.println("-----------" + txtName + "创建完成-----------");
			return true;
		} catch (Exception e) {
			System.out.println("-----------" + txtName + "创建失败-----------");
			e.printStackTrace();
			return false;
		}
	}

二、新写/重命名 文件目录

	/**
	 * 文件夹存在:原有的文件夹重命名(带时间);新建 文件夹不存在:新建
	 * 
	 * @Method: mkdirsFold
	 * @author: nanfangzhe_xsz    
	 * @date:  2021年8月2日
	 * @param foldPath 目录地址
	 * @param isMkdirsRename 目录存在,是否重命名
	 * @param isNewMkdirs 目录存在,重命名后,是否再新创建
	 * @return String
	 */
	public static String mkdirsFold(String foldPath, boolean isMkdirsRename, boolean isNewMkdirs) {
		File dirFile = new File(foldPath);
		try {
			if (!dirFile.exists()) {
				System.out.println("-----------正在创建" + foldPath + "-----------");
				dirFile.mkdirs();
				System.out.println("-----------创建完成" + foldPath + "-----------");
				return foldPath;
			} else {
				if (isMkdirsRename) {
					String dirFilePath = dirFile.getAbsolutePath();
					SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
					String date = df.format(new Date());// new
														// Date()为获取当前系统时间,也可使用当前时间戳
					dirFilePath = dirFilePath + date;
					System.err.println(dirFilePath);
					boolean renameTo = dirFile.renameTo(new File(dirFilePath));
					if (!renameTo) {
						System.out.println("-----------重命名目录出错了!" + dirFilePath + "-----------");
						return null;
					}
					if (isNewMkdirs) {
						boolean mkdirs = dirFile.mkdirs();
						if (!mkdirs) {
							System.out.println("-----------重新创建目录出错了!" + foldPath + "-----------");
							return null;
						}
					}
					String path = dirFile.getPath().replaceAll("\\\\", "/") + date;
					System.out.println("-----------已存在,已重命名并创建。" + path + "-----------");
					return path;
				}
				return foldPath;
			}
		} catch (Exception e) {
			System.out.println("-----------目录出错了!" + foldPath + "-----------");
			e.printStackTrace();
			return null;
		}
	}

三、查看文件

	// 查看文件
	public static String readFileContent(String fileName) {
		File file = new File(fileName);
		BufferedReader reader = null;
		StringBuffer sbf = new StringBuffer();
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempStr;
			while ((tempStr = reader.readLine()) != null) {
				sbf.append(tempStr);
			}
			reader.close();
			return sbf.toString();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		return sbf.toString();
	}