📄 sortfilebybihua.java
字号:
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 packageimport 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 SortFileByBihua{ private static Connection con; private static Statement stat = null; private static ResultSet rs; 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) { SortStringByBihua ss = new SortStringByBihua(line); result.add(ss); line = br.readLine(); } 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++) { SortStringByBihua ss = (SortStringByBihua) result.get(i); fw.write(ss.getS()); fw.write("\n"); } fw.close(); con.close(); } 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); int c1_bihua=getbihua(c1); int c2_bihua=getbihua(c2); if(c1_bihua<c2_bihua) return -1; else if(c1_bihua>c2_bihua) return 1; //////////////////////////////////////////////////// int c1_qibi = getqibi(c1); int c2_qibi = getqibi(c2); System.out.println(); System.out.println(c1_qibi); System.out.println(c2_qibi); if (c1_qibi < c2_qibi) { return -1; } else if (c1_qibi> c2_qibi) { return 1; } int c1_cibi=getcibi(c1); int c2_cibi=getcibi(c2); if(c1_cibi<c2_cibi) return -1; else if(c1_cibi>c2_cibi) return 1; }//////////////////////////////////////////////////////////////////////////////////// if (s1.length() > s2.length()) { return 1; } else if (s1.length() < s2.length()) { return -1; } return 0; } public static int getbihua(String hanzi) { String querystr = "select * from pinyin where hanzi='"+hanzi+"'"; System.out.println(querystr); try { rs = stat.executeQuery(querystr); while (rs.next()) { return rs.getInt("bihua"); } } catch (SQLException e3) { // TODO Auto-generated catch block e3.printStackTrace(); } return 0;}////////////////////////////////////public static int getqibi(String hanzi) { String querystr = "select * from pinyin where hanzi='"+hanzi+"'"; System.out.println(querystr); try { rs = stat.executeQuery(querystr); while (rs.next()) { return rs.getInt("qibi"); } } catch (SQLException e3) { // TODO Auto-generated catch block e3.printStackTrace(); } return 0;}public static int getcibi(String hanzi) { String querystr = "select * from pinyin where hanzi='"+hanzi+"'"; System.out.println(querystr); try { rs = stat.executeQuery(querystr); while (rs.next()) { return rs.getInt("cibi"); } } catch (SQLException e3) { // TODO Auto-generated catch block e3.printStackTrace(); } 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 SortStringByBihua implements Comparable { String s; public SortStringByBihua(String s) { this.s = s; } public int compareTo(Object o) { SortStringByBihua compare = (SortStringByBihua) o; return SortFileByBihua.compareString(this.s, compare.s); } public String getS() { return this.s; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -