All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.javaclub.configcenter.client.util.LocalFileUtils Maven / Gradle / Ivy

The newest version!
package com.github.javaclub.configcenter.client.util;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;


public class LocalFileUtils {
	
	public static String rootPath = "/data/javaclub/midware";
	
	static { // 主要是为了确保 "xxx/config-client" 目录已创建
		try {
			File defaultDir = new File(rootPath);
			tryMakeDirs(defaultDir);
			
			File configclientDir = new File(rootPath + "/config-client");
			if(defaultDir.exists() && configclientDir.exists() && configclientDir.isDirectory()) {
				rootPath = rootPath + "/config-client";
			} else if(defaultDir.exists() && defaultDir.canWrite()) {
				rootPath = rootPath + "/config-client";
				FileUtils.forceMkdir(new File(rootPath));
			} else {
				rootPath = ".javaclub/midware/config-client";
				String parentPath = PropUtil.getSystemProperty("user.dir", "");
				if (Utils.isNotBlank(parentPath)) {
					rootPath = parentPath + System.getProperty("file.separator") + ".javaclub/midware/config-client";
				}
				FileUtils.forceMkdir(new File(rootPath));
			}
		} catch (IOException e) {
			System.out.println(e);
		}
	}
	
	public static String getLocalSaveDirPath() throws IOException {
		String path = rootPath + System.getProperty("file.separator") + "data";
		File dataDir = new File(path);
		
		if(dataDir.exists() && !dataDir.isDirectory()) {
			throw new IOException("ConfigData storePath=[" + path + "] exists but not a directory.");
		}
		if(!dataDir.exists()) {
			FileUtils.forceMkdir(dataDir);
		}
		return path;
	}
	
	public static String getLocalLogDirPath() throws IOException {
		File logDir = new File(rootPath + System.getProperty("file.separator") + "log");
		FileUtils.forceMkdir(logDir);
		return rootPath + System.getProperty("file.separator") + "log";
	}
	
	public static void tryMakeDirs(File directory) {
		if(null != directory && !directory.exists()) {
			try {
				FileUtils.forceMkdir(directory);
			} catch (Exception e) {
			}
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy