1. 程式人生 > >JDBC一鍵生成JavaBean Dao Dao實現類

JDBC一鍵生成JavaBean Dao Dao實現類

package com.util;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Date;
import java.util.Scanner;

/**
 * Created by Gaojie on 2018/10/2 11:37.
 * 通過資料庫表自動生成JavaBean Dao.
 * 暫無發現BUG 學生一枚 才疏學淺 如若複製程式碼需要改寫的位置已標紅;
 * 棄用功能:百度翻譯
 */
public class Automatically {
    private static String packageOutPath = "E:\\IDEA工程\\1701\\1001_自動生成Dao\\src";//指定實體生成所在包的路徑
private static String authorName = "GaoJie";//作者名字 private static String tablename = "shop-depot-commodity-supplier-goodType";//表名 用-隔開 private static String[] colnames; // 列名陣列 private static String[] colTypes; //列名型別陣列 public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] split = tablename.split("-");//分割表名陣列 for (int i = 0; i < split.length; i++) {//遍歷 tablename = split[i]; //生成JavaBean generateJavaBean(); //生成Dao 介面 generateDao(); //生成Dao 實現 generateDaoImpl(); } } /** * 功能:生成JavaBean */ private static void generateJavaBean() { //建立連線 Connection con = null; PreparedStatement ps = null; //查要生成JavaBean的表 String sql = "select * from " + tablename;//SQL語句 try { con = Dbmanger.getConn();//獲取連線
這裡的Dbmanger是我的工具類 getConn()用於獲取con 若使用請改寫為自己的工具類 ps = con.prepareStatement(sql); ResultSetMetaData rsmd = ps.getMetaData(); int size = rsmd.getColumnCount(); //返回列數 colnames = new String[size]; colTypes = new String[size]; for (int i = 0; i < size; i++) {//得到列名和列型別 colnames[i] = rsmd.getColumnName(i + 1);//列名 colTypes[i] = rsmd.getColumnTypeName(i + 1);//列資料庫型別名 } String content = parse(colnames, colTypes);//得到JavaBean程式碼 File directory = new File(packageOutPath + "\\com\\pojo"); directory.mkdirs(); String outputPath = packageOutPath + "\\com\\pojo\\" + initcap(tablename) + ".java"; PrintWriter pw = new PrintWriter(new FileWriter(outputPath)); pw.println(content); pw.flush(); pw.close(); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /** * 功能:生成JavaBean程式碼 * * @param colnames * @param colTypes * @return */ private static String parse(String[] colnames, String[] colTypes) throws Exception { StringBuffer sb = new StringBuffer(); sb.append("package " + "com.pojo" + ";\r\n"); sb.append("\r\n"); //註釋部分 sb.append("/**\r\n"); //String translateResult = getTranslateResult(tablename); sb.append("* " + tablename + " " + " 實體類\r\n"); sb.append("* " + new Date() + " " + authorName + "\r\n"); sb.append("*/ \r\n"); //實體部分 sb.append("\r\n\r\npublic class " + initcap(tablename) + " {\r\n"); processAllAttrs(sb);//屬性 processAllMethod(sb);//get set方法 sb.append("}\r\n"); return sb.toString(); } /** * 功能:生成屬性 * * @param sb */ private static void processAllAttrs(StringBuffer sb) { for (int i = 0; i < colnames.length; i++) { try { //加上翻譯 //String translateResult = getTranslateResult(colnames[i]); sb.append("\tprivate " + sqlType2JavaType(colTypes[i]) + " " + colnames[i] + ";" + "\r\n"); } catch (Exception e) { e.printStackTrace(); } } sb.append("\r\n"); } /** * 功能:生成set get方法 * * @param sb */ private static void processAllMethod(StringBuffer sb) { //無參 有參 sb.append("\tpublic " + initcap(tablename) + "(){\r\n"); sb.append("\t}\r\n\r\n"); StringBuffer sb2 = new StringBuffer(); for (int i = 0; i < colnames.length; i++) { sb2.append(sqlType2JavaType(colTypes[i]) + " " + colnames[i] + ","); } String s = sb2.toString(); s = s.substring(0, s.length() - 1); sb.append("\tpublic " + initcap(tablename) + "(" + s + "){\r\n"); StringBuffer sb3 = new StringBuffer(); for (int i = 0; i < colnames.length; i++) { sb3.append("\t\tthis." + colnames[i] + " = " + colnames[i] + ";\r\n"); } String s2 = sb3.toString(); sb.append(s2); sb.append("\t}\r\n"); //toString sb.append("\tpublic " + "String toString(){\r\n\t\treturn "); StringBuffer sb4 = new StringBuffer(); for (int i = 0; i < colnames.length; i++) { sb4.append("\"," + colnames[i] + "=\" + " + colnames[i] + " + \r\n\t\t"); } String s3 = sb4.toString(); s3 = s3.substring(0, 1) + s3.substring(2, s3.length() - 7) + ";"; sb.append(s3); sb.append("\r\n\t}\r\n"); //set get for (int i = 0; i < colnames.length; i++) { sb.append("\tpublic void set" + initcap(colnames[i]) + "(" + sqlType2JavaType(colTypes[i]) + " " + colnames[i] + "){\r\n"); sb.append("\t\tthis." + colnames[i] + "=" + colnames[i] + ";\r\n"); sb.append("\t}\r\n\r\n"); sb.append("\tpublic " + sqlType2JavaType(colTypes[i]) + " get" + initcap(colnames[i]) + "(){\r\n"); sb.append("\t\treturn " + colnames[i] + ";\r\n"); sb.append("\t}\r\n\r\n"); } } /** * Dao * 功能:生成Dao介面 建立檔案 */ public static void generateDao() { File directory = new File(packageOutPath + "\\com\\dao"); directory.mkdirs();//建立目錄 try { String content = parseDao();//程式碼 String outputPath = packageOutPath + "\\com\\dao\\" + initcap(tablename) + "Dao.java"; FileWriter fw = new FileWriter(outputPath); PrintWriter pw = new PrintWriter(fw); pw.println(content); pw.flush(); pw.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 功能:生成Dao介面主體程式碼 * * @param * @return * @throws Exception */ private static String parseDao() { StringBuffer sb = new StringBuffer(); sb.append("package com.dao;\r\n"); sb.append("\r\n"); sb.append("import com.pojo." + initcap(tablename) + ";"); sb.append("\r\n"); sb.append("import java.util.List;"); sb.append("\r\n"); //註釋部分 sb.append(" /**\r\n"); sb.append(" * " + tablename + " Dao介面類\r\n"); sb.append(" * " + new Date() + " " + authorName + "\r\n"); sb.append(" */ \r\n"); //實體部分 sb.append("\r\n\r\npublic interface " + initcap(tablename) + "Dao{\r\n"); processDaoAllMethod(sb);//生成Dao所有方法 sb.append("}\r\n"); //System.out.println(sb.toString()); return sb.toString(); } /** * 功能:生成Dao介面的方法 * * @param sb */ private static void processDaoAllMethod(StringBuffer sb) { String[] str = {"增加", "刪除", "修改", "查詢", "根據id進行查詢"}; String[] str2 = {"insert", "delete", "update", "selectAll", "selectAllById"}; for (int i = 0; i < str.length; i++) { sb.append("\r\n\t"); sb.append("// " + str[i]); sb.append("\r\n"); if (str[i].equals("查詢")) { sb.append("\tList " + str2[i] + "();"); } else if (str[i].equals("根據id進行查詢")) { sb.append("\tList " + str2[i] + "(" + initcap(tablename) + " " + tablename + ");"); } else { sb.append("\tvoid " + str2[i] + "(" + initcap(tablename) + " " + tablename + ");"); } sb.append("\t\r\n"); } } /** * 功能:生成DaoService介面實現 */ public static void generateDaoImpl() { File directory = new File(packageOutPath + "\\com\\dao\\impl"); directory.mkdirs();//建立目錄 try { String content = parseDaoImpl(directory);//程式碼 String outputPath = packageOutPath + "\\com\\dao\\impl\\" + initcap(tablename) + initcap("Impl") + ".java"; FileWriter fw = new FileWriter(outputPath); PrintWriter pw = new PrintWriter(fw); pw.println(content); pw.flush(); pw.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 功能:生成daoimpl主體程式碼 * * @param directory * @return */ private static String parseDaoImpl(File directory) { StringBuffer sb = new StringBuffer(); sb.append("package com.dao.impl;\r\n"); sb.append("\r\n"); sb.append("import java.util.List;"); sb.append("\r\n"); sb.append("import java.util.ArrayList;"); sb.append("\r\n"); sb.append("import com.util.Dbmanger;"); sb.append("\r\n"); sb.append("import java.sql.*;"); sb.append("\r\n"); sb.append("import com.dao." + initcap(tablename) + "Dao;"); sb.append("\r\n"); sb.append("import com.pojo." + initcap(tablename) + ";"); // sb.append("\r\n"); // sb.append("import java.util.Scanner;"); sb.append("\r\n"); sb.append("\r\n"); //註釋部分 sb.append(" /**\r\n"); sb.append(" * " + tablename + " 介面實現類\r\n"); sb.append(" * " + new Date() + " " + authorName + "\r\n"); sb.append(" */ \r\n"); //實體部分 sb.append("\r\n\r\npublic class " + initcap(tablename) + "Impl" + " implements " + initcap(tablename) + "Dao{\r\n"); processDaoImplAllMethod(sb);//生成Dao所有方法 sb.append("}\r\n"); //System.out.println(sb.toString()); return sb.toString(); } /** * 功能:生成Dao實現類方法 * * @param sb * @param */ private static void processDaoImplAllMethod(StringBuffer sb) { String[] str = {"增加", "刪除", "修改", "查詢", "根據id進行查詢"}; String[] str2 = {"insert", "delete", "update", "selectAll", "selectAllById"}; for (int i = 0; i < str.length; i++) { sb.append("\r\n\t"); sb.append("// " + str[i]); sb.append("\r\n"); if (str[i].equals("增加")) { sb.append("\tpublic void " + str2[i] + "(" + initcap(tablename) + " " + tablename + ") {\r\n"); DaoImplInsert(sb); sb.append("\t}\r\n"); } else if (str[i].equals("刪除")) { sb.append("\tpublic void " + str2[i] + "(" + initcap(tablename) + " " + tablename + ") {\r\n"); DaoImplDelete(sb); sb.append("\t}\r\n"); } else if (str[i].equals("修改")) { sb.append("\tpublic void " + str2[i] + "(" + initcap(tablename) + " " + tablename + ") {\r\n"); DaoImplUpdate(sb); sb.append("\t}\r\n"); } else if (str[i].equals("查詢")) { sb.append("\tpublic List " + str2[i] + "() {\r\n"); DaoImplSelectAll(sb); sb.append("\t}\r\n"); } else if (str[i].equals("根據id進行查詢")) { sb.append("\tpublic List " + str2[i] + "(" + initcap(tablename) + " " + tablename + ") {\r\n"); DaoImplSelectAllById(sb); sb.append("\t}\r\n"); } sb.append("\t\r\n"); } } /** * 增加 * * @param sb */ private static void DaoImplInsert(StringBuffer sb) { sb.append("\t\tConnection con = Dbmanger.getConn();\r\n");//獲取連線
如若使用請改寫為自己獲取con的工具類 sb.append("\t\tString sql = \"INSERT INTO " + tablename + " ("); StringBuffer temporary = new StringBuffer(); for (int i = 0; i < colnames.length; i++) { temporary.append(colnames[i] + ","); } sb.append(temporary.substring(0, temporary.length() - 1)); sb.append(")VALUES ("); StringBuffer temporary2 = new StringBuffer(); for (int i = 0; i < colnames.length; i++) { temporary2.append("?,"); } sb.append(temporary2.substring(0, temporary2.length() - 1) + ")\";"); sb.append("\r\n\t\t"); sb.append("PreparedStatement ps = null;"); sb.append("\r\n\t\t"); sb.append("try {"); sb.append("\r\n\t\t\t"); sb.append("ps = con.prepareStatement(sql);"); for (int i = 0; i < colnames.length; i++) { sb.append("\r\n\t\t\t"); sb.append("ps.set" + initcap(sqlType2JavaType(colTypes[i])) + "(" + (i + 1) + ", " + tablename + ".get" + initcap(colnames[i]) + "());"); } sb.append("\r\n\t\t\t"); sb.append("ps.executeUpdate();"); sb.append("\r\n\t\t"); sb.append("} catch (SQLException e) {"); sb.append("\r\n\t\t\t"); sb.append("e.printStackTrace();"); sb.append("\r\n\t\t"); sb.append("} finally {"); sb.append("\r\n\t\t\t"); sb.append("try {"); sb.append("\r\n\t\t\t\t"); sb.append("ps.close();"); sb.append("\r\n\t\t\t\t"); sb.append("con.close();"); sb.append("\r\n\t\t\t"); sb.append("} catch (SQLException e) {"); sb.append("\r\n\t\t\t\t"); sb.append("e.printStackTrace();"); sb.append("\r\n\t\t\t"); sb.append("}\r\n\t\t}\r\n"); } /** * 刪除 * * @param sb */ private static void DaoImplDelete(StringBuffer sb) { sb.append("\t\tConnection con = Dbmanger.getConn();\r\n");//獲取連線 如若使用請改寫為自己獲取con的工具類 sb.append("\t\tString sql = \"DELETE FROM " + tablename + " WHERE " + colnames[0] + " = ?\";"); sb.append("\r\n\t\t"); sb.append("PreparedStatement ps = null;"); sb.append("\r\n\t\t"); sb.append("try {"); sb.append("\r\n\t\t\t"); sb.append("ps = con.prepareStatement(sql);"); sb.append("\r\n\t\t\t"); sb.append("ps.set" + initcap(sqlType2JavaType(colTypes[0])) + "(" + (0 + 1) + ", " + tablename + ".get" + initcap(colnames[0]) + "());"); sb.append("\r\n\t\t\t"); sb.append("ps.executeUpdate();"); sb.append("\r\n\t\t"); sb.append("} catch (SQLException e) {"); sb.append("\r\n\t\t\t"); sb.append("e.printStackTrace();"); sb.append("\r\n\t\t"); sb.append("} finally {"); sb.append("\r\n\t\t\t"); sb.append("try {"); sb.append("\r\n\t\t\t\t"); sb.append("ps.close();"); sb.append("\r\n\t\t\t\t"); sb.append("con.close();"); sb.append("\r\n\t\t\t"); sb.append("} catch (SQLException e) {"); sb.append("\r\n\t\t\t\t"); sb.append("e.printStackTrace();"); sb.append("\r\n\t\t\t"); sb.append("}\r\n\t\t}\r\n"); } /** * 修改 * * @param sb */ private static void DaoImplUpdate(StringBuffer sb) { sb.append("\t\tConnection con = Dbmanger.getConn();\r\n");//獲取連線 如若使用請改寫為自己獲取con的工具類 sb.append("\t\tString sql = \"UPDATE "+tablename+" SET "); StringBuffer temporary = new StringBuffer(); for (int i = 0; i < colnames.length; i++) { temporary.append(colnames[i]+" = ?,"); } sb.append(temporary.substring(0,temporary.length()-1)+" WHERE "+colnames[0]+" = ?\";"); sb.append("\r\n\t\t"); sb.append("PreparedStatement ps = null;"); sb.append("\r\n\t\t"); sb.append("try {"); sb.append("\r\n\t\t\t"); sb.append("ps = con.prepareStatement(sql);"); for (int i = 0; i < colnames.length; i++) { sb.append("\r\n\t\t\t"); sb.append("ps.set" + initcap(sqlType2JavaType(colTypes[i])) + "(" + (i + 1) + ", " + tablename + ".get" + initcap(colnames[i]) + "());"); } sb.append("\r\n\t\t\t"); sb.append("ps.set" + initcap(sqlType2JavaType(colTypes[0])) + "(" + (colnames.length + 1) + ", " + tablename + ".get" + initcap(colnames[0]) + "());"); sb.append("\r\n\t\t\t"); sb.append("ps.executeUpdate();"); sb.append("\r\n\t\t"); sb.append("} catch (SQLException e) {"); sb.append("\r\n\t\t\t"); sb.append("e.printStackTrace();"); sb.append("\r\n\t\t"); sb.append("} finally {"); sb.append("\r\n\t\t\t"); sb.append("try {"); sb.append("\r\n\t\t\t\t"); sb.append("ps.close();"); sb.append("\r\n\t\t\t\t"); sb.append("con.close();"); sb.append("\r\n\t\t\t"); sb.append("} catch (SQLException e) {"); sb.append("\r\n\t\t\t\t"); sb.append("e.printStackTrace();"); sb.append("\r\n\t\t\t"); sb.append("}\r\n\t\t}\r\n"); } /** * 查詢所有 * * @param sb */ private static void DaoImplSelectAll(StringBuffer sb) { sb.append("\t\tList<"+initcap(tablename)+"> list = new ArrayList<"+initcap(tablename)+">();\r\n");//獲取連線 sb.append("\t\tConnection con = Dbmanger.getConn();\r\n");//獲取連線 如若使用請改寫為自己獲取con的工具類 sb.append("\t\tString sql = \"SELECT "); StringBuffer temporary = new StringBuffer(); for (int i = 0; i < colnames.length; i++) { temporary.append(colnames[i] + ","); } sb.append(temporary.substring(0, temporary.length() - 1)); sb.append(" FROM "+tablename+"\";"); StringBuffer temporary2 = new StringBuffer(); sb.append("\r\n\t\t"); sb.append("PreparedStatement ps = null;"); sb.append("\r\n\t\t"); sb.append("ResultSet rs = null;"); sb.append("\r\n\t\t"); sb.append("try {"); sb.append("\r\n\t\t\t"); sb.append("ps = con.prepareStatement(sql);"); sb.append("\r\n\t\t\t"); sb.append("rs = ps.executeQuery();"); sb.append("\r\n\t\t\t"); sb.append("while (rs.next()) {"); sb.append("\r\n\t\t\t\t"); sb.append(initcap(tablename)+" "+tablename+" = new "+initcap(tablename)+"();"); for (int i = 0; i < colnames.length; i++) { sb.append("\r\n\t\t\t\t"); sb.append(tablename+".set"+initcap(colnames[i])+"(rs.get"+initcap(sqlType2JavaType(colTypes[i]))+"(\""+colnames[i]+"\"));"); } sb.append("\r\n\t\t\t\t"); sb.append("list.add("+tablename+");"); sb.append("\r\n\t\t\t"); sb.append("}"); sb.append("\r\n\t\t"); sb.append("} catch (SQLException e) {"); sb.append("\r\n\t\t\t"); sb.append("e.printStackTrace();"); sb.append("\r\n\t\t"); sb.append("} finally {"); sb.append("\r\n\t\t\t"); sb.append("try {"); sb.append("\r\n\t\t\t\t"); sb.append("rs.close();"); sb.append("\r\n\t\t\t\t"); sb.append("ps.close();"); sb.append("\r\n\t\t\t\t"); sb.append("con.close();"); sb.append("\r\n\t\t\t"); sb.append("} catch (SQLException e) {"); sb.append("\r\n\t\t\t\t"); sb.append("e.printStackTrace();"); sb.append("\r\n\t\t\t"); sb.append("}"); sb.append("\r\n\t\t"); sb.append("}"); sb.append("\r\n\t\t"); sb.append("return list;\r\n"); } /** * 根據id查詢 * * @param sb */ private static void DaoImplSelectAllById(StringBuffer sb) { sb.append("\t\tList<"+initcap(tablename)+"> list = new ArrayList<"+initcap(tablename)+">();\r\n");//獲取連線 sb.append("\t\tConnection con = Dbmanger.getConn();\r\n");//獲取連線 如若使用請改寫為自己獲取con的工具類 sb.append("\t\tString sql = \"SELECT "); StringBuffer temporary = new StringBuffer(); for (int i = 0; i < colnames.length; i++) { temporary.append(colnames[i] + ","); } sb.append(temporary.substring(0, temporary.length() - 1)); sb.append(" FROM "+tablename+" WHERE "+colnames[0]+" = ?\";"); StringBuffer temporary2 = new StringBuffer(); sb.append("\r\n\t\t"); sb.append("PreparedStatement ps = null;"); sb.append("\r\n\t\t"); sb.append("ResultSet rs = null;"); sb.append("\r\n\t\t"); sb.append("try {"); sb.append("\r\n\t\t\t"); sb.append("ps = con.prepareStatement(sql);"); sb.append("\r\n\t\t\t"); sb.append("ps.set" + initcap(sqlType2JavaType(colTypes[0])) + "(" + (0 + 1) + ", " + tablename + ".get" + initcap(colnames[0]) + "());"); sb.append("\r\n\t\t\t"); sb.append("rs = ps.executeQuery();"); sb.append("\r\n\t\t\t"); sb.append("while (rs.next()) {"); sb.append("\r\n\t\t\t\t"); sb.append(initcap(tablename)+" "+tablename+"2= new "+initcap(tablename)+"();"); for (int i = 0; i < colnames.length; i++) { sb.append("\r\n\t\t\t\t"); sb.append(tablename+"2.set"+initcap(colnames[i])+"(rs.get"+initcap(sqlType2JavaType(colTypes[i]))+"(\""+colnames[i]+"\"));"); } sb.append("\r\n\t\t\t\t"); sb.append("list.add("+tablename+"2);"); sb.append("\r\n\t\t\t"); sb.append("}"); sb.append("\r\n\t\t"); sb.append("} catch (SQLException e) {"); sb.append("\r\n\t\t\t"); sb.append("e.printStackTrace();"); sb.append("\r\n\t\t"); sb.append("} finally {"); sb.append("\r\n\t\t\t"); sb.append("try {"); sb.append("\r\n\t\t\t\t"); sb.append("rs.close();"); sb.append("\r\n\t\t\t\t"); sb.append("ps.close();"); sb.append("\r\n\t\t\t\t"); sb.append("con.close();"); sb.append("\r\n\t\t\t"); sb.append("} catch (SQLException e) {"); sb.append("\r\n\t\t\t\t"); sb.append("e.printStackTrace();"); sb.append("\r\n\t\t\t"); sb.append("}"); sb.append("\r\n\t\t"); sb.append("}"); sb.append("\r\n\t\t"); sb.append("return list;\r\n"); } /************************************* * 功能:將輸入字串的首字母改成大寫 * * @param str * @return */ private static String initcap(String str) { char[] ch = str.toCharArray(); if (ch[0] >= 'a' && ch[0] <= 'z') { ch[0] = (char) (ch[0] - 32); } return new String(ch); } /** * 功能:獲得列的資料型別 * * @param sqlType * @return */ private static String sqlType2JavaType(String sqlType) { if (sqlType.equalsIgnoreCase("bit")) { return "boolean"; } else if (sqlType.equalsIgnoreCase("tinyint")) { return "byte"; } else if (sqlType.equalsIgnoreCase("smallint")) { return "short"; } else if (sqlType.equalsIgnoreCase("int")) { return "int"; } else if (sqlType.equalsIgnoreCase("bigint")) { return "long"; } else if (sqlType.equalsIgnoreCase("float")) { return "float"; } else if (sqlType.equalsIgnoreCase("decimal") || sqlType.equalsIgnoreCase("numeric") || sqlType.equalsIgnoreCase("real") || sqlType.equalsIgnoreCase("money") || sqlType.equalsIgnoreCase("smallmoney")) { return "double"; } else if (sqlType.equalsIgnoreCase("varchar") || sqlType.equalsIgnoreCase("char") || sqlType.equalsIgnoreCase("nvarchar") || sqlType.equalsIgnoreCase("nchar") || sqlType.equalsIgnoreCase("text")) { return "String"; } else if (sqlType.equalsIgnoreCase("datetime")) { return "Date"; } else if (sqlType.equalsIgnoreCase("image")) { return "Blod"; } return null; } // /** // * 功能:翻譯字串 // * // * @param urlString // * @return // * @throws Exception // */ // private static String getTranslateResult(String urlString) throws Exception { // final String PreUrl = "http://www.baidu.com/s?wd="; //百度搜索URL // final String TransResultStartFlag = "<span class=\"op_dict_text2\">"; //翻譯開始標籤 // final String TransResultEndFlag = "</span>"; //翻譯結束標籤 // URL url = new URL(PreUrl + urlString); //生成完整的URL // // 開啟URL // HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); // // 得到輸入流,即獲得了網頁的內容 // BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); // String preLine = ""; // String line; // int flag = 1; // // 讀取輸入流的資料,並顯示 // String content = ""; //翻譯結果 // while ((line = reader.readLine()) != null) { //獲取翻譯結果的演算法 // if (preLine.indexOf(TransResultStartFlag) != -1 && line.indexOf(TransResultEndFlag) == -1) { // content += line.replaceAll(" | ", ""); //去電原始碼上面的半形以及全形字元 // break; // } // if (line.indexOf(TransResultEndFlag) != -1) { // flag = 1; // } // if (flag == 1) { // preLine = line; // } // } // return content;//返回翻譯結果 // } }