java的demo_Java Demo

图案解锁

文件写入 文件读取

第一次运行:

设置密码: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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值