日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

文件保存,String与int转换。

發(fā)布時間:2025/6/15 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 文件保存,String与int转换。 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標準>>>

public void yuyongfile(int num_insect) throws IOException {
try {
FileOutputStream os = this.openFileOutput("yuyong.txt",
MODE_APPEND);
OutputStreamWriter outWriter = new OutputStreamWriter(os);
outWriter.write(num_insect);
//outWriter.write("\n");
outWriter.close();
// Toast.makeText(this, "成功寫入", 1).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "文件沒有找到", 1).show();
} catch (IOException e) {
e.printStackTrace();
}

}


其中,openFileOutput方法的第二個參數(shù)有很多方式,詳見

http://my.oschina.net/laigous/blog/29076

http://my.oschina.net/tcy901209/blog/101022



由于這種儲存方式是用流來存儲數(shù)據(jù),故如果分次儲存到同一個文件時,取出來的時候會有麻煩。例如存數(shù)字。

方法:

存的時候以String存進去。用/當分隔符

拿出來的時候直接拿整個字符串,再Split開


存:

public void yuyongfile(String num_insect) throws IOException {
try {
FileOutputStream os = this.openFileOutput("yuyong.txt",
MODE_APPEND);
OutputStreamWriter outWriter = new OutputStreamWriter(os);
outWriter.write("/"+num_insect);
//outWriter.write("\n");
outWriter.close();
// Toast.makeText(this, "成功寫入", 1).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "文件沒有找到", 1).show();
} catch (IOException e) {
e.printStackTrace();
}
}


取:

public int[] aboutfile() throws IOException {
int[] num_insect=new int[100];
? ? ? ? String res="";
? ? ? ? String[] ins;
FileInputStream fin = openFileInput("yuyong.txt");
int length = fin.available();
byte[] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
ins=res.split("/");
// Toast.makeText(context, ins.length+"int[0]:"+ins[0], Toast.LENGTH_SHORT).show();
for(int i=0;i<ins.length;i++){
num_insect[i]=Integer.parseInt(ins[i]);
}
fin.close();
return num_insect;
}


其中包含字符轉(zhuǎn)整形,整形轉(zhuǎn)字符。

轉(zhuǎn)載于:https://my.oschina.net/u/944946/blog/116092

總結(jié)

以上是生活随笔為你收集整理的文件保存,String与int转换。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。