歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java 從指定行讀文件,執行系統命令

Java 從指定行讀文件,執行系統命令

日期:2017/3/1 9:18:19   编辑:Linux編程

Java 從指定行讀文件,執行系統命令

import java.util.*;
import java.io.*;

public class Example {
public static void main(String[] args){
readFile("proxy.txt",0);
readFile("proxy.txt",1);
readFile("proxy.txt",4);
execSystemCmd("notepad"); // windows cmd
execSystemCmd("ls /home/whucs");
execSystemCmd("tail -n /home/whucs/vote.py");
execSystemCmd("wc -l php-fpm.log"); // count lines of a file. 500MB ~ 2s
}

public static void readFile(String path, int beginLine) {
FileInputStream inputStream = null;
Scanner sc = null;
try {
inputStream = new FileInputStream(path);
sc = new Scanner(inputStream, "UTF-8");
int begin = 0;
if (beginLine == 0) beginLine = 1;
while (sc.hasNextLine()) {
begin ++;
if (begin >= beginLine) {
String line = sc.nextLine();
// TODO...
System.out.println(line);
} else {
sc.nextLine();
}
}
if (begin < beginLine) {
System.out.println("error! beginLine > file's total lines.");
}
inputStream.close();
sc.close();
} catch (IOException e) {
System.out.println("FileReader IOException!");
e.printStackTrace();
}
}

public static void execSystemCmd(String cmd) {
String outPut = null;
System.out.println("cmd=" + cmd);
try
{
Process p = Runtime.getRuntime().exec(cmd);
InputStream is = p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder buf = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) buf.append(line + "\n");
outPut = buf.toString();
System.out.printf("outPut = %s",outPut);
}
catch (IOException e)
{
e.printStackTrace();
}
}

}

Copyright © Linux教程網 All Rights Reserved