有时候在进行开发的过程中,需要自己写个测试类来进行某个局部功能的测试,在测试的过程中,需要引入第三方jar包或者公司其他成员的帮助类,比如说:我需要测试一个
切记:最后一个jar包后的:后面要加上一个空格再引入自己的Java文件
注意:
net.sf.json.JSONObject解析数据的时候中文乱码问题
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import java.io.*;
import java.net.URLEncoder;
/**
* Created by xx on 2017/3/29.
*/
public class ZyhqErrorMsgDemo {
public static void main(String[] args) {
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader in = null;
try{
String filePath = "/home/1.txt";
File file = new File(filePath);
String code = codeString(filePath);
System.out.println("文件类型:" + code);
inputStream = new FileInputStream(file);
inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
in = new BufferedReader(inputStreamReader);
String firstLine = in.readLine();//过滤掉首行,以便循环体从第二行(申赎明细处理结果)
String line = "";
while ((line = in.readLine()) != null) {
System.out.println("line:=====>"+ line);
System.out.println();
System.out.println();
JSONObject jsonObject = JSONObject.fromObject(line);
String status = jsonObject.getString("status");
String objectStr = jsonObject.getString("content");
String errorMessage = null;
try{
errorMessage = jsonObject.get("errorMessage").toString();
}catch (JSONException ex) {
System.out.println("there is no errorMessage from Zyhq holding respon file");
}
System.out.println("errorMessage:------>" + errorMessage);
}
in.close();
inputStreamReader.close();
inputStream.close();
}catch (Exception ex){
System.out.println("下载文件并解析文件内容时出错"+ex.getStackTrace());
}finally {
if (in != null){
try {
in.close();
} catch (IOException e) {
}
}
if (inputStreamReader != null){
try {
inputStreamReader.close();
} catch (IOException e) {
}
}
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
}
}
}
}
/**
* 判断文件的编码格式
* @param fileName :file
* @return 文件编码格式
* @throws Exception
*/
private static String codeString(String fileName) throws Exception{
BufferedInputStream bin = new BufferedInputStream(
new FileInputStream(fileName));
int p = (bin.read() << 8) + bin.read();
String code = null;
switch (p) {
case 0xefbb:
code = "UTF-8";
break;
case 0xfffe:
code = "Unicode";
break;
case 0xfeff:
code = "UTF-16BE";
break;
default:
code = "GBK";
}
return code;
}
}
写好后,我将这个java文件(ZyhqErrorMsgDemo.java)上传到远端的centos服务器上,目录结构如下图所示:
编译的命令:
javac -cp /home/cjh/commons-beanutils-1.8.3.jar:/home/cjh/commons-collections-3.2.1.jar:/home/cjh/commons-lang-2.6.jar:/home/cjh/commons-logging-1.2.jar:/home/cjh/ezmorph-1.0.6.jar:/home/cjh/json-lib-2.4-jdk15.jar: /home/cjh/ZyhqErrorMsgDemo.java
切记:最后一个jar包后的:后面要加上一个空格再引入自己的Java文件
执行的命令:
java -cp /home/cjh/commons-beanutils-1.8.3.jar:/home/cjh/commons-collections-3.2.1.jar:/home/cjh/commons-lang-2.6.jar:/home/cjh/commons-logging-1.2.jar:/home/cjh/ezmorph-1.0.6.jar:/home/cjh/json-lib-2.4-jdk15.jar: ZyhqErrorMsgDemo
注意:
1、我写的测试类ZyhqErrorMsgDemo是没有自己的包的,当你写的类的在某个包下时,再执行命令的时候要记得把全路径带进去.
2、另外,所依赖的几个jar包,下载地址:
http://blog.youkuaiyun.com/gu_gu_/article/details/50551775
3、参考文献:
http://blog.youkuaiyun.com/m53931422/article/details/42174609
https://zhidao.baidu.com/question/1512086588648273940.html