文件保存,String与int转换。
2019独角兽企业重金招聘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方法的第二个参数有很多方式,详见
http://my.oschina.net/laigous/blog/29076
http://my.oschina.net/tcy901209/blog/101022
由于这种储存方式是用流来存储数据,故如果分次储存到同一个文件时,取出来的时候会有麻烦。例如存数字。
方法:
存的时候以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;
}
其中包含字符转整形,整形转字符。
转载于:https://my.oschina.net/u/944946/blog/116092
总结
以上是生活随笔为你收集整理的文件保存,String与int转换。的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 指定字符串按指定长度进行剪切
- 下一篇: ios学习记录 UITextField