目的
學(xué)習(xí)操作Java文件漫玄,實(shí)現(xiàn)對其寫入與輸出數(shù)據(jù)坦冠,掌握其相關(guān)操作
File文件操作
文件操作無外乎就是讀和寫算吩,但是這里面的方法方式又有很多種。主要是從文件格式岁钓,文件的存儲介質(zhì)方面來看的升略。文件是一種持久存儲的方式,要查找一個文件甜紫,就要找到其相對應(yīng)的路徑降宅,而路徑又分為相對路徑和絕對路徑,絕對路徑就是指文件的完整路徑囚霸,相對路徑是部分路徑(當(dāng)前路徑下的子路徑)
Java文件的寫入與輸出腰根,可以用流這個概念來表示;而流的方向是參考自己的內(nèi)存空間拓型,輸出流是從內(nèi)存空間將數(shù)據(jù)寫到外部設(shè)備(磁盤\硬盤\光盤)额嘿,而輸入流是將外部數(shù)據(jù)寫到內(nèi)存中;其實(shí)流也是統(tǒng)一管理數(shù)據(jù)的寫入和讀取劣挫,輸出流是開發(fā)者只需要將內(nèi)存里面的數(shù)據(jù)寫到流里面册养,而輸入流是從流里面讀取數(shù)據(jù)
而流也分為兩種——字符流和字節(jié)流
字節(jié)流,顧名思義压固,是以字節(jié)為單位進(jìn)行IO操作球拦;而它最大的兩個父類是InputStream和OutputStream,這兩者都是抽象類帐我,需要通過多態(tài)坎炼,初始化具體實(shí)現(xiàn)的子類來進(jìn)行讀寫操作;實(shí)際操作的子類為
FileOutputStream/FileInputStream
ObjectOutputStream/ObjectInputStream
字符流拦键,顧名思義谣光,是以字符為單位進(jìn)行IO操作的,而一個字符為兩個字節(jié)芬为;其最大的兩個父類為Writer和Reader這兩個抽象類萄金,多通過FileWriter/FileReader這兩個具體實(shí)現(xiàn)的子類來進(jìn)行操作
如果是音頻蟀悦、圖片、歌曲氧敢,就使用字節(jié)流日戈;如果是文本的,使用字符流
注意:I/O對象不屬于對象福稳,需要自己關(guān)閉
具體操作
創(chuàng)建文件
// 創(chuàng)建文件 完整路徑
String path = "E:/JavaCourse/day1/src/main/java/day9";
// path/1.txt
File file = new File(path.concat("/1.txt"));
//判斷是否存在
if (file.exists() == false){
//不存在就創(chuàng)建
file.createNewFile();
}
向文件中寫入字節(jié)流
// 向文件寫入數(shù)據(jù)
// 1.創(chuàng)建文件輸出對象
FileOutputStream fos = new FileOutputStream(file);
// 2.調(diào)用write方法寫入
byte[] text = {'1','2','3','4'};
fos.write(text);
// 3.操作完畢需要關(guān)閉stream對象
fos.close();
讀取字節(jié)流
//讀取內(nèi)容
FileInputStream fis = new FileInputStream(file);
byte[] name = new byte[12];
int count = fis.read(name);
fis.close();
System.out.println(count+" "+new String(name));
向文件中寫入字符流
// 向文件寫入數(shù)據(jù)-字符流
FileWriter fw = new FileWriter(file);
char[] name = {'安','卓','開','發(fā)'};
fw.write(name);
fw.close();
讀取字符流
FileReader fr = new FileReader(file);
char[] book = new char[4];
count = fr.read(book);
fr.close();
System.out.println(count+""+new String(book));
文件中保存對象
向文件里面存一個對象涎拉,保存對象必須實(shí)現(xiàn)序列化即Serializable接口;如果對象內(nèi)部還有屬性變量是其他類的對象的圆,這個類也必須實(shí)現(xiàn)接口
import java.io.Serializable;
public class Person implements Serializable {
public String name;
public int age;
public Dog dog;
}
class Dog implements Serializable{
public String name;
}
Dog wc = new Dog();
wc.name = "旺財";
Person xw = new Person();
xw.name = "小王";
xw.age = 20;
xw.dog = wc;
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();
ois.close();
System.out.println(xw.name+" "+xw.age+" "+xw.dog.name);
將一個文件拷貝到另一個位置
// 1.源文件的路徑
String sourcePath = "C:\\Users\\管理員\\Desktop\\1.png";
// 2.目標(biāo)文件的路徑
String desPath = "E:\\JavaCourse\\day1\\src\\main\\java\\day9\\1.png";
// 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;
}
}
fis.close();
fos.close();
使用BufferedInputStream和BufferedOutputStream提高讀寫的速度
long start = System.currentTimeMillis();
String sourcePath = "C:\\Users\\管理員\\Desktop\\1.png";
String desPath = "E:\\JavaCourse\\day1\\src\\main\\java\\day9\\1.png";
// 輸入流
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);
密碼解鎖demo
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOperation {
//文件的路徑
public static final String PATH = "E:\\JavaCourse\\day1\\src\\main\\java\\Secert\\pas.txt";
//保存密碼
String password;
//單例
public static final FileOperation instance = new FileOperation();
//私有化構(gòu)造方法
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.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Secert {
public static void main(String[] args) throws IOException {
boolean logined = false;
// 判斷是否登錄
String password = FileOperation.instance.password;
if (password.length() > 0){
logined = true;
}
//提示用戶操作
String alert;
if (logined){
alert = "請輸入密碼:";
}else{
alert = "請?jiān)O(shè)置密碼:";
}
System.out.println(alert);
String firt = 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 {
System.out.println("解鎖失敗");
wrongTime--;
}
}else{
//沒有登錄過 設(shè)置密碼
//判斷是設(shè)置密碼的第一次還是第二次
if (firt == null){
//第一次 保存第一次輸入的密碼
firt = inputPassword;
System.out.println("請確認(rèn)密碼:");
}else{
//第二次 比較兩次輸入的密碼是否相同
if (firt.equals(inputPassword)){
System.out.println("設(shè)置密碼成功");
//保存設(shè)置的密碼
FileOperation.instance.save(firt);
break;
}else{
System.out.println("密碼不一致");
firt = null;
wrongTime--;
}
}
}
scanner.nextLine();
}
}
}
心得體會
對于新學(xué)習(xí)的東西越妈,還是十分不熟悉季俩,要多謝謝,才能熟練掌握