java txt转json,转sql

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值