运行外部命令并获得返回结果
public static String runProcess(String command) {
StringBuilder builder = new StringBuilder();
try {
Process process = Runtime.getRuntime().exec(command);
try (SequenceInputStream input = new SequenceInputStream(process.getInputStream(),
process.getErrorStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(input))) {
String msg;
while ((msg = reader.readLine()) != null) {
builder.append(msg);
builder.append(System.lineSeparator());
}
process.waitFor();
process.destroy();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
if (builder.length()==0) {
return "";
} else {
return builder.substring(0, builder.length() - System.lineSeparator().length());
}
}
使用递归遍历文件夹及子文件夹中文件
public static List<File> filesDirs(File file,List<File> list) {
if (file != null) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File flies2 : files) {
filesDirs(flies2,list);
}
} else {
list.add(file);
System.out.println("文件名字" + file.getName());
}
} else {
System.out.println("文件不存在");
}
return list;
}
java写出txt文件
public static void writeTxt(String filePath,String content,Boolean isCover){
try {
FileWriter write=new FileWriter(filePath,!isCover);
BufferedWriter bw=new BufferedWriter(write);
bw.write(content);
bw.write("\n");
bw.close();
write.close();
}catch(IOException e){
e.printStackTrace();
}
}
版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://blog.csdn.net/qq_44732146/article/details/126498908,作者:胡八一,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。