从后缀名为grib2的文件中查询相关的信息,并将查出来的信息保存起来。

主要是学习java中调用windows下的cmd平台,并进行执行相关的命令。

package com.wis.wgrib2;

import java.io.IOException;

/**
* @description 主函数
* @author wis
*
*/
public class WgriB2Main { public static void main(String[] args) throws IOException, InterruptedException {
new WgriB2CMD().executeCMDfile(new WgriB2FIle().fileList());
System.out.println("------------------------执行完成---------------------");
} }
package com.wis.wgrib2;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.List; /**
* @description 在控制台进行执行
* @author wis
*
*/
public class WgriB2CMD { /**
* @description 在windows控制台执行命令
* @param list
* 文件路径的集合
*/
public void executeCMDfile(List<String> list) {
// D:\test\wgrib2_windows\wgrib2.exe
// D:\test\wgrib2_windows\110010300.gr2 -lon 89.9 38.4 -s|find "TMP"
File fileS = new File(WgriB2Bean.getOutPath());
WgriB2FIle wf = new WgriB2FIle();
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
BufferedWriter wr = null; try {
for (String fileNamePath : list) {
System.out.println(fileNamePath);
System.out.println(WgriB2Bean.getExePath());
Process p = new ProcessBuilder(WgriB2Bean.getExePath(), fileNamePath, WgriB2Bean.getVar(),
WgriB2Bean.getNx(), WgriB2Bean.getNy(), "-s").start();
// 等待进程执行完毕,否则会造成死锁。没加这一条的时候,执行了600条左右数据就写不进去了
p.waitFor();
is = p.getInputStream();
isr = new InputStreamReader(is, "utf-8");
br = new BufferedReader(isr);
wr = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileS, true)));
String line;
while ((line = br.readLine()) != null) {
String lineSuc = "";
if (line.indexOf(WgriB2Bean.getBianZhi()) != -1) {
// 89.905338,38.399768,-4.24,2011010103
// 1:0 : lon=89.905338,lat=38.399768,val=-3.42 : d=2011010104 : TMP:2 m above
// ground:anl:
String[] lineMao = line.split(":", 5);
lineSuc = wf.ziDuanSplit(lineMao);
System.out
.println(new SimpleDateFormat("yyyy-mm-dd hh:mm:ss").format(System.currentTimeMillis())
+ " " + "正在处理的文件为: " + new File(fileNamePath).getName());
wr.write(lineSuc + ","); } else if (line.indexOf(WgriB2Bean.getBianZhi2()) != -1) {
// 8:11000501:lon=89.905338,lat=38.399768,val=0:d=2011010104:APCP:surface:anl:
String val = wf.jiangShuiVal(line);
wr.write(lineSuc + val);
wr.newLine();
} else if (line.indexOf(WgriB2Bean.getBianZhi2()) == -1
&& Integer.parseInt(line.substring(0, 1)) == 8) {
wr.newLine();
}
}
wr.flush();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
try {
if (wr != null) {
wr.close();
wr = null;
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (br != null) {
br.close();
br = null;
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (isr != null) {
isr.close();
isr = null;
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (is != null) {
is.close();
is = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.wis.wgrib2;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List; /**
* @description 对文件或者是字段的操作方法
* @author wis
*
*/
public class WgriB2FIle { /**
* @description 获取文件列表
* @return 文件路径的集合
*/
public List<String> fileList() {
System.out.println(WgriB2Bean.getFilePath());
File file = new File(WgriB2Bean.getFilePath());
List<String> list = new LinkedList<String>();
if (file.isDirectory()) { System.out.println(new SimpleDateFormat("yyyy-mm-dd hh:mm:ss").format(System.currentTimeMillis()) + " "
+ "文件夹路径为: " + file.getPath());
File[] files = file.listFiles();
for (File file2 : files) {
if (file2.getName().indexOf("gr2") != -1) {
System.out.println(new SimpleDateFormat("yyyy-mm-dd hh:mm:ss").format(System.currentTimeMillis())
+ " " + "即将处理的文件为: " + file2.getName());
if (file2.isFile()) {
String fileName = file2.getAbsolutePath();
list.add(fileName);
}
} else {
System.out.println(new SimpleDateFormat("yyyy-mm-dd hh:mm:ss").format(System.currentTimeMillis())
+ " " + "此文件不能进行处理:" + file2.getName());
}
}
}
return list;
} /**
* @description 将用冒号“:”切割出来的字段,继续再进行用逗号“,”切割,然后再组合起来(分隔符为逗号,)。原始字段
* 1:0:lon=89.798607,lat=38.397492,val=266.208:d=2011010103:TMP:2 m
* above ground:anl:
* @param lineMao
* 完整字段1:0:lon=89.905338,lat=38.399768,val=269.73:d=2011010104:TMP:2
* m above ground:anl:中 的[1 0
* lon=89.905338,lat=38.399768,val=269.73:d=2011010104 TMP:2 m above
* ground:anl:]这个值,很神奇的字符串集合中本身应该使用逗号隔开的,字段解析出来本身也有逗号,尽然没有识别出错。
* @return 经度、纬度、开尔文和时间戳的值的集合,中间是使用逗号隔开
*/
public String ziDuanSplit(String[] lineMao) {
int num = 0;
String lineSuc = "";
for (int i = 2; i < (lineMao.length - 1); i++) {
// lon=89.905338,lat=38.399768,val=-3.42
String[] lineDengS = lineMao[i].split(",", -1);
for (int y = 0; y < lineDengS.length; y++) {
String lineDengSO = lineDengS[y].substring(lineDengS[y].indexOf("=") + 1);
if (y == 2) {
// 取出开尔文,并转化成摄氏温度
lineDengSO = String.format("%.2f", (Double.parseDouble(lineDengSO) - 273.15));
}
num++;
if (num < 5) {
lineSuc = lineSuc.trim() + lineDengSO.trim();
if (num < 4) {
lineSuc = lineSuc.trim() + ",";
}
}
}
}
return lineSuc;
} /**
* @description 变量值后面字段的提取(包含变量名的提取)
* @param lineMao
* 经度、纬度、开尔文和时间戳的集合
* @return 经度、纬度、开尔文和时间戳的值的集合
*/
public String ziDuanHou(String[] lineMao) {
String bian2 = "";
// APCP:surface:anl:
for (int i = 0; i < (lineMao.length - 1); i++) {
bian2 = bian2 + lineMao[i];
if (i < (lineMao.length - 2)) {
bian2 = bian2 + ",";
}
}
return bian2;
} /**
* @description 降水值的提取
* @param line
* 完整的字段
* @return 降水的值
*/
public String jiangShuiVal(String line) {
return line.substring(line.indexOf("val=") + 4, line.indexOf(":d="));
} }
package com.wis.wgrib2;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Properties; /**
* @description bean文件
* @author wis
*
*/
public class WgriB2Bean { private static Properties prop = null;
private static String filePath = null;
private static String var = null;
private static String varX = null;
private static String nx = null;
private static String ny = null;
private static String npts = null;
private static String bianZhi = null;
private static String exePath = null;
private static String outPath = null;
private static String bianZhi2 = null;
/**
* @description 加载配置文件
*/
static {
InputStream in = null;
try {
// 包内读取
in = WgriB2FIle.class.getClassLoader().getResourceAsStream("wgrib2.properties");
// jar包外读取
// in = new FileInputStream("wgrib2.properties"); prop = new Properties();
prop.load(in);
filePath = prop.getProperty("FILE_PATH");
var = prop.getProperty("VAR");
varX = prop.getProperty("VARX");
nx = prop.getProperty("NX");
ny = prop.getProperty("NY");
npts = prop.getProperty("NPTS");
bianZhi = prop.getProperty("BIANZHI");
exePath = prop.getProperty("EXT_PATH");
outPath = prop.getProperty("OUT_PATH");
bianZhi2 = prop.getProperty("BIANZHI_2"); // System.out.println("=============================" + nx);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null)
in.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(new SimpleDateFormat("yyyy-mm-dd hh:mm:ss").format(System.currentTimeMillis()) + " "
+ "属性文件加载完成,开始数据处理");
}
} public static Properties getProp() {
return prop;
} public static void setProp(Properties prop) {
WgriB2Bean.prop = prop;
} public static String getFilePath() {
return filePath;
} public static void setFilePath(String filePath) {
WgriB2Bean.filePath = filePath;
} public static String getVar() {
return var;
} public static void setVar(String var) {
WgriB2Bean.var = var;
} public static String getVarX() {
return varX;
} public static void setVarX(String varX) {
WgriB2Bean.varX = varX;
} public static String getNx() {
return nx;
} public static void setNx(String nx) {
WgriB2Bean.nx = nx;
} public static String getNy() {
return ny;
} public static void setNy(String ny) {
WgriB2Bean.ny = ny;
} public static String getNpts() {
return npts;
} public static void setNpts(String npts) {
WgriB2Bean.npts = npts;
} public static String getBianZhi() {
return bianZhi;
} public static void setBianZhi(String bianZhi) {
WgriB2Bean.bianZhi = bianZhi;
} public static String getExePath() {
return exePath;
} public static void setExePath(String exePath) {
WgriB2Bean.exePath = exePath;
} public static String getOutPath() {
return outPath;
} public static void setOutPath(String outPath) {
WgriB2Bean.outPath = outPath;
} public static String getBianZhi2() {
return bianZhi2;
} public static void setBianZhi2(String bianZhi2) {
WgriB2Bean.bianZhi2 = bianZhi2;
} }
FILE_PATH=D:/wgrib2_test/test/2011
EXT_PATH=d:/LAPS1KM20112013/run/jar2011q/exe/wgrib2.exe
VAR=-lon
VARX=
#文件格式支持txt和csv的
OUT_PATH=D:/test/2013hbu.csv
NPTS=
BIANZHI=TMP
BIANZHI_2=APCP
#xx县101.002080000,37.0347970000
NX=101.002080000
NY=37.0347970000

调用windows系统下的cmd命令窗口处理文件的更多相关文章

  1. windows系统下在dos命令行kill掉被占用的pid (转)

    原文出自:http://www.2cto.com/os/201304/203771.html   windows系统下在dos命令行kill掉被占用的pid   1.开始-->运行-->c ...

  2. c语言基础学习02_windows系统下的cmd命令

    =============================================================================注意:cmd的命令很多,需要用的时候可以查询即 ...

  3. Windows 系统中的 CMD 黑窗口简单介绍

    简介 DOS是磁盘操作系统的缩写,是个人计算机上的一类操作系统DOS命令,是DOS操作系统的命令,是一种面向磁盘的操作命令,主要包括目录操作类命令.磁盘操作类命令.文件操作类命令和其它命令.DOS系统 ...

  4. WIndows 系统下的常用命令 和 检测方法

    ### 一.检测硬盘速度(Windows 自带工具) #### 使用windows 系统自带的工具测试硬盘读写速度 > 在使用下面命令前,需要获得管理员权限,才会在Dos窗口上显示(否则,一闪而 ...

  5. 三种方法在当前目录下打开cmd命令窗口

    概述 运行npm的时候,每次都要cd到目录,很麻烦,所以总结了三种在当前目录下直接打开cmd窗口的方法,供以后开发时参考,相信对其他人也有用. 方法一 在当前目录按住shift再右键. 会看到右键菜单 ...

  6. Windows系统下在Git Bash中把文件内容复制到剪贴板的命令

    众所周知,在OS系统中,复制文件内容到剪贴板(比如复制公钥到剪贴板)的命令是: pbcopy < ~/.ssh/id_rsa.pub 在Win7或者Win10下这条命令就没用了.可以这样: cl ...

  7. Windows 系统下安装 dig 命令

    dig是一个Linux下用来DNS查询信息的小工具,dig全称是Domain Information Groper,与nslookup类似,但比nslookup功能更强大.Windows只有nsloo ...

  8. Windows系统下安装dig命令

    dig 是一个 Linux 下用来 DNS 查询信息的工具,全称是Domain Information Groper,与 nslookup 类似,但比 nslookup 功能更强大.Windows 下 ...

  9. windows系统下使用cd命令

    如果要切换到D:\Program Files目录下,大多数人会想当然的在命令行窗口输入 cd D:\Program Files回车. 如下所示: 发现并没有切换到D:\Program Files. 正 ...

随机推荐

  1. 为什么关不掉所有的OSD

    前言 碰到一个cepher问了一个问题: 为什么我的OSD关闭到最后有92个OSD无法关闭,总共的OSD有300个左右 想起来在很久以前帮人处理过一次问题,当时环境是遇上了一个BUG,需要升级到新版本 ...

  2. ESP8266 鼓捣记 - 从零制作一个温湿度计

    一.前言 经过上一篇文章 <ESP8266 鼓捣记 - 入门(环境搭建) >搭建好环境后,肯定不会满足于 Hello World ,想快速做一个实际有用的东西出来,我认为温湿度计就非常合适 ...

  3. (7)ASP.NET Core3.1 Ocelot Swagger

    1.前言 前端与后端的联系更多是通过API接口对接,API文档变成了前后端开发人员联系的纽带,开始变得越来越重要,而Swagger就是一款让你更好的书写规范API文档的框架.在Ocelot Swagg ...

  4. CSS浮动和清除浮动

    1.浮动float div配合float浮动来做页面的布局,浮动最常用的地方就是用来做布局.只有一个标签设置了浮动float属性,就会变成块级标签. <!DOCTYPE html> < ...

  5. Java web项目 上传图片保存到数据库,并且查看图片,(从eclipse上移动到tomact服务器上,之路径更改,包括显示图片和导出excel)

    //项目做完之后,在本机电脑运行完全正常,上传图片,显示图片,导出excel,读取excel等功能,没有任何问题,但是,当打成war包放到服务器上时,这些功能全部不能正常使用. 最大的原因就是,本机测 ...

  6. DNS域传输漏洞复现

    漏洞原理 DNS分类 常见的DNS记录类型 A IP地址记录,记录一个域名对应的IP地址 AAAA IPv6 地址记录,记录一个域名对应的IPv6地址 CNAME 别名记录,记录一个主机的别名 MX ...

  7. [原题复现]百度杯CTF比赛 十月场 WEB EXEC(PHP弱类型)

    简介  原题复现:  考察知识点:PHP弱类型.  线上平台:https://www.ichunqiu.com/battalion(i春秋 CTF平台) 过程 看源码发现这个 vim泄露  下方都试了 ...

  8. kali 系列学习07-攻击之密码生成

    比较理想的字典是拖库字典,比如CSDN字典,如果要生成字典,可以使用Crunch 和 rtgen 两个工具, 一.密码生成 1.Crunch (1)启动crunch命令.执行命令如下所示. #crun ...

  9. 如何使用Camtasia给视频或者图片调色

    喜欢摄影过着做视频的朋友一定知道,一张好看的照片或者一段精美视频的构成因素很多,取景本身肯定是个很重要的条件,相机的素质是非常重要的硬件条件,接下来的就是后期的编辑和处理了,而在后期处理过程中调色就显 ...

  10. mac中怎么完成移动硬盘分区这个操作

    移动硬盘在出厂时只有一个区,不方便我们存储和查阅文件,移动硬盘分区可以防止硬盘发生错误,以免造成资料丢失,也可以防止产生无用文件. 移动硬盘基本上都是用Windows系统进行分区的,但是现在很多人使用 ...