1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| import org.json.JSONArray; import org.json.JSONObject; import java.io.*; import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] args) { String inputFilePath = "D:\\comment.txt"; //Twikoo文件输入路径 String outputFilePath = "D:\\out.txt"; //Sql文件输出路径 try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFilePath), StandardCharsets.UTF_8)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFilePath), StandardCharsets.UTF_8))) { StringBuilder jsonBuilder = new StringBuilder(); String line; boolean isArrayStarted = false; // 用来检查是否已经开始读取数组 // 读取文件内容,构建完整的JSON数组 while ((line = reader.readLine()) != null) { // 假设JSON数组可能跨越多行,我们在这里构建完整的数组 jsonBuilder.append(line); // 检查当前行是否是数组的结束(这取决于你的具体文件格式,可能是一个逗号,或者文件结束) // 这里我们简单地假设文件结束就是数组的结束 if (line.trim().endsWith("]")) { isArrayStarted = true; break; // 假设整个数组已经读取完毕 } // 如果数组跨越多行,并且不是最后一行,可能需要添加逗号或其他分隔符 // 这里我们假设没有额外的分隔符需要添加,因为JSONArray可以处理连续的JSON对象 } if (!isArrayStarted) { throw new IOException("JSON array not found or not properly formatted in the input file."); } // 现在jsonBuilder包含完整的JSON数组字符串 JSONArray jsonArray = new JSONArray(jsonBuilder.toString()); StringBuilder sqlStatements = new StringBuilder(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject commentObj = jsonArray.getJSONObject(i); String sqlStatement = generateSqlStatement(commentObj); sqlStatements.append(sqlStatement); if ((i + 1) % 10 == 0) { sqlStatements.append("\n"); } } writer.write(sqlStatements.toString()); System.out.println("SQL statements have been written to " + outputFilePath); } catch (IOException e) { e.printStackTrace(); } } private static String generateSqlStatement(JSONObject commentObj) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String createdAt = sdf.format(new Date(commentObj.getLong("created")));
return "INSERT INTO wp_comments (\n" + //wp_comments改为数据库表的名字 "comment_post_ID,\n" + "comment_author,\n" + "comment_author_email,\n" + "comment_author_url,\n" + "comment_author_IP,\n" + "comment_date,\n" + "comment_date_gmt,\n" + "comment_karma,\n" + "comment_content,\n" + "comment_approved,\n" + "comment_agent,\n" + "comment_type,\n" + "comment_parent,\n" + "user_id\n" + ") VALUES (\n" + "999,\n" + "'" + commentObj.getString("nick") + "',\n" + "'" + commentObj.getString("mail") + "',\n" + "'',\n" + "'" + commentObj.getString("ip") + "',\n" + "'" + createdAt + "',\n" + "'" + createdAt.substring(0, 10) + " " + createdAt.substring(11, 19) + "',\n" + "0,\n" + "'" + commentObj.getString("comment").replace("<p>", "").replace("</p>", "").trim() + "',\n" + "1,\n" + "'" + commentObj.getString("ua") + "',\n" + "'comment',\n" + "0, \n" + "0 \n" + ");\n"; } }
|