Android使用Linux命令执行文件操作

package com.android.xjf.utils;

import java.io.IOException;
import java.util.ArrayList;

/**
 * File operators. Like: delete, create, move...
 * Use linux shell to implement these operators.
 * So, only for linux like operator system. 
 * All return the execute process, and if the requested program can not be executed,
 * may throws IOException.
 * And all files or diretorys String parameters are absolute path.
 * The return new {@code Process} object that represents the native process.
 * */
public class LinuxFileCommand {
	public final Runtime shell;
	//private static final int c = 0;
	/**
	 * Constructor. /data/busybox/
	 * @param
	 * 		interface with the environment in which they are running
	 * */
	public LinuxFileCommand(Runtime runtime) {
		// TODO Auto-generated constructor stub
		shell = runtime;
	}
	
	/** Delete {@code file} file, nerver prompt*/
	public final Process deleteFile(String file) throws IOException{
		String[] cmds = {"rm", file};
		return shell.exec(cmds);
	}
	
	public final Process delete(String file) throws IOException {
		String[] cmds = {"rm", "-r", file};
		return shell.exec(cmds);
	}
	
	public final Process deleteMult(String[] cmds) throws IOException{
		return shell.exec(cmds);
	}
	
	/** Delete {@code dire} directory, nerver prompt*/
	public final Process deleteDirectory(String dire) throws IOException{
		String[] cmds = {"rm", "-r", dire};
		return shell.exec(cmds);
	}
	
	/** Create {@code file} file, if file has already exist, update the
	 * file access and modification time to current time.
	 * @throws IOException 
	 * */
	public final Process createFile(String file) throws IOException {
		String[] cmds = {"touch", file};
		return shell.exec(cmds);
	}
	
	/** Create directory. If parent path is not existed, also create parent directory
	 * @throws IOException */
	public final Process createDirectory(String dire) throws IOException{
		String[] cmds = {"mkdir", dire};
		return shell.exec(cmds);
	}
	
	/** Move or rename(if they in the same directory) file or directory
	 * @throws IOException */
	public final Process moveFile(String src, String dir) throws IOException{
		String[] cmds = {"mv", src, dir};
		return shell.exec(cmds);
	}
	
	/** Copy file
	 * @throws IOException */
	public final Process copyFile(String src, String dir) throws IOException{
		String[] cmds = {"cp", "-r", src, dir};
		return shell.exec(cmds);
	}
	
	/**`
	 * File soft Link
	 * @throws IOException 
	 * */
	public final Process linkFile(String src, String dir) throws IOException{
		String[] cmds = {"ln", "-l", src, dir};
		return shell.exec(cmds);
	}
	
	
	public final Process du_s(String file) throws IOException{
		String[] cmds = {"du", "-s", file};
		return shell.exec(cmds);
	}

	public final Process ls_lhd(String file) throws IOException{
		String[] cmds = {"ls", "-l", file};
		return shell.exec(cmds);
	}
	
	public final Process ls_Directory(String directory) throws IOException{
		if (directory.equals("/"))
			directory = "";
		String[] cmds = {"ls", "-a", directory};
		return shell.exec(cmds);
	}
	
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值