java 从指定行读文件,执行系统命令
2016-03-08 14:12
525 查看
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(); } } }
相关文章推荐
- RxJava学习教程
- java 通过反射获取调用类方法及属性
- Java设计模式——Observer(观察者)模式
- java 通过反射获取调用类方法及属性
- 【项目管理和构建】十分钟教程,eclipse配置maven + 创建maven项目(三)
- Spring事务配置的五种方式
- java 线程池学习
- 利用bouncycastle 生成pfx公私钥文件
- spring aop 异常记录
- java中的多态
- 使用Eclipse开发Java Web过程中Debug调试的使用方法
- eclipse怎么设置字体大小
- eclipse怎么设置字体大小
- springMVC 常用注解
- 在已经有xml文件的情况下快速生成javabean
- Spring MVC常用的注解
- spring加载配置文件
- 关于局部内部类访问所在方法的参数或局部变量为什么必须是final的
- java.lang.Exception: OALL8 处于不一致状态
- spring事务