要處理的資料為以下資料,需要將資料中的(,),以及|全部變成tab空格:
1 rhizoma polygoni cuspidati(hu zhang|虎杖) cyp1b1
2 rhizoma polygoni cuspidati(hu zhang|虎杖) hgf hpta
3 rhizoma polygoni cuspidati(hu zhang|虎杖) brca1 rnf53
要求處理後的資料變成這樣:
2 rhizoma polygoni cuspidati
hu zhang
虎杖 hgf
hpta
3 rhizoma polygoni cuspidati
hu zhang
虎杖 brca1
rnf53
4 rhizoma polygoni cuspidati
hu zhang
虎杖 brca2
facd
fancd1
大體步驟是先用while迴圈(注意這個迴圈是怎麼寫的,好好理解)一行一行讀取要處理的檔案,對每一行做處理,
處理完一行將這一行寫入另乙個檔案直到所有行都處理完。
1.首先要建立乙個讀取txt檔案的bufferedreader用來讀取檔案
public bufferedreader createbr(string pathname)catch(exception e)
return null;
}
2.然後建立乙個寫入txt檔案的bufferedwriter用來寫入檔案
public bufferedwriter createbw(string pathname)
filewriter fw = new filewriter(file);
bufferedwriter bw = new bufferedwriter(fw);
return bw;
}catch(exception e)
return null;
}3.具體的main函式如下
public static void main(string args)
}for(int i = temp.length-1;i >= 0; i--)
}for(int i = temp.length-1;i >= 0; i--)
}system.out.println( string.valueof(temp));
bw.write(string.valueof(temp) + "\r\n");
+ "\r\n");
line = br.readline(); //一次讀取一行資料
}// 把快取區內容壓入檔案
bw.flush();
// 最後記得關閉檔案
bw.close();
br.close();
}catch(exception e)
}4.剛開始在main函式中寫入檔案是這麼寫的:
system.out.println( temp.tostring());
bw.write(temp.tostring + "\r\n");
結果控制台和輸出檔案都出現這樣的錯誤,
[c@1fb8ee3
[c@61de33
[c@14318bb
剛開始以為是亂碼,就在編碼上找原因,後來修改編碼後發現仍舊是這種錯誤,後來一問才知道
原來這種形式的輸出是一些記憶體位址,知道這個後,就查了一下才知道char型別轉換成string
型別不是用temp.tostring()函式,而是這樣寫的 string.valueof(temp),修改後控制台和輸出檔案
顯示記憶體位址的問題就解決了
java寫入txt檔案
用另乙個構造方法filewriter filewriter new filewriter c result.txt true true代表追加 同理 bufferedwriter writer new bufferedwriter new filewriter new file c result.t...
python 寫入 讀取txt檔案
with open desc.txt w as f f.write 我是個有想法的小公舉 這句 自帶檔案關閉功能。比較常用的檔案讀寫選項 r 以讀的方式開啟,只能讀檔案,若檔案不存在,則發生異常 w 以寫的方式開啟,只能寫檔案,如果檔案不存在,建立該檔案 如果檔案已存在,先清空,再開啟檔案 rb 以...
java讀取txt檔案
一 讀取txt檔案。1 步驟 建立檔案控制代碼 file file new file filepath 將檔案內容讀取到記憶體中 new fileinputstream file 封裝資料 inputstreamreader inputstreamreader read new inputstrea...