目的
了解Java中文件输入输出的方式,写出图案解锁的模拟demo。
技术及其对应使用方法
1.输入输出流的概念
//创建文件 完整路径
String path = "D:/Program Files/Java/day1/src/main/java/day8";
//path/1.txt
File file = new File(path.concat("/1.txt"));
//判断是否存在
if (file.exists() == false){
//不存在就创建
file.createNewFile();
}
读取文件的内容
I/O 流
流的方向:参考的是自己的内存空间
输出流:从内存空间将数据写到外部设备(磁盘\硬盘\光盘)
输入流:将外部数据写到内存中
流:统一管理数据的写入和读取
输出流:开发者只需要将内存里面的数据写到流里面
输入流:或者从流里面读取数据
输出流:OutputStream字节流 Writer字符流
输入流:InputStream字节流 Reader字符流
I/O流对象 不属于内存对象 需要自己关闭
OutputStream和InputSteam都是抽象类 不能直接使用
FileOutputStream/FileInputStream
ObjectOutputStream/ObjectInputStream
FileWriter/FileReader
2.读写文件
//向文件写入数据-字节流
//1.创建文件输出流对象
FileOutputStream fos = new FileOutputStream(file);
//2.调用write方法写入
byte[] text = {'1','2','3','4'};
fos.write(text);
//3.操作完毕需要关闭stream对象
fos.close();
//向文件写入数据-字符流
FileWriter fw = new FileWriter(file);
char[] name = {'安','卓','开','发'};
fw.write(name);
fw.close();*/
//读取文件
FileInputStream fis = new FileInputStream(file);
byte[] name = new byte[12];
int count = fis.read(name);
fis.read(name);
fis.close();
System.out.println(count+" "+new String(name));
FileReader fr = new FileReader(file);
char[] book = new char[4];
count = fr.read(book);
fr.close();
System.out.println(count+" "+new String(book));
3.对象存取
//向文件里面存一个对象
//序列化 serializable
//保存的对象必须实现Serializable接口
//如果对象内部还有属性变量是其他类的对象
//这个类也必须实现Serializable接口
Person xw = new Person();
xw.name = "小王";
xw.age = 20;
OutputStream os = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(xw);
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();
4.图片文件复制
//将一个文件 copy 到另外一个位置
//1.源文件的路径
String sourcePath = "C:\\Users\\lenovo\\Desktop\\HINS.png";
//2.目标文件的路径
String desPath = "D:\\Program Files\\Java\\day1\\src\\main\\java\\day8\\HINS.png";
//3.图片 字节
FileInputStream fis = new FileInputStream(sourcePath);
FileOutputStream fos = new FileOutputStream(desPath);
byte[] in = new byte[1024];
int count = 0;
while ((count = fis.read(in)) != -1){
fos.write(in,0,count);
}
//或者
/* while(true) {
int count = fis.read(in);
if (count != -1){
//读取到内容了
//将这一次读取的内容写入到目标文件
fos.write(in,0,count);
}else {
break;
}
}
*/
fis.close();
fos.close();
5.使用Buffered提高读取和写入速度
long start = System.currentTimeMillis();
String sourcePath = "C:\\Users\\lenovo\\Desktop\\HINS.mp4";
String desPath = "D:\\Program Files\\Java\\day1\\src\\main\\java\\day8\\HINS.mp4";
//输入流
InputStream is = new FileInputStream(sourcePath);
BufferedInputStream bis = new BufferedInputStream(is);
//输出流
OutputStream os = new FileOutputStream(desPath);
BufferedOutputStream bos = new BufferedOutputStream(os);
byte[] in = new byte[1024];
int count = 0;
while ((count = bis.read(in)) != -1){
bos.write(in,0,count);
}
bis.close();
bos.close();
long end = System.currentTimeMillis();
System.out.println(end-start);
6.计算代码运行速度
long start = System.currentTimeMillis();
中间代码块
long end = System.currentTimeMillis();
System.out.println(end-start);
7. 位、字节、字符的区别
位(bit):是计算机 内部数据 储存的最小单位,11001100是一个八位二进制数。
字节(byte):是计算机中 数据处理 的基本单位,习惯上用大写 B 来表示,1B(byte,字节)= 8bit(位)
字符:是指计算机中使用的字母、数字、字和符号
ASCIIS码: 1个英文字母(不分大小写)= 1个字节的空间
1个中文汉字 = 2个字节的空间
1个ASCII码 = 一个字节
UTF-8编码:1个英文字符 = 1个字节
英文标点 = 1个字节
1个中文(含繁体) = 3个字节
中文标点 = 3个字节
Unicode编码:1个英文字符 = 2个字节
英文标点 = 2个字节
1个中文(含繁体) = 2个字节
中文标点 = 2个字节
我们使用的Android Studio 使用的是UTF-8编码,所以一个中文占了三个字节。
实际使用
实现如下功能:
图案解锁
文件写入 文件读取
第一次运行:
设置密码:6位
从终端输入一次密码
确认密码
设置过了 3次错误的机会
检查密码:相同提示解锁成功
不同就提示密码不一致 重新输入
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOperation {
public static final String PATH = "/Users/pengxiaodong/Desktop/day1/java/src/main/java/day8/Demo/pwd.txt";
String password;
public static final FileOperation instance = new FileOperation();
private FileOperation(){
try {
load();
}catch (IOException e){
System.out.println("io 异常");
}
}
public void load() throws IOException {
FileInputStream fis = new FileInputStream(PATH);
byte[] pwd = new byte[4];
int count = fis.read(pwd);
if (count == -1){
password = null;
}else{
password = new String(pwd);
}
fis.close();
}
public void save(String password){
try {
FileOutputStream fos = new FileOutputStream(PATH);
fos.write(password.getBytes());
fos.close();
} catch (IOException e){
System.out.println("io异常");
}
}
}
心得感悟
这几天学习的内容给我的感觉是思维难度没有前面几天学习的难度大了,但是要想掌握好也并不容易,比如文件这一块要好好理解才能熟练掌握。
往后学给我的一个感觉是也可以回过头来理解前面的知识,都有了更深刻的理解。
还是要把重心转移到学习上面,不能太放松自己。加油。