图案解锁
文件写入 文件读取
第一次运行:
设置密码:6位
从终端一次输入一个整数
确认密码
设置过了 3次错误的机会
检查密码:相同提示解锁成功 不同就提示密码不一致 重新输入
创建文件
public static void main(String[] args) throws IOException {
//创建文件 完整路径(系统文件夹里可以找到)
String path="E:/Android/java/Java/src/main/java/day8";
//path/1.text
File file=new File(path.concat("/1.txt"));
//判断是否存在(也可以采用 try{ }catch{ })
if (file.exists()==false){
//不存在就创建
file.createNewFile();
}
向文件写入数据
1.字节流
//1.创建文件输出流对象
FileOutputStream fos=new FileOutputStream(file);
//2.调用write方法写入
byte[] text={'1','2','3'};
fos.write(text);
//3.操作完毕需要关闭stream对象
fos.close();
2.字符流
FileWriter fw=new FileWriter(file);
char[] name={'张','钰','琪'};
fw.write(name);
fw.close();
读取文件内容
FileInputStream fis=new FileInputStream(file);
//字节
byte[] name=new byte[9];
int count=fis.read(name);
fis.close();
System.out.println(count+" "+new String(name));
//字符
FileReader fr=new FileReader(file);
char[] book=new char[3];
count=fr.read(book);
fr.close();
System.out.println(count+" "+new String(book));
向文件里存一个对象
Person xw=new Person();
xw.name="小王";
xw.age=20;
OutputStream os=new FileOutputStream(file);
ObjectOutputStream oos=new ObjectOutputStream(os);
oos.write();
oos.close();
从文件里面读取一个对象
InputStream is=new FileInputStream(file);
ObjectInputStream ois=new ObjectInputStream(is);
Person xw = (Person) ois.readObject();
System.out.println(xw.name+" "+xw.age);
ois.close();