ファイル入出力:サンプル

public class CreateHtml {
public static void main(String[] str) {
//ファイル名指定
String inputfilename = “D:\\dummy\\old.htm”;//読み出し用のファイル
String outputfilename = “D:\\dummy\\new.htm”; //書き出しファイル
//ファイル読み込み開始
File file = new File(inputfilename);
try {
FileInputStream fs = new FileInputStream(file);
InputStreamReader ir = new InputStreamReader(fs,”SJIS”);
BufferedReader br = new BufferedReader(ir);
StringBuffer buffer = new StringBuffer();
char[] readBuffer = new char[8192];
int length = br.read(readBuffer, 0, 8192);
while(length>=0){
buffer.append(readBuffer,0,length);
length = br.read(readBuffer, 0, 8192);
}
String out = buffer.toString();
//System.out.print(out);
br.close();
ir.close();
fs.close();
//ファイル読み込み終了
///////////////文字列操作開始///////////////
int searchStartNo = 0;
int searchEndNo = 0;
String startKey1 = “color:blue’>・”;
String startKey2 = “(”;
String startKey = startKey1 + startKey2;
String endKey = “)”;
String afterstr = “”;//変更後
String nokori =””;
//検索
while((out.indexOf(startKey))!=-1){
~省略~
out = nokori;//残りの対象文字列格納
//初期化
searchStartNo = out.indexOf(startKey,searchEndNo);
searchEndNo = out.indexOf(endKey,searchStartNo);
}
afterstr = afterstr + nokori;//残りの文字列を付加
//ファイル書き込み開始
File outfile = new File(outputfilename);
outfile.createNewFile();
FileOutputStream outfs = new FileOutputStream(outfile);
OutputStreamWriter ow = new OutputStreamWriter(outfs,”SJIS”);
BufferedWriter bw = new BufferedWriter(ow);
StringBuffer outbuffer = new StringBuffer(afterstr);//変更後の文字列を書き出し
bw.write(outbuffer.toString(), 0, outbuffer.length());
bw.flush();
bw.close();
ow.close();
fs.close();
//ファイル書き込み終了
} catch (FileNotFoundException e) {
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
}
}
//全角→半角変換
public static String replaceZenkakuHankaku(String str){
StringBuffer buffer = new StringBuffer(str);
String fromArray[] = {“A”,”B”,”C”,”D”,”E”,”F”,”G”,”H”,”I”,”J”,”K”,”L”,”M”,”N”,”O”,”P”,”Q”,”R”,”S”,”T”,”U”,”V”,”W”,”X”,”Y”,”Z”,”0”,”1”,”2”,”3”,”4”,”5”,”6”,”7”,”8”,”9”,”-”,”ー”};
String toArray[] = {“A”,”B”,”C”,”D”,”E”,”F”,”G”,”H”,”I”,”J”,”K”,”L”,”M”,”N”,”O”,”P”,”Q”,”R”,”S”,”T”,”U”,”V”,”W”,”X”,”Y”,”Z”,”0″,”1″,”2″,”3″,”4″,”5″,”6″,”7″,”8″,”9″,”-“,”-“};
for(int i=0;i
その他

コメント

  1. ??Blog より:

    納豆キャベツトライ

タイトルとURLをコピーしました