文件的相關(guān)操作
目的
掌握文件的相關(guān)操作,理解什么是“流”和清楚I/O流。
技術(shù)
1.創(chuàng)建一個(gè)文件
2.向文件寫入數(shù)據(jù)(字節(jié)流)
3.向文件寫入數(shù)據(jù)(字符流)
4.從文件讀取數(shù)據(jù)(字節(jié)流)
5.從文件讀取數(shù)據(jù)(字符流)
6.向文件存一個(gè)對(duì)象
7.從文件中讀取對(duì)象
8.將一個(gè)文件copy到另一個(gè)位置
9.用緩沖流copy文件
技術(shù)實(shí)現(xiàn)
1.創(chuàng)建文件
String path ="/Users/Administrator/Desktop/Android/firstExperience/java/src/main/java/day8";
File file = new File(path.concat("/1.txt"));
// 判斷是否存在
if (file.exists() == false){
// 不存在就創(chuàng)建
try {
file.createNewFile();
}catch (IOException e){
System.out.println("IO異常了");
}
2.向文件寫入數(shù)據(jù)(字節(jié)流)
// 1. 創(chuàng)建文件輸出流對(duì)象
FileOutputStream fos = new FileOutputStream(file);
// 2.調(diào)用write方法寫入
byte[] text = {'1','2','3','4'};
fos.write(text);
// 3.操作完畢需要關(guān)閉stream對(duì)象
fos.close();
3.向文件寫入數(shù)據(jù)(字符流)
FileWriter fw = new FileWriter(file);
char[] name = {'安','卓','開','發(fā)'};
fw.write(name);
fw.close();
4.從文件讀取數(shù)據(jù)(字節(jié)流)
FileInputStream fis = new FileInputStream(file);
byte[] name = new byte[8];
int count = fis.read(name);
fis.close();
System.out.println(count+""+ new String(name));
5.從文件讀取數(shù)據(jù)(字符流)
FileReader fr = new FileReader(file);
char[] book = new char[4];
count = fr.read(book);
fr.close();
System.out.println(count+""+new String(book));
6.向文件存一個(gè)對(duì)象
創(chuàng)建一個(gè)類并序列化
import java.io.Serializable;
public class Person implements Serializable {
public String name ;
public int age;
}
創(chuàng)建該類的一個(gè)對(duì)象并存入文件
Person xw = new Person();
xw.name = "小王";
xw.age = 20;
OutputStream os = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(xw);
oos.close();
7.從文件讀取對(duì)象
InputStream is = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(is);
Person xw = (Person) ois.readObject();
System.out.println(xw.name+" "+xw.age);
ois.close();
8.將一個(gè)文件copy到另一個(gè)文件
// 1.源文件的路徑
String sourcePath = "C:\\Users\\Administrator\\Desktop\\17981969_1359970249177.jpg";
// 2.目標(biāo)文件路徑
String desPath = "C:\\Users\\Administrator\\Desktop\\Android\\firstExperience\\java\\src\\main\\java\\day8\\1.jpg";
// 3. 圖片 字節(jié)
FileInputStream fis = new FileInputStream(sourcePath);
FileOutputStream fos = new FileOutputStream(desPath);
byte[] in = new byte[1024];
// while (true) {
// int count = fis.read(in);
// if(count != -1){
// // 讀取到內(nèi)容了
// // 將這一次讀取的內(nèi)容寫入到目標(biāo)文件
// fos.write(in,0,count);
// }else {
// break;
// }
// }
int count = 0;
while ((count = fis.read(in)) != -1){
fos.write(in,0,count);
}
fis.close();
fos.close();
9.用緩沖流copy文件
String sourcePath = "C:\\Users\\Administrator\\Desktop\\17981969_1359970249177.jpg";
String desPath = "C:\\Users\\Administrator\\Desktop\\Android\\firstExperience\\java\\src\\main\\java\\day8\\1.jpg";
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();
技術(shù)運(yùn)用
- 密碼解鎖demo
創(chuàng)建一個(gè)類實(shí)現(xiàn)文件操作部分
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOperation {
public static final String PATH = "C:\\Users\\Administrator\\Desktop\\Android\\firstExperience\\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異常");
}
}
}
密碼解鎖
import java.util.Scanner;
public class MyClass {
public static void main(String[] args){
boolean logined =false;
//判斷是否已經(jīng)登錄
String password = FileOperation.instance.password;
if (password != null){
logined = true;
}
//提示用戶操作
String alert;
if (logined) {
alert = "請(qǐng)輸入密碼";
} else {
alert = "請(qǐng)?jiān)O(shè)置密碼";
}
System.out.println(alert);
String first = null;
int wrongTime = 3;
while (wrongTime >= 0) {
//接收用戶輸入
Scanner scanner = new Scanner(System.in);
String inputPassword = scanner.next();
//判斷操作
if (logined) {
//已經(jīng)登陸過 直接比較
if (password.equals(inputPassword)) {
System.out.println("解鎖成功");
break;
} else {
if (wrongTime == 0)
{
System.out.println("密碼錯(cuò)誤次數(shù)過多,已退出!");
return;
}
System.out.println("解鎖失敗 請(qǐng)重新輸入");
wrongTime--;
}
}else{
//沒有登陸過 在設(shè)置密碼
//判斷是設(shè)置密碼的第一次還是第二次
if (first == null){
//第一次 保存第一次輸入的密碼
first = inputPassword;
System.out.println("請(qǐng)確認(rèn)密碼 ");
}else{
//第二次 比較兩次輸入的密碼是否相同
if (first.equals(inputPassword)){
System.out.println("設(shè)置密碼成功");
//保存設(shè)置的密碼
FileOperation.instance.save(first);
break;
}else{
System.out.println("兩次密碼不一致 請(qǐng)重新設(shè)置密碼:");
first = null;
wrongTime--;
}
}
}
scanner.nextLine();
}
}
}
運(yùn)行結(jié)果
QQ圖片20190814185120.png

QQ圖片20190814185223.png

QQ圖片20190814185304.png
心得
文件的學(xué)習(xí)雖然容易理解,但是內(nèi)容還是有點(diǎn)雜的。感覺說不定哪天我就會(huì)把字符流和字節(jié)流記混了。還需溫故而知新,這樣我才能記得牢。