⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sortfileutil.java~60~

📁 完成一级汉字和二级汉字的按拼音排序和按笔画排序功能
💻 JAVA~60~
字号:
package net.bishe.file;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
import java.sql.*; // JDBC package
import com.microsoft.jdbc.sqlserver.*;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Hashtable;
import java.util.*;
import java.io.File;
import java.io.FileWriter;

public class SortFileUtil {
  public SortFileUtil() {
  }

  private static Connection con;
  private static Statement stat = null;
  private static ResultSet rs;
  private static String compare_hanzi1 = null;
  private static String compare_hanzi2 = null;

  public static void main(String[] args) throws Exception {

    try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con = DriverManager.getConnection("jdbc:odbc:sjy", "", "");
      stat = con.createStatement();
    }
    catch (ClassNotFoundException e) {}

    FileReader fr = new FileReader("d:\\unsortedfile.txt");
    BufferedReader br = new BufferedReader(fr);
    String line = br.readLine();
    LinkedList result = new LinkedList();

    while (line != null) {

      //    System.out.println(line);
      line = br.readLine();
      if (line != null) {
        SortString ss = new SortString(line);
        result.add(ss);

      }
    }

    Collections.sort(result);
    File f = new File("d:\\sortedfile.txt");

    if (f.exists()) {
      f.delete();

    }
    else {
      f.createNewFile();

    }

    FileWriter fw = new FileWriter(f);
    for (int i = 0; i < result.size(); i++) {
      SortString ss = (SortString) result.get(i);
      fw.write(ss.getS());
      fw.write("\n");
    }

    fw.close();
    con.close();
  }

  public static boolean isDuoyinzi(String hanzi) {

    String querystr2 = "select * from duoyinci where duoyinzi='" + hanzi + "'";
    try {
      rs = stat.executeQuery(querystr2);
      if (rs.next()) {
        return true;
      }

    }
    catch (SQLException e3) {
      // TODO Auto-generated catch block
      e3.printStackTrace();

    }
    return false;

  }

  public static String getDuoyinziPinyin(String hanzi, String s) {

    String querystr2 = "select * from duoyinci where duoyinzi='" + hanzi + "'" +
        " and duoyinci='" + s + "'";
    try {
      rs = stat.executeQuery(querystr2);
      while (rs.next()) {
        return rs.getString("pinyin");
      }

    }
    catch (SQLException e3) {
      // TODO Auto-generated catch block
      e3.printStackTrace();

    }
    return null;
  }

  public static String getpinyin(String hanzi) {

    String querystr = "select * from pinyin where hanzi='" + hanzi + "'";
    try {
      rs = stat.executeQuery(querystr);
      while (rs.next()) {
        return rs.getString("pinyin");
      }

    }
    catch (SQLException e3) {
      // TODO Auto-generated catch block
      e3.printStackTrace();

    }
    return null;
  }

  public static int compareString(String s1, String s2) {
    int length = 0;
    if (s1.length() > s2.length()) {
      length = s2.length();
    }
    else {
      length = s1.length();
    }
    for (int i = 0; i < length; i++) {
      String c1 = s1.substring(i, i + 1);
      String c2 = s2.substring(i, i + 1);
      String c1_pinyin, c2_pinyin;
      if (isDuoyinzi(c1)) {
        c1_pinyin = getDuoyinziPinyin(c1, s1);
      }
      else {
        c1_pinyin = getpinyin(c1);
      }
      if (isDuoyinzi(c2)) {
        c2_pinyin = getDuoyinziPinyin(c2, s2);
      }
      else {
        c2_pinyin = getpinyin(c2);

      }

      if (c1_pinyin == null) {
        c1_pinyin = getpinyin(c1);
      }
      if (c2_pinyin == null) {
        c2_pinyin = getpinyin(c2);
      }

      //   System.out.println(c1);
      ////   System.out.println(c2);
      //   System.out.println(c1_pinyin);
      //   System.out.println(c2_pinyin);
compare_hanzi1=c1;
      compare_hanzi2=c2;
      if (compareHanzi(c1_pinyin, c2_pinyin) > 0) {
        return 1;
      }
      else if (compareHanzi(c1_pinyin, c2_pinyin) < 0) {
        return -1;
      }

    }
    if (s1.length() > s2.length()) {
      return 1;
    }
    else if (s1.length() < s2.length()) {
      return -1;
    }
    return 0;

  }

  public static int compareHanzi(String this_pinyin, String that_pinyin,String   hanzi1,String hanzi2) {

    int length = 0;
    if (this_pinyin.length() > that_pinyin.length()) {
      length = that_pinyin.length();
    }
    else {
      length = this_pinyin.length();
    }
    for (int i = 0; i < length; i++) {
      String c1 = this_pinyin.substring(i, i + 1);
      String c2 = that_pinyin.substring(i, i + 1);
      if (c1.compareTo(c2) > 0) {
        return 1;
      }
      else if (c1.compareTo(c2) < 0) {
        return -1;
      }
    }

    int c1_yindiao = getyindiao(this_pinyin,hanzi1);
    int c2_yindiao = getyindiao(that_pinyin,hanzi2);
    System.out.println();
    System.out.println(c1_yindiao);
    System.out.println(c2_yindiao);
    if (c1_yindiao < c2_yindiao) {
      return -1;
    }
    else if (c1_yindiao > c2_yindiao) {
      return 1;
    }

    return 0;

  }

  public static int getyindiao(String pinyin,String  hanzi) {

    String querystr = "select * from pinyin where pinyin='" + pinyin + "' and hanzi='"+hanzi+"'";
    System.out.println(querystr);
    try {
      rs = stat.executeQuery(querystr);
      while (rs.next()) {
        return rs.getInt("yindiao");
      }

    }
    catch (SQLException e3) {
      // TODO Auto-generated catch block
      e3.printStackTrace();

    }
    return 0;
  }

}

class SortString
    implements Comparable {

  String s;

  public SortString(String s) {
    this.s = s;
  }

  public int compareTo(Object o) {
    SortString compare = (SortString) o;
    return SortFileUtil.compareString(this.s, compare.s);

  }

  public String getS() {
    return this.s;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -