import android.content.Context;
import android.util.Log;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* Created by zhizhao on 2015/11/1 0001 in 16:00.
*/
public class UsingFileInputStream {
private Context fileContext;
private String fileName;
private String fileUserName;
private String filePassword;
public UsingFileInputStream(String name, Context context, String userName, String password) {
this.fileName = name;
this.fileContext = context;
this.fileUserName = userName;
this.filePassword = password;
}
//保存的时候是在文件内容中连续写入,也就是在之前保存的数据基础上再次写入。
public void writeFileInputStream() {
try {
FileOutputStream fileOutputStream = fileContext.openFileOutput(fileName,
fileContext.MODE_PRIVATE);
byte[] byteUserName = fileUserName.getBytes();
byte[] bytePassword = filePassword.getBytes();
fileOutputStream.write(byteUserName);
fileOutputStream.write(bytePassword);
Log.v("FileInputStream保存结果 ", "UserName = " + fileUserName + " Password = " + filePassword);
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//读取文件是把整个文件的内容进行读取。如果要加入解析,则保存的时候保存为特殊格式。
public void readFileInputStream() {
try {
FileInputStream fileInputStream = fileContext.openFileInput(fileName);
int len = fileInputStream.available();
byte[] buffer = new byte[len];
fileInputStream.read(buffer);
Log.v("读取到的文件为:", ""+new String(buffer));
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void writeFile(){
UsingFileInputStream fileInputStream = new UsingFileInputStream("account",
MySaveDataActivity.this, userName, userPass);
fileInputStream.writeFileInputStream();
tvReadInformation.setText("保存成功!" + "\n UserName = " + userName + "\n UserPass = " + userPass);
}
private void readFile(){
UsingFileInputStream fileInputStream = new UsingFileInputStream("account",
MySaveDataActivity.this, userName, userPass);
fileInputStream.readFileInputStream();
tvReadInformation.setText("读取成功!"+"\n UserName = "+userName+"\n UserPass = "+userPass);
}
filename = "abc.txt" ; FileInputStream fis = new FileInputStream(filename);
java.io.FileNotFoundException: dbconfig.properties (系统找不到指定的文件。) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:106) at java.io.FileInputStream.<init>(FileInputStream.java:66)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有