package com.xmg.txt;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.xmg.constants.ExcelConstant;
import com.xmg.tool.FileUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* text转json数据最终版本
*
* @author xuminggang
*
*/
public class TextToJsonFinal {
private static List<String> filePathNameList = new ArrayList<String>();
public static void main(String[] args) {
// String cfgFile = "s1.txt";
// if (args.length > 0) {
// cfgFile = args[0];
// }
// new TextToJsonFinal().translateFileNew(cfgFile);
String filepath = ExcelConstant.GENERATE_TXT_LOCATION + "phantomdatas/";
FileUtil.getFileNameWithPath(filepath, filePathNameList);
TextToJsonFinal finaljson = new TextToJsonFinal();
for (String path : filePathNameList) {
System.out.println(path);
finaljson.translateFileNew("", path);
}
}
private void translateFileNew(String cfgFile, String path) {
Map<Long, JSONObject> jsonMap = new HashMap<Long, JSONObject>();
String filepath = "";
if (!cfgFile.isEmpty()) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL url = classLoader.getResource(cfgFile);
filepath = url.getFile();
} else {
filepath = path;
}
String outputName = "";
try {
FileInputStream fis = new FileInputStream(new File(filepath));
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String lineTxt = null;
JSONArray array = new JSONArray();
int linenum = 0;
while ((lineTxt = br.readLine()) != null) {// 数据以逗号分隔
String[] names = lineTxt.split("\t");
linenum++;
if (linenum == 1) {
outputName = names[0];
} else {
Long id = Long.valueOf(names[0]);
int count = Integer.valueOf(names[2]);
if (jsonMap.containsKey(id)) {
JSONObject obj = jsonMap.get(id);
obj.put("value", obj.getInt("value") + count);
jsonMap.put(id, obj);
} else {
JSONObject obj = new JSONObject();
obj.put("charId", id);
obj.put("charName", names[1]);
obj.put("value", count);
obj.put("resourceType", 0);
obj.put("hasAdded", false);
jsonMap.put(id, obj);
}
}
}
array = mapToJsonNewArray(jsonMap, array);
String sql = jsonToSql(array);
String outName = "result_" + outputName.substring(15, outputName.length() - 7);
System.out.println(outName);
writeToFile(sql, outputName.substring(15, outputName.length() - 7), ".sql");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 新map转jsonarray
* @param array
* @param jsonMap2
*/
private JSONArray mapToJsonNewArray(Map<Long, JSONObject> jsonMap, JSONArray array) {
for (JSONObject obj : jsonMap.values()) {
array.add(obj);
}
System.out.println(array.size());
return array;
}
/**
* jsonarray转sql语句
* @param array
*/
private String jsonToSql(JSONArray array) {
StringBuilder builder = new StringBuilder();
String sqlheader = "use wg_lj;";
String insertHeader = "update t_game_server_values set data='";
String jsonString = array.toString();
String insertTail = "' where id=21;";
builder.append(sqlheader).append(insertHeader).append(jsonString).append(insertTail);
String sql = builder.toString();
return sql;
}
public void writeToFile(String info, String name, String filePostfix) throws IOException {
String path = ExcelConstant.GENERATE_TXT_LOCATION + name + filePostfix;
BufferedWriter bw = null;
try {
FileOutputStream out = new FileOutputStream(path, true);// true,��ʾ:�ļ������ݣ�����������,Ĭ��Ϊfalse
bw = new BufferedWriter(new OutputStreamWriter(out, "utf-8"));
bw.write(info += "\r\n");// ����
bw.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
bw.close();
}
}
}
package com.xmg.tool;
import java.io.File;
import java.util.List;
public class FileUtil {
/**
* 获取文件夹下的文件路径和名字
* @param filePath
* @param files
* @return
*/
public static List<String> getFileNameWithPath(String filePath,List<String> files){
File file = new File(filePath);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
files.add(tempList[i].toString());
}
if (tempList[i].isDirectory()) {
getFileNameWithPath(tempList[i].getAbsolutePath(),files);
}
}
return files;
}
/**
* 获取所有文件的名字
* @param filePath
* @param files
* @return
*/
public static List<String> getFileNameListUnderPath(String filePath,List<String> files){
File file = new File(filePath);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
files.add(tempList[i].getName());
}
if (tempList[i].isDirectory()) {
getFileNameListUnderPath(tempList[i].getAbsolutePath(),files);
}
}
return files;
}
}