Java调用linux指令工具类,直接执行cmd,执行grep指令返回结果,执行sed追加指令,hdfs下载指令,获取文件行数
问题背景
有时候会操作linux指令读取或修改文件,封装一个常用的Linux工具类比较方便
LinuxUtils工具类
1 LinuxUtils工具类
package com.yg.util;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
/**
* @author suolong
* @time 2022-03-19 10:57
**/
@Slf4j
public class LinuxUtils {
public static boolean downloadHdfsFile(String hdfsPath, String outputPath) {
String cmd = "hdfs dfs -cat " + hdfsPath + "/* > " + outputPath;
log.info("downloadHdfsFile cmd:" + cmd);
return executeCmd(cmd);
}
public static void addHead(String filePath, String labelField) {
//在test.txt文件中的第一行加入id appname msg-cn
// 1i表示在第一行追加 \t表示以制位符
//sed -i "1iid\tappnmae\tmsg-cn" test.txt
String headcmd = "sed -i \"1iid\\t";
if (org.apache.commons.lang3.StringUtils.isNotEmpty(labelField)) {
String[] labelArray = labelField.split(",");
for (int i = 0; i < labelArray.length; i++) {
if (i < labelArray.length - 1) {
headcmd = headcmd + labelArray[i] + "\\t";
} else {
headcmd = headcmd + labelArray[i] + "\" ";
}
}
headcmd = headcmd + filePath;
} else {
headcmd = "sed -i \"1iid" + "\" " + filePath;
}
log.info("add head===headcmd:" + headcmd + "====" + LinuxCmdUtils.getGrepCmdReturn(headcmd));
return;
}
public static boolean executeCmd(String commandStr) {
log.info("run command = " + commandStr);
String[] cmd = {"/bin/sh", "-c", commandStr};
boolean isSuc = false;
try {
Process proc = Runtime.getRuntime().exec(cmd);
if (proc.waitFor() == 0) {
isSuc = true;
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return isSuc;
}
public static boolean executeLinuxCmd(String cmd) {
boolean result = false;
System.out.println("got cmd : " + cmd);
Runtime run = Runtime.getRuntime();
//InputStream in=null;
try {
Process process = run.exec(cmd);
//执行结果 0 表示正常退出
int exeResult = process.waitFor();
if (exeResult == 0) {
System.out.println("执行成功");
result = true;
}
} catch (Exception e) {
log.error("LinuxCmdUtils.executeLinuxCmd error", e);
}
return result;
}
/**
* 获取linux命令执行的结果,cat 之类
*
* @param cmd
* @return
*/
public static String getCmdResult(String cmd) {
String result = "";
try {
Process process = Runtime.getRuntime().exec(cmd);
InputStreamReader ir = new InputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
while ((line = input.readLine()) != null) {
result = line;
}
} catch (IOException e) {
System.err.println("IOException " + e.getMessage());
}
return result;
}
/**
* grep 类的shell命令
*
* @param cmdStr
* @return
*/
public static String getGrepCmdReturn(String cmdStr) {
String[] cmd = new String[3];
cmd[0] = "/bin/sh";
cmd[1] = "-c";
cmd[2] = cmdStr;
//得到Java进程的相关Runtime运行对象
Runtime runtime = Runtime.getRuntime();
StringBuffer stringBuffer = null;
try {
Process process = runtime.exec(cmd);
BufferedReader bufferReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
stringBuffer = new StringBuffer();
String temp = null;
while ((temp = bufferReader.readLine()) != null) {
stringBuffer.append(temp);
}
} catch (IOException e) {
e.printStackTrace();
}
return stringBuffer.toString();
}
public static int getFileRows(String path) {
FileReader in;
int rows = 0;
try {
in = new FileReader(path);
LineNumberReader reader = new LineNumberReader(in);
reader.skip(Long.MAX_VALUE);
rows = reader.getLineNumber();
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
return rows;
}
public static void main(String[] args) {
System.out.println(LinuxCmdUtils.getGrepCmdReturn("dmidecode -s system-serial-number|grep -v \"#\""));
}
}
作为程序员第 80 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …