java中的多執行緒Thread十九個例項帶你輕鬆學會
阿新 • • 發佈:2021-06-29
目錄
- IO流的概念(大綱):
IO流的概念(大綱):
1.InputStream和OutputStream的繼承關係圖
2.Reader和Writer的繼承關係圖
3.檔案專屬流(加※為重點掌握)
※FileInputStream(檔案位元組輸入流)例項:
package fileInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; /* FileInputStream常用的方法 int available(); 返回流當中剩餘的沒有讀到的位元組數量 long skip(long n); 跳過幾個位元組不讀 */ public class FileInputStreamTest05 { public static void main(String[] args) { // TODO Auto-generated method stub FileInputStream fis = null; try { //1.要讀取某一個檔案,先與這個檔案建立一個"輸入流" //String filePath = "afterglow.txt";//相對路徑,相對當前而言,在當前路徑下找 String filePath = "E:\\eclipse-workspace\\day26-IO\\InputStream\\fileInputStream\\afterglow.txt";//絕對路徑 fis = new FileInputStream(filePath); //讀 System.out.println(fis.available());//共有多少個位元組 byte[] bytes = new byte[fis.available()];//這種方式不太適合大檔案,byte[]不能太大 int readByte = fis.read(bytes); System.out.println(new String(bytes)); // readByte = fis.read(); System.out.println(fis.available());//剩餘多少個位元組沒有讀 //skip跳過幾個位元組不讀 //fis.skip(3); }catch(FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { //在finally語句塊中確保流一定關閉 if(fis!=null) {//避免空指標異常 //關閉流的前提是流不為空 try { fis.close(); }catch(IOException e) { e.printStackTrace(); } } } } }
※FileOutputStream(檔案位元組輸出流)例項:
package fileOutputStream;
/*
檔案位元組輸出流,負責寫
從記憶體到硬碟
*/
import java.io.*;
public class FileOutputStreamTest01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileOutputStream fos = null;
try {
//fos = new FileOutputStream("myFile.txt");//先清空在寫入
fos = new FileOutputStream("myFile.txt",true);//追加內容
byte[] bytes = {97,98,99,100};
//byte[] bytes = {'a','b'};
//fos.write(bytes);//ab
fos.write(bytes, 0, 1);//a
String str = "無為在歧路";
fos.write(str.getBytes());//字串轉換為字元陣列
//寫完之後要重新整理
fos.flush();
}catch(FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch(NullPointerException e) {
e.printStackTrace();
}
finally {
if(fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
如何利用FileInputStream和FileOutputStream實現單個檔案的copy?
package copy;
/*
使用FileInputStream和FileOutputStream完成檔案的拷貝
拷貝的過程應該是一邊讀一邊寫
使用以上的位元組流拷貝檔案的時候,檔案型別沒有限制
*/
import java.io.*;
public class Copy01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//建立一個輸入流物件
fis = new FileInputStream("afterglow.txt");
//建立一個輸出流物件
fos = new FileOutputStream("copy.txt");
//核心內容:一邊讀一邊寫
byte[] bytes = new byte[1024*1024];//一次最多拷貝1MB
int readCount = 0;
while((readCount=fis.read(bytes)) != -1){
fos.write(bytes,0,readCount);
}
//輸出流需要重新整理
fos.flush();
}catch(FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fis != null) {
try {
fis.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
我們成功的使用程式碼複製了檔案,當然此處可以複製任意的檔案型別,就不一一演示
FileReader(檔案字元輸入流)例項:
package fileReader;
/*
檔案字元輸入流,只能讀取普通文字
讀取文字內容時,比較方便快捷
*/
import java.io.*;
public class FileReaderTest01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileReader reader = null;
try {
reader = new FileReader("afterglow.txt");
//讀
char[] chars = new char[4];//一次讀取4個字元
int readCount = 0;
while((readCount = reader.read(chars)) != -1) {
System.out.print(new String(chars,0,readCount));
}
}catch(FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
FileWriter(檔案字元輸出流)例項:
package fileWriter;
import java.io.FileWriter;
import java.io.IOException;
/*
檔案字元輸出流,寫
只能輸出普通文字
*/
public class FileWriterTest01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileWriter out = null;
try {
//建立檔案字元輸出流物件
out = new FileWriter("FileWriter.txt");
//開始寫
char[] chars = {'我','是','種','花','家','|'};
out.write(chars);
out.write(chars,2,4);
out.write("\n");
out.write("我是亞托克斯,我是世界的終結者");
//重新整理
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
寫入完成,如下圖
如何利用FieReader和FileWriter實現單個檔案的copy?
package copy;
import java.io.*;
public class Copy02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileReader in = null;
FileWriter out = null;
try {
in = new FileReader("afterglow.txt");
out = new FileWriter("copy2.txt");
char[] chars = new char[1024];//1MB
int readCount = 0;
while((readCount = in.read(chars)) != -1) {
out.write(chars,0,readCount);
}
out.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
實現成功,如下圖(採用字元流複製只能複製純文字檔案)
關於檔案copy的總結:
其實使用FileInputStream和FileOutputStream複製檔案和FileReader和FileWriter複製檔案從本質上來說是相同的,因為過程如下圖所示
graph TD 建立讀取檔案和寫入檔案的物件 --> 建立一個數組用來存放讀取到的資料-->寫入新的檔案中-->最後關閉所有程序4.緩衝流(只講個例)
BuffteredReader(帶有緩衝區的字元輸入流)例項:
package bufferedReader;
/*
帶有緩衝區的字元輸入流
使用這個流的時候不需要自定義char陣列,或者說不需要自定義byte陣列,自帶緩衝
*/
import java.io.*;
public class BuffteredReaderTest01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//當一個流的構造方法中需要一個流的時候,被傳進來流叫做節點流
//外部負責包裝的這個流叫做包裝流/處理流
//就當前而言:FileReader是一個一個節點流;BufferedReader是一個包裝流/處理流
FileReader reader = null;
BufferedReader br = null;
try {
reader = new FileReader("afterglow.txt");
br = new BufferedReader(reader);
//readLine()不帶換行符
String s = null;
while((s = br.readLine()) != null) {
System.out.println(s);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//關閉流:只需要關閉包裝流,節點流自動關閉(原始碼)
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
BufferedWriter(帶有緩衝的位元組輸出流)例項:
(此處為了節省時間採用了throws丟擲異常,平時大家寫程式碼儘量使用try-catch語句塊)
package bufferedWriter;
/*
帶有緩衝的位元組輸出流
*/
import java.io.*;
public class BufferedWriterTest01 {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
BufferedWriter out = new BufferedWriter(new FileWriter("BufferedWriter.txt"));
out.write("NIHAODIQIU");
out.write("\n");
out.write("Yeah!");
//重新整理
out.flush();
out.close();
}
}
5.轉換流(只講個例)
InputStreamReader(位元組流轉換為字元流)例項:
package inputStreamReader;
/*
轉換流 InputStreamReader 將位元組流轉換為字元流
*/
import java.io.*;
public class InputStreamReaderTest01 {
//此處為了節省時間採用了throws丟擲異常,平時大家寫程式碼一定儘量使用try-catch語句塊
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
/*
* //位元組流 FileInputStream in = new FileInputStream("copy.txt"); //通過轉換流轉換
* InputStreamReader reader = new InputStreamReader(in);//in是節點流,reader是包裝流
* //字元流 BufferedReader br = new BufferedReader(reader);//該構造方法只能傳字元流,不能傳位元組流
*/
//改進:合併
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("copy.txt")));
String s = null;
while((s = br.readLine()) != null) {
System.out.println(s);
}
//關閉最外層的包裝流
br.close();
}
}
6.資料流
DataOutputSteam(資料位元組輸出流)例項:
package dataOutputStream;
/*
資料專屬流
這個流可以將資料連同資料的型別一併寫入檔案
注意:這個檔案不是普通的文字文件(記事本打不開)
*/
import java.io.*;
public class DataOutputSteamTest01 {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
//建立資料專屬的位元組輸出流
//此處建立的檔案可以是任何字尾,只有DataInputStream能開啟並讀取
DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.a"));
//定義資料
byte b = 97;
short s = 200;
int i = 300;
long l = 400L;
float f = 3.14f;
double d = 3.1415926;
char c = 97;
boolean flag = false;
//寫資料
dos.writeByte(b);
dos.writeShort(s);
dos.writeInt(i);
dos.writeLong(l);
dos.writeFloat(f);
dos.writeDouble(d);
dos.writeChar(c);
dos.writeBoolean(flag);
dos.flush();
dos.close();
}
}
DataInputStream(資料位元組輸入流)例項:
package dataInputStream;
/*
資料位元組輸入流
DataOutputStream寫的檔案只能使用DataInputStream去讀,並且讀的順序必須和寫的順序一致
*/
import java.io.*;
public class DataInputStreamTest01 {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
DataInputStream dis = new DataInputStream(new FileInputStream("data.a"));
byte b = dis.readByte();
short s = dis.readShort();
int i = dis.readInt();
long l = dis.readLong();
float f = dis.readFloat();
double d = dis.readDouble();
char c = dis.readChar();
boolean flag = dis.readBoolean();
System.out.println(b);
System.out.println(s);
System.out.println(i);
System.out.println(l);
System.out.println(f);
System.out.println(d);
System.out.println(c);
System.out.println(flag);
dis.close();
}
}
經檢查,讀取的資料與寫入的資料一致。
7.標準輸出流(只講個例)
※PrintStream(位元組輸出流)例項:
package printStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
/*
標準的位元組輸出流。預設輸出到控制檯
日誌框架的實現原理
*/
public class PrintStreamTest01 {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
PrintStream ps = System.out;
ps.print("你好,呆呆");
ps.print("你好,棉花");
ps.print("你好,李肖瑤");
//可以改變標準輸出流的輸出方向嗎?可以
//標準輸出流不在指向控制檯,指向了"PrintStream.txt"檔案
PrintStream printStream = new PrintStream(new FileOutputStream("PrintStream.txt"));
System.setOut(printStream);
System.out.println("aha my baby");
System.out.println("Second Line");
//標準輸出流不需要手動關閉
}
}
利用PrintStream實現的日誌工具:
大家在寫程式碼一定要仔細,不然你怎麼找都找不到那裡發生了錯誤(笑)
package logUtil;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
日誌工具
*/
public class LogUtil {
public static void main(String[] args) {
// TODO Auto-generated method stub
LogUtil.log("hello");
LogUtil.log("呼叫了System類的gc方法,建議啟動垃圾回收");
LogUtil.log("呼叫了UserService的doSome方法");
LogUtil.log("使用者嘗試登入,驗證失敗");
}
public static void log(String msg) {
try {
//指向日誌檔案
//PrintStream printStream = new PrintStream(new FileOutputStream("Log.txt"),true);
//第一次輸程式碼的時候粗心傳參時傳錯了位置,導致檢查了半個小時沒發現問題的結果,
//在後邊慢慢梳理髮現每一次傳參都會重新整理檔案,然後把兩個new分開寫了一遍後發現了問題是構造方法傳參的true放錯了位置,
//所以說大家一定要仔細啊,一個小問題都有可能引發一場災難
PrintStream printStream = new PrintStream(new FileOutputStream("Log.txt",true));//不清空
//改變輸出方向
System.setOut(printStream);
//當前時間
Date nowTime = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
String strTime = sdf.format(nowTime);
//輸出
System.out.println(strTime + " :" + msg);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
結果如下圖:
8.物件流
圖解序列化(ObjectOutputStream)和反序列化(ObjectInputStream):
由上圖我們可以得知:
ObjectOutputStream是用來序列化的,即把物件從記憶體拆分到硬碟中
ObjectInputStream是用來反序列化的,即把物件從硬碟恢復到記憶體中
在序列化的過程中可以使用transient關鍵字表示遊離的,不參與序列化
當然,我們需要知道參與序列化和反序列化必須實現Serializable介面,原始碼如下:
public interface Serializable {
}
該介面只是一個標誌性介面,java虛擬機器可以識別,並自動生成一個序列化版本號
這個版本號有什麼用呢?
我們要知道java語言中是採用什麼機制來區分類的?
1.類名
2.序列化版本號
如果類名相同的兩個類都實現了Serializable介面
優點:java虛擬機器技能區分開他們,因為他們的序列化版本號不同
缺點:後續不能修改程式碼(重新編譯會生成新的序列版本號)
結論:
建議給實現了Serialzable介面的類提供一個不變的序列版本號
這樣更新這個類就不會影響我們的反序列化
定義一個Student類用來存放資料:
package objectOutputStream;
import java.io.Serializable;
public class Student implements Serializable{
@Override
public String toString() {
return "Student [no=" + no + ", name=" + name + ", age=" + age + "]";
}
//固定一個序列版本號,這樣更新這個類就不會影響反序列化
private static final long serialVersionUID = 89821732739371298L;
//transient關鍵字表示遊離的,不參與序列化
//private transient int no;
private int no;
private String name;
private int age;//增加屬性也不會報錯
public Student() {
}
public Student(int no, String name) {
this.no = no;
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
※ObjectOutputStream例項:
package objectOutputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class ObjectOutputStreamTest01 {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Student s = new Student(1111,"zhaoxinyu");
//序列化
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ObjectOutputStream.txt"));
//序列化物件
oos.writeObject(s);
List<Student> studentList = new ArrayList<Student>();
studentList.add(new Student(1234,"zhangjing"));
studentList.add(new Student(5678,"LIUXUETING"));
studentList.add(new Student(43133,"YUEYANG"));
ObjectOutputStream oos2 = new ObjectOutputStream(new FileOutputStream("ObjectOutputStream2.txt"));
oos2.writeObject(studentList);
//重新整理和關閉
oos.flush();
oos.close();
oos2.flush();
oos2.close();
}
}
※ObjectInputStream例項:
package objectInputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.List;
import objectOutputStream.Student;
/*
反序列化
*/
public class ObjectInputStreamTest01 {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("ObjectOutputStream.txt"));
//開始反序列化,讀
Object obj = ois.readObject();
//反序列化返回一個學生物件,會呼叫學生的toString方法
System.out.println(obj);
ois.close();
//反序列化集合
ObjectInputStream ois2 = new ObjectInputStream(new FileInputStream("ObjectOutputStream2.txt"));
//開始反序列化,讀
Object obj2 = ois2.readObject();
//反序列化返回一個學生物件,會呼叫學生的toString方法
//System.out.println(obj2);
List<Student> studentList = (List<Student>)obj2;
for(Student student:studentList) {
System.out.println(student);
}
System.out.println(obj2 instanceof List);
ois2.close();
}
}
9.File類
File類中常用的方法例項1
boolean exists();判斷檔案存不存在
boolean createNewFile();以檔案形式新建
boolean mkdir();以目錄形式新建
boolean mkdirs();以多重目錄形式新建
String getParent();獲取檔案的父路徑,返回String
File getParentFile();獲取檔案的父路徑,返回File
String getAbsolutePath();獲取檔案的絕對路徑
package file;
import java.io.File;
import java.io.IOException;
public class FileTest01 {
public static void main(String[] args) throws IOException {
File f1 = new File("after.txt");
System.out.println(f1.exists());//false
File f2 = new File("afterglow.txt");
System.out.println(f2.exists());//true
// if(!f1.exists()) {
// f1.createNewFile();//新檔案
// }
if(!f1.exists()) {
f1.mkdir();//新目錄
}
File f3 = new File("a\\b\\c");
if(!f3.exists()) {
f3.mkdirs();//新多重目錄
}
//獲取檔案的父路徑
String parentPath = f2.getParent();
System.out.println(parentPath);//null,相對路徑的當前路徑下父路徑為空
parentPath = f3.getParent();
System.out.println(parentPath);//a\b,相對路徑下的當前路徑從根目錄開始
File parentFile = f3.getParentFile();//絕對路徑
System.out.println(parentFile.getAbsolutePath());
//E:\eclipse-workspace\day26-IO\a\b
System.out.println(f2.getAbsolutePath());
//E:\eclipse-workspace\day26-IO\afterglow.txt
}
}
File類中常用的方法例項2
boolean delete();刪除檔案
String getName();獲取檔名
boolean isDirectory();判斷是否是一個目錄
boolean isFile();判斷是否是一個檔案
long lastModified();最後一次修改時間(從1970.1.1開始的總毫秒數)
long length();獲取檔案大小
package file;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FileTest02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f1 = new File("E:\\eclipse-workspace\\day26-IO\\afterglow.txt");
System.out.println("檔名:"+f1.getName());
//判斷是否是一個目錄
System.out.println(f1.isDirectory());//false
//判斷是否是一個檔案
System.out.println(f1.isFile());//true
System.out.println(f1.lastModified());
//轉換成日期
Date time = new Date(f1.lastModified());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
System.out.println(sdf.format(time));//2021-04-06 15:47:00 561
System.out.println(f1.length());//9
}
}
File類中常用的方法例項3
File[] listFiles();獲取當前目錄下所有的子目錄
package file;
import java.io.File;
public class FileTest03 {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f1 = new File("E:\\eclipse-workspace\\day26-IO\\bin");
File[] files = f1.listFiles();
for(File file : files) {
System.out.println(file.getName());
// System.out.println(file.getAbsolutePath());
}
}
}
10.利用File類常用方法和FileInputStream和FileOutputStream實現資料夾的複製(包含資料夾下所有內容)
原始檔路徑:D:\java_copy\a
目標檔案路徑:D:\java_copy\copytext
文中提到的變數名含義:
yuan:原始檔
mubiao:目標檔案
length:原始檔路徑名長度
newFile:目標檔案中新增的檔案
sonmulu1:資料夾的目錄下所有檔案
資料夾複製的程式碼實現及思路:
思路:一層一層的複製
graph TD 要分層實現複製 -->那麼每開啟一層就要複製該層下的所有檔案 -->程式呼叫自身 -->使用遞迴graph TD 在每一層中複製所有檔案-->使用迭代-->判斷該檔案是資料夾還是檔案--資料夾-->使用mkdirs來建立多重目錄-->迴圈結束 判斷該檔案是資料夾還是檔案--檔案-->使用createNewFile來建立檔案-->迴圈結束遞迴是指程式經過不斷呼叫自身得出結果的方法
迭代是迴圈,是把本次迴圈的結果當做下一次迴圈的初值的方法
程式碼如下:
package copy;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy03 {
public static void main(String[] args) {
File yuan = new File("D:\\java_copy\\a");
int length = new String("D:\\java_copy\\a").length();//獲取原始檔路徑名長度
File mubiao = new File("D:\\java_copy\\copytext");
if(!mubiao.exists()) {
mubiao.mkdirs();
}
copy(yuan,mubiao,length);
}
public static void copy(File yuan,File mubiao,int length) {
//複製檔案
if(yuan.isFile()) {
FileInputStream f1 = null;
FileOutputStream f2 = null;
String str = (mubiao.getAbsolutePath()+yuan.getAbsolutePath().substring(length));
//System.out.println(str);//測試路徑是否正確
File newFile = new File(str);
try {
f1 = new FileInputStream(yuan);
if(!newFile.exists()) {
newFile.createNewFile();
}
f2 = new FileOutputStream(newFile);
byte[] bytes = new byte[1024*1024];//1MB
int readCount = 0;
while((readCount = f1.read(bytes)) != -1) {
f2.write(bytes,0,readCount);
}
f2.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(f1!=null) {
try {
f1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(f2!=null) {
try {
f2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return;
}
File[] sonmulu1 = yuan.listFiles();
for(int i = 0;i<sonmulu1.length;i++) {
//複製目錄
if(sonmulu1[i].isDirectory()) {
String str = (mubiao.getAbsolutePath()+sonmulu1[i].getAbsolutePath().substring(length));
//System.out.println(str);//測試路徑是否正確
File newFile = new File(str);
if(!newFile.exists()) {
newFile.mkdirs();
}
}
copy(sonmulu1[i],mubiao,length);
}
}
}
11.IO+Properties的聯合使用
程式碼示例:
package test;
import java.io.*;
import java.util.Properties;
public class IOPropertiesTest01 {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//新建輸入流物件
FileReader reader = new FileReader("userinfo");
//新建一個Map集合
Properties pro = new Properties();
//呼叫Properties物件的Load()方法將檔案中的資料載入到Map集合中
pro.load(reader);//檔案中的資料順著管道載入到Map集合中,其中等號左邊做key,右邊做value
//通過key來獲取value
String username = pro.getProperty("username");
System.out.println(username);
String password = pro.getProperty("password");
System.out.println(password);
String date = pro.getProperty("date");
System.out.println(date);
reader.close();
}
}