Created
February 1, 2018 06:18
-
-
Save ckoshien/d54901e285e62ac47374c17479b664da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedInputStream; | |
import java.io.BufferedReader; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.LineNumberReader; | |
import java.io.UnsupportedEncodingException; | |
import java.security.DigestInputStream; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.ArrayList; | |
import com.sun.org.apache.bcel.internal.generic.GETSTATIC; | |
public class ranbatojava { | |
private static ArrayList<String> data; // データを格納するArrayList | |
public static void main(String[] args) throws ClassNotFoundException, | |
SQLException, NoSuchAlgorithmException, IOException { | |
data = new ArrayList<String>(); // ArrayListオブジェクトの生成 | |
String filename="200807.dat"; | |
System.out.println("ファイルのMD5ハッシュ:"+getMd5(filename)); | |
//ファイルの拡張子を格納 | |
String filename_extend = filename.substring(filename.length()-4); | |
if (filename_extend.equals(".dat")){ | |
readNoteData(filename); | |
} else if (filename_extend.equals(".htm")||filename_extend.equals("html")){ | |
readHtml(filename); | |
} | |
} | |
// データをファイルから読み込んでArrayListに格納 | |
private static void readNote(String filename) { | |
LineNumberReader aInputFile =null; | |
try { | |
// FileReader aFileReader = new FileReader(filename); | |
// BufferedReader aInputFile = new BufferedReader(aFileReader); | |
aInputFile = new LineNumberReader(new InputStreamReader(new FileInputStream(filename),"sjis")); | |
// ファイルの終わりに到達するまでデータを読み込む | |
String aLine; | |
while ((aLine = aInputFile.readLine()) != null) { | |
data.add(aLine); | |
// System.out.println("["+aInputFile.getLineNumber()+"]:"+aLine); // データが追加されていることを確認するための出力 | |
} | |
// System.out.println("データが追加されている数 "); | |
// System.out.println(data.size());// データが追加されている数を確認 | |
aInputFile.close(); | |
} catch (FileNotFoundException e) { | |
} catch (IOException e) { | |
}// catch end | |
}//method end | |
/** | |
* 文字列をbyte単位で切り出すメソッド(HSPのstrmid) | |
* @param str | |
* @param beginindex | |
* @param bytes | |
* @return | |
* @throws UnsupportedEncodingException | |
*/ | |
public static String subStringBytes(String str,int beginindex,int bytes) | |
throws UnsupportedEncodingException{ | |
String str2=new String(str.getBytes("Shift-JIS"),beginindex,bytes,"Shift-JIS"); | |
return str2; | |
}//method end | |
/** | |
* datファイルに格納されているデータを読み出すメソッド | |
* @param filename | |
* @throws NumberFormatException | |
* @throws UnsupportedEncodingException | |
*/ | |
public static void readNoteData(String filename) throws NumberFormatException, UnsupportedEncodingException{ | |
readNote(filename); | |
int limit100,scr_st,id_st; | |
if (data.get(0).equals("#DATAFILE ver.1.00#")){ | |
limit100=0; | |
scr_st=9; | |
id_st=18; | |
}else{ | |
limit100=1; | |
scr_st=6; | |
id_st=15; | |
} | |
int lineNum=2; | |
do{ | |
String requestNoUp4=data.get(lineNum+1).substring(0,4); | |
String requestNoLow2=data.get(lineNum+1).substring(5,5+2); | |
String songTitle=data.get(lineNum+2); | |
String artist=data.get(lineNum+3); | |
int sangNum=Integer.parseInt(data.get(lineNum+5)); | |
//ランバトネーム | |
String[]singerName={"","",""}; | |
// String[]singerPref={""}; | |
//完全一致検索の準備 | |
for (int i=0;i<singerName.length;i++){ | |
int strlen=singerName[i].getBytes("Shift-JIS").length; | |
for (int j=0;j<(16-strlen);j++) | |
singerName[i]=singerName[i]+" "; | |
} | |
//得点・順位検索 | |
double score=0; | |
int rank=0; | |
for (int i=1;i<sangNum+1;i++){ | |
String str=data.get(lineNum+5+i); | |
for(int j=0;j<singerName.length;j++){ | |
if (subStringBytes(str, id_st, 16).equals(singerName[j])){ | |
score = Double.parseDouble(subStringBytes(str,scr_st,6)); | |
rank = i; | |
} | |
} | |
} | |
System.out.println(requestNoUp4+"-"+requestNoLow2); | |
System.out.println(songTitle); | |
System.out.println(artist); | |
System.out.println("順位"+rank+"/"+sangNum); | |
System.out.println("得点"+score); | |
lineNum=(lineNum+5)+sangNum+1;//次の曲に移動 | |
}while(lineNum<data.size()-1); | |
}//readData end | |
/** | |
* テキストファイルからデータを読み出して1つの文字列に連結するメソッド | |
* @param filename | |
*/ | |
public static String readFileAsString(String filename){ | |
try { | |
BufferedReader aInputFile = new BufferedReader(new InputStreamReader(new FileInputStream(filename),"sjis")); | |
StringBuilder sb = new StringBuilder(); | |
// ファイルの終わりに到達するまでデータを読み込む | |
String aLine; | |
while ((aLine = aInputFile.readLine()) != null) { | |
//1行ずつ読み込んで文字列を連結 | |
sb.append(aLine); | |
} | |
aInputFile.close(); | |
String str=new String(sb); | |
return str; | |
} catch (FileNotFoundException e) { | |
return "-1"; | |
} catch (IOException e) { | |
return "-1"; | |
}// catch end | |
}//readFileAsString end | |
/** | |
* HTMLファイルを読み込んで成績を読み出すメソッド | |
* @param filename | |
* @throws NumberFormatException | |
* @throws UnsupportedEncodingException | |
*/ | |
public static void readHtml(String filename)throws | |
NumberFormatException, UnsupportedEncodingException{ | |
String fileContents=readFileAsString(filename); | |
int mainPointer = 0; | |
int limit100 = 0; | |
int index=0; | |
if(fileContents.indexOf("<style TYPE=\"text/css\">", mainPointer) != -1){ | |
limit100=1; | |
}else{ | |
limit100=0; | |
} | |
int length=0; | |
do{ | |
int pointer=0; | |
if (limit100==1){ | |
// | |
index = fileContents.indexOf("class=\"score", mainPointer); | |
if (index==-1){ | |
break;//解析終了 | |
} | |
//得点の取得 | |
pointer = index + 48; | |
length = fileContents.indexOf("<", pointer); | |
double score = Double.parseDouble(fileContents.substring(pointer,length)); | |
//順位の取得 | |
index = fileContents.indexOf("class=\"rank", mainPointer); | |
pointer = index + 40; | |
length = fileContents.indexOf("<", pointer); | |
String strRank = fileContents.substring(pointer,length); | |
int rank; | |
if (strRank.equals("--")){ | |
rank=0; | |
}else{ | |
rank = Integer.parseInt(strRank); | |
} | |
//リクエストno.の取得 | |
index = fileContents.indexOf("request_no=", mainPointer); | |
pointer = index + ("request_no=").length(); | |
length = fileContents.indexOf("\"", pointer); | |
String requestNo = getString(fileContents,mainPointer,"request_no=","\"",0); | |
String requestNoUp4=requestNo.substring(0,0+4); | |
String requestNoLow2=requestNo.substring(5,5+2); | |
//曲名取得 | |
String beginStr="<td rowspan=1 class=\"up\">"; | |
String endStr="<"; | |
String songTitle =getString(fileContents, mainPointer,beginStr,endStr,0); | |
String[] top3=new String[6]; | |
//1位の得点とネームの取得 | |
top3[0] = getString(fileContents,mainPointer,"1位【","点】",0); | |
top3[1] = getString(fileContents, mainPointer,"点】<br>","</td>",0); | |
//2位のネームと得点の取得 | |
top3[2] = getString(fileContents, mainPointer,"2位【","点】",0); | |
//indexを2位【の後ろに設定 | |
index = fileContents.indexOf("2位【", mainPointer)+("2位【").length(); | |
//pointerを"2位【"の後ろの"点】"に移動 | |
pointer = fileContents.indexOf("点】", index)+("点】").length(); | |
length = fileContents.indexOf("</td>",pointer); | |
top3[3] = fileContents.substring(pointer,length); | |
//歌唱人数の取得 | |
// index = fileContents.indexOf("class=\"down\">/", mainPointer); | |
// pointer = index + ("class=\"down\">/").length()+1; | |
// length = fileContents.indexOf("<", pointer); | |
// String strNum = fileContents.substring(pointer,length); | |
beginStr="class=\"down\">/"; | |
endStr="<"; | |
String strNum = getString(fileContents,mainPointer,beginStr,endStr,1); | |
int sangNum; | |
if (strNum.equals("100+")){ | |
sangNum=100; | |
if (score>0 && rank==0){ | |
rank=101; | |
sangNum=101; | |
} | |
}else{ | |
sangNum = Integer.parseInt(strNum); | |
} | |
//アーティスト名取得 | |
String artist = getString(fileContents, mainPointer, "<td rowspan=1 class=\"down\">", "<",0); | |
//3位の得点とネームを取得 | |
top3[4] = getString(fileContents, mainPointer, "3位【", "点】",0); | |
pointer = fileContents.indexOf("点】", pointer) + ("点】").length(); | |
length = fileContents.indexOf("</td>", pointer); | |
top3[5] = fileContents.substring(pointer,length); | |
//表示 | |
System.out.println(requestNoUp4+"-"+requestNoLow2); | |
System.out.println(songTitle); | |
System.out.println(artist); | |
System.out.println("順位:"+rank+"/"+sangNum); | |
System.out.println("得点:"+score); | |
System.out.println("1位"+top3[0]+"点:"+top3[1]); | |
System.out.println("2位"+top3[2]+"点:"+top3[3]); | |
System.out.println("3位"+top3[4]+"点:"+top3[5]); | |
}else{ | |
//v1.0以降のHTML | |
index = fileContents.indexOf("class=\"MS", mainPointer); | |
//得点取得 | |
pointer = index+ ("class=\"MS").length()+3; | |
length = fileContents.indexOf("<",pointer); | |
if (index==-1){ | |
break;//解析終了 | |
} | |
double score = Double.parseDouble(fileContents.substring(pointer,length)); | |
//順位取得 | |
String beginStr="class=\"MR"; | |
String endStr="<"; | |
int rank= Integer.parseInt(getString(fileContents,mainPointer,beginStr,endStr,3)); | |
//リクエストnoの取得 | |
// index = fileContents.indexOf("request_no=", mainPointer); | |
// pointer= index +("request_no=").length(); | |
// length = fileContents.indexOf("\"",pointer); | |
beginStr="request_no="; | |
endStr="\""; | |
String requestNo=getString(fileContents,mainPointer,beginStr,endStr,0); | |
String requestNoUp4 = requestNo.substring(0, 0+4); | |
String requestNoLow2 = requestNo.substring(5, 5+2); | |
//曲名取得 | |
// index = fileContents.indexOf("class=\"sng\"", mainPointer); | |
// pointer= index +("class=\"sng\"").length()+1; | |
// length = fileContents.indexOf("<",pointer); | |
// String songTitle = fileContents.substring(pointer, length); | |
beginStr="class=\"sng\""; | |
endStr="<"; | |
String songTitle = getString(fileContents,mainPointer,beginStr,endStr,1); | |
//1位の得点・ネームを取得 | |
String[] top3=new String[6]; | |
// index = fileContents.indexOf("1位【", mainPointer); | |
// pointer= index +("1位【").length(); | |
// length = fileContents.indexOf("点】",pointer); | |
// top3[0]=fileContents.substring(pointer,length); | |
top3[0]=getString(fileContents,mainPointer,"1位【","点】",0); | |
// index = fileContents.indexOf("点】<br>", mainPointer); | |
// pointer= index+("点】<br>").length(); | |
// length=fileContents.indexOf("</td>",pointer); | |
// top3[1]=fileContents.substring(pointer,length); | |
top3[1]=getString(fileContents,mainPointer,"点】<br>","</td>",0); | |
//2位の得点・ネームを取得 | |
// index = fileContents.indexOf("2位【", mainPointer); | |
// pointer= index +("2位【").length(); | |
// length = fileContents.indexOf("点】",pointer); | |
// top3[2]=fileContents.substring(pointer,length); | |
top3[2]=getString(fileContents,mainPointer,"2位【","点】",0); | |
index = fileContents.indexOf("2位【", mainPointer)+("2位【").length(); | |
//pointerを"2位【"の後ろの"点】"に移動 | |
pointer = fileContents.indexOf("点】<br>", index)+("点】<br>").length(); | |
length = fileContents.indexOf("</td>",pointer); | |
top3[3] = fileContents.substring(pointer,length); | |
//3位の得点・ネームを取得 | |
// index = fileContents.indexOf("3位【", mainPointer); | |
// pointer= index +("3位【").length(); | |
// length = fileContents.indexOf("点】",pointer); | |
// top3[4]=fileContents.substring(pointer,length); | |
top3[4]=getString(fileContents,mainPointer,"3位【","点】",0); | |
index = fileContents.indexOf("3位【", mainPointer)+("3位【").length(); | |
//pointerを"3位【"の後ろの"点】"に移動 | |
pointer = fileContents.indexOf("点】<br>", index)+("点】<br>").length(); | |
length = fileContents.indexOf("</td>",pointer); | |
top3[5] = fileContents.substring(pointer,length); | |
//総歌唱人数を取得 | |
index = fileContents.indexOf("class=\"RK", mainPointer); | |
//pointerを"class=RK"の後ろ+4に移動 | |
pointer = fileContents.indexOf("class=\"RK", index)+("class=\"RK").length()+5; | |
length = fileContents.indexOf("</td>",pointer); | |
int sangNum=Integer.parseInt(fileContents.substring(pointer, length)); | |
//アーティスト名取得 | |
// index = fileContents.indexOf("class=\"sgr\">", mainPointer); | |
// pointer= index +("class=\"sgr\">").length(); | |
// length = fileContents.indexOf("</td>",pointer); | |
// String artist = fileContents.substring(pointer,length); | |
beginStr="class=\"sgr\">"; | |
endStr="</td>"; | |
String artist = getString(fileContents,mainPointer,beginStr,endStr,0); | |
//表示 | |
System.out.println(requestNoUp4+"-"+requestNoLow2); | |
System.out.println(songTitle); | |
System.out.println(artist); | |
System.out.println("順位:"+rank+"/"+sangNum); | |
System.out.println("得点:"+score); | |
System.out.println("1位"+top3[0]+"点:"+top3[1]); | |
System.out.println("2位"+top3[2]+"点:"+top3[3]); | |
System.out.println("3位"+top3[4]+"点:"+top3[5]); | |
} | |
mainPointer=pointer; | |
}while(true); | |
}//readHtmlend | |
public static String getMd5(String filename) throws NoSuchAlgorithmException, IOException { | |
MessageDigest md = MessageDigest.getInstance("MD5"); | |
DigestInputStream inStream = new DigestInputStream( | |
new BufferedInputStream(new FileInputStream(filename)), md | |
); | |
while (inStream.read() != -1) { | |
} | |
byte[] digest = md.digest(); | |
inStream.close(); | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < digest.length; i++) { | |
int b = (0xFF & digest[i]); | |
if (b < 16) | |
sb.append("0"); | |
sb.append(Integer.toHexString(b)); | |
} | |
return sb.toString(); | |
}//getMd5end | |
/** | |
* ファイルの内容から文字を抜き出すメソッド | |
* @param fileContents ファイルの中身 | |
* @param mainPointer 探索ポインタの位置 | |
* @param beginStr 始まりの文字列 | |
* @param endStr 終わりの文字列 | |
* @param cnt 各種調整カウンタ | |
* @return | |
*/ | |
public static String getString(String fileContents,int mainPointer,String beginStr,String endStr,int cnt) { | |
int index = fileContents.indexOf(beginStr, mainPointer); | |
int pointer = index + beginStr.length()+cnt; | |
int length = fileContents.indexOf(endStr, pointer); | |
// System.out.println(mainPointer+","+pointer+","+index+","+length); | |
return fileContents.substring(pointer,length); | |
} | |
}//class end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment