与文件路径相关操作

与文件路径相关的操作

(1)判断给定路径是否是绝对路径

例如d:\bin和E:是绝对路径,而..\temp 和 test\ccc 不是绝对路径

public static final String OSNAME = System.getProperty("os.name");
public static boolean isWindows = false;
	public static boolean isHP_UX = false;
	public static boolean isSolaris = false;
	public static boolean isOS32bit = true;
static {
		if (SystemHWUtil.OSNAME.toLowerCase().contains("window")) {
			isWindows = true;
		}
		if (SystemHWUtil.OSNAME.toLowerCase().contains("hp-ux")) {
			isHP_UX = true;
		}
		if (SystemHWUtil.OSNAME.toLowerCase().contains("Solaris")) {
			isSolaris = true;
		}
		if (SystemHWUtil.OSARCH.contains("64")) {
			isOS32bit = false;
		}
	}

/***
	 * Determine whether it is an absolute path.
	 * 
	 * @param input
	 * @return
	 */
	public static boolean isAbsolutePath(String input) {
		if (ValueWidget.isNullOrEmpty(input)) {
			throw new RuntimeException("can not be null");
		}
		if (isWindows) {
			return input.matches("^[a-zA-Z]:\\\\?.*");//"^[a-zA-Z]:\\\\.*"
		} else {
			return input.matches("^/.*");
		}
	}

 测试代码:

@Test
	public void test_isAbsolutePath(){
		String path="d:\\bin";
		System.out.println(SystemHWUtil.isAbsolutePath(path));
	}

 执行结果:true

 

 

(2)打开指定路径

/***
	 * 
	 * @param folder
	 *            : directory
	 */
	public static void open_directory(Object folderObj) {
		if (ValueWidget.isNullOrEmpty(folderObj)) {
			return;
		}
		File file = null;
		if (folderObj instanceof JTextField) {
			JTextField tf = (JTextField) folderObj;
			file = new File(tf.getText());
		} else if (folderObj instanceof String) {
			file = new File((String) folderObj);
		} else {
			file = (File) folderObj;
		}
		if (!file.exists()) {
			return;
		}
		Runtime runtime = null;
		try {
			runtime = Runtime.getRuntime();
			if (!SystemHWUtil.isWindows) {
				// System.out.println("is linux");
				runtime.exec("nautilus " + file.getAbsolutePath());
			} else {
				runtime.exec("cmd /c start explorer " + file.getAbsolutePath());
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			if (null != runtime) {
				runtime.runFinalization();
			}
		}
	}

	/***
	 * 
	 * @param filePath
	 *            : only regular file
	 */
	public static boolean open_file(Object folderObj) {
		if (ValueWidget.isNullOrEmpty(folderObj)) {
			return false;
		}
		File file = null;
		if (folderObj instanceof JTextField) {
			JTextField tf = (JTextField) folderObj;
			file = new File(tf.getText());
		} else if (folderObj instanceof String) {
			file = new File((String) folderObj);
		} else {
			file = (File) folderObj;
		}
		if (!file.exists()) {
			return false;
		}
		Runtime runtime = null;
		try {
			runtime = Runtime.getRuntime();
			if (!SystemHWUtil.isWindows) {
				// System.out.println("is linux");
				runtime.exec("nautilus " + file.getAbsolutePath());
			} else {
				runtime.exec("cmd /c start explorer /select,/e, "
						+ file.getAbsolutePath());
			}
		} catch (IOException ex) {
			ex.printStackTrace();
			return false;
		} finally {
			if (null != runtime) {
				runtime.runFinalization();
			}
		}
		return true;
	}

 

见附件中类com.io.hw.file.util.FileUtils

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值