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();
}
} }

java 从指定行读文件,执行系统命令的更多相关文章

  1. java中多种方式读文件

    转自:http://www.jb51.net/article/16396.htm java中多种方式读文件 一.多种方式读文件内容. 1.按字节读取文件内容 2.按字符读取文件内容 3.按行读取文件内 ...

  2. python (11)文件的读写 按行读文件

    读文件: 读取文件 f = open('\info.txt') fil = f.read() f.close() 按行读文件: f = open("info.txt") while ...

  3. (转)Java按指定行数读取文件

    package test import java.io.File; import java.io.FileReader; import java.io.IOException; import java ...

  4. Java显示指定类型的文件

    文件作为存储数据的单元,会根据数据类型产生很多分类,也就是所谓的文件类型.在对数据文件进行操作时,常常需要根据不同的文件类型来作不同的处理.本实例实现的是读取文件夹指定类型的文件并显示到表格控件中.这 ...

  5. Python之路 day2 按行读文件

    #1. 最基本的读文件方法: # File: readline-example-1.py file = open("sample.txt") while 1: line = fil ...

  6. Python按行读文件对比

    1. 最基本的读文件方法: # File: readline-example-1.py   file = open("sample.txt")   while 1:     lin ...

  7. java统计指定目录中文件的个数和总的大小

    转: 统计指定目录中文件的个数和总的大小 package file; import java.io.File; import java.util.ArrayList; public class Fil ...

  8. java使用指定的国际化文件

    java代码: import java.util.Locale; import org.junit.Test; /** * 使用指定的国际化文件 */ public class Demo { @Tes ...

  9. java监控指定路径下文件及文件夹变化

    之前用jdk7的WatchService API(java.nio.file包)来做目录下的子文件监控,后改为使用commons-io包.主要有下面几点不同:1. WatchService是采用扫描式 ...

随机推荐

  1. sqlserver操作geography方法

    参考:https://www.cnblogs.com/ytwy/p/5977848.html http://desktop.arcgis.com/zh-cn/arcmap/latest/manage- ...

  2. echarts饼图配置模板

    var option = { title:{ text:'完成人构成分析--申报', //标题的样式 textSytle:{ //颜色 color : '#FF0000', //粗细 // fontW ...

  3. Javascript异步编程之一异步原理

    本系列的例子主要针对node.js环境,但浏览器端的原理应该也是类似的. 本人也是Javascript新手,把自己这段时间学习积累的要点总结下来,希望可以对同样在学习Javascript/node.j ...

  4. 【Dojo 1.x】笔记2 使用服务器环境及使用模块

    又开坑了.上次静态html页面完成本地module的引用,算是成功了,但是并不知道是怎么运作的,没关系慢慢来. 我用的环境是VSCode,这次因为官方说要在服务器环境下运行,所以就用上了VSCode的 ...

  5. Cesium 之简介以及离线部署运行篇

    前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. C ...

  6. Mac上webstorm与git仓库建立连接

    1.打开Mac终端,输入$ cd ~/.ssh检查.ssh文件是否存在($在终端中存在,不需要自己输入),不存在,进行步骤2 2.如果没有安装ssh文件,输入命令$ssh -v,安装ssh文件,成功时 ...

  7. IDEA修改Git账户和密码

    找到c盘中git目录的.gitconfig文件可以直接修改name和邮箱   

  8. Xamarin 打包生成 Android apk 文件

    Visual Studio 支持 apk 发布 Xamarin.Forms项目或Xamarin.Android项目开发完成之后需要发布.比较常规的发布方式是生成 apk 文件,微软也考虑到开发者有发布 ...

  9. mysql 的远程链接字符

    默认情况下,mysql只允许本地登录,如果要开启远程连接,则需要修改/etc/mysql/my.conf文件. 一.修改/etc/mysql/my.conf找到bind-address = 127.0 ...

  10. Mysql数据中Packet for query is too large错误的解决方法

    有时,程序在连接mysql执行操作数据库时,会出现如下类似错误信息: Packet for query is too large (4230 > 1024). You can change th ...