一,文件IO
package cn.itcast_01;
import java.io.File;
/*
* 我們要想實現IO的操作,就必須知道硬盤上文件的表現形式。
* 而Java就提供了一個類File供我們使用。
*
* File:文件和目錄(文件夾)路徑名的抽象表示形式
* 構造方法:
* File(String pathname):根據一個路徑得到File對象
* File(String parent, String child):根據一個目錄和一個子文件/目錄得到File對象
* File(File parent, String child):根據一個父File對象和一個子文件/目錄得到File對象
*/
public class FileDemo {
public static void main(String[] args) {
// File(String pathname):根據一個路徑得到File對象
// 把e:\\demo\\a.txt封裝成一個File對象
File file = new File("E:\\demo\\a.txt");
// File(String parent, String child):根據一個目錄和一個子文件/目錄得到File對象
File file2 = new File("E:\\demo", "a.txt");
// File(File parent, String child):根據一個父File對象和一個子文件/目錄得到File對象
File file3 = new File("e:\\demo");
File file4 = new File(file3, "a.txt");
// 以上三種方式其實效果一樣
}
}
===========================================================
package cn.itcast_02;
import java.io.File;
import java.io.IOException;
/*
*創建功能:
*public boolean createNewFile():創建文件 如果存在這樣的文件,就不創建了
*public boolean mkdir():創建文件夾 如果存在這樣的文件夾,就不創建了
*public boolean mkdirs():創建文件夾,如果父文件夾不存在,會幫你創建出來
*
*騎白馬的不一定是王子,可能是班長。
*注意:你到底要創建文件還是文件夾,你最清楚,方法不要調錯了。
*/
public class FileDemo {
public static void main(String[] args) throws IOException {
// 需求:我要在e盤目錄下創建一個文件夾demo
File file = new File("e:\\demo");
System.out.println("mkdir:" + file.mkdir());
// 需求:我要在e盤目錄demo下創建一個文件a.txt
File file2 = new File("e:\\demo\\a.txt");
System.out.println("createNewFile:" + file2.createNewFile());
// 需求:我要在e盤目錄test下創建一個文件b.txt
// Exception in thread "main" java.io.IOException: 系統找不到指定的路徑。
// 注意:要想在某個目錄下創建內容,該目錄首先必須存在。
// File file3 = new File("e:\\test\\b.txt");
// System.out.println("createNewFile:" + file3.createNewFile());
// 需求:我要在e盤目錄test下創建aaa目錄
// File file4 = new File("e:\\test\\aaa");
// System.out.println("mkdir:" + file4.mkdir());
// File file5 = new File("e:\\test");
// File file6 = new File("e:\\test\\aaa");
// System.out.println("mkdir:" + file5.mkdir());
// System.out.println("mkdir:" + file6.mkdir());
// 其實我們有更簡單的方法
File file7 = new File("e:\\aaa\\bbb\\ccc\\ddd");
System.out.println("mkdirs:" + file7.mkdirs());
// 看下面的這個東西:
File file8 = new File("e:\\liuyi\\a.txt");
System.out.println("mkdirs:" + file8.mkdirs());
}
}
============================================================
package cn.itcast_03;
import java.io.File;
import java.io.IOException;
/*
* 刪除功能:public boolean delete()
*
* 注意:
* A:如果你創建文件或者文件夾忘了寫盤符路徑,那么,默認在項目路徑下。
* B:Java中的刪除不走回收站。
* C:要刪除一個文件夾,請注意該文件夾內不能包含文件或者文件夾
*/
public class FileDemo {
public static void main(String[] args) throws IOException {
// 創建文件
// File file = new File("e:\\a.txt");
// System.out.println("createNewFile:" + file.createNewFile());
// 我不小心寫成這個樣子了
File file = new File("a.txt");
System.out.println("createNewFile:" + file.createNewFile());
// 繼續玩幾個
File file2 = new File("aaa\\bbb\\ccc");
System.out.println("mkdirs:" + file2.mkdirs());
// 刪除功能:我要刪除a.txt這個文件
File file3 = new File("a.txt");
System.out.println("delete:" + file3.delete());
// 刪除功能:我要刪除ccc這個文件夾
File file4 = new File("aaa\\bbb\\ccc");
System.out.println("delete:" + file4.delete());
// 刪除功能:我要刪除aaa文件夾
// File file5 = new File("aaa");
// System.out.println("delete:" + file5.delete());
File file6 = new File("aaa\\bbb");
File file7 = new File("aaa");
System.out.println("delete:" + file6.delete());
System.out.println("delete:" + file7.delete());
}
}
===========================================================================package cn.itcast_04;
import java.io.File;
/*
* 重命名功能:public boolean renameTo(File dest)
* 如果路徑名相同,就是改名。
* 如果路徑名不同,就是改名並剪切。
*
* 路徑以盤符開始:絕對路徑 c:\\a.txt
* 路徑不以盤符開始:相對路徑 a.txt
*/
public class FileDemo {
public static void main(String[] args) {
// 創建一個文件對象
// File file = new File("林青霞.jpg");
// // 需求:我要修改這個文件的名稱為"東方不敗.jpg"
// File newFile = new File("東方不敗.jpg");
// System.out.println("renameTo:" + file.renameTo(newFile));
File file2 = new File("東方不敗.jpg");
File newFile2 = new File("e:\\林青霞.jpg");
System.out.println("renameTo:" + file2.renameTo(newFile2));
}
}
==========================================================================package cn.itcast_05;
import java.io.File;
/*
* 判斷功能:
* public boolean isDirectory():判斷是否是目錄
* public boolean isFile():判斷是否是文件
* public boolean exists():判斷是否存在
* public boolean canRead():判斷是否可讀
* public boolean canWrite():判斷是否可寫
* public boolean isHidden():判斷是否隱藏
*/
public class FileDemo {
public static void main(String[] args) {
// 創建文件對象
File file = new File("a.txt");
System.out.println("isDirectory:" + file.isDirectory());// false
System.out.println("isFile:" + file.isFile());// true
System.out.println("exists:" + file.exists());// true
System.out.println("canRead:" + file.canRead());// true
System.out.println("canWrite:" + file.canWrite());// true
System.out.println("isHidden:" + file.isHidden());// false
}
}
========================================================
package cn.itcast_06;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
* 獲取功能:
* public String getAbsolutePath():獲取絕對路徑
* public String getPath():獲取相對路徑
* public String getName():獲取名稱
* public long length():獲取長度。字節數
* public long lastModified():獲取最后一次的修改時間,毫秒值
*/
public class FileDemo {
public static void main(String[] args) {
// 創建文件對象
File file = new File("demo\\test.txt");
System.out.println("getAbsolutePath:" + file.getAbsolutePath());
System.out.println("getPath:" + file.getPath());
System.out.println("getName:" + file.getName());
System.out.println("length:" + file.length());
System.out.println("lastModified:" + file.lastModified());
// 1416471971031
Date d = new Date(1416471971031L);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = sdf.format(d);
System.out.println(s);
}
}
===========================================================================package cn.itcast_07;
import java.io.File;
/*
* 獲取功能:
* public String[] list():獲取指定目錄下的所有文件或者文件夾的名稱數組
* public File[] listFiles():獲取指定目錄下的所有文件或者文件夾的File數組
*/
public class FileDemo {
public static void main(String[] args) {
// 指定一個目錄
File file = new File("e:\\");
// public String[] list():獲取指定目錄下的所有文件或者文件夾的名稱數組
String[] strArray = file.list();
for (String s : strArray) {
System.out.println(s);
}
System.out.println("------------");
// public File[] listFiles():獲取指定目錄下的所有文件或者文件夾的File數組
File[] fileArray = file.listFiles();
for (File f : fileArray) {
System.out.println(f.getName());
}
}
}
test
新建文件夾
新建文本文檔.txt
file:1470298076394
E:\demo\test
E:\demo\新建文件夾
E:\demo\新建文本文檔.txt
$RECYCLE.BIN
animal.jar
big data
big data.txt
job English
software
System Volume Information
tu.jpg
VOA
xunleiyingying
大數據學習筆記
訊傑工作
------------
$RECYCLE.BIN
animal.jar
big data
big data.txt
job English
software
System Volume Information
tu.jpg
VOA
xunleiyingying
大數據學習筆記
訊傑工作
====================================
package cn.itcast_08;
import java.io.File;
/*
* 判斷E盤目錄下是否有后綴名為.jpg的文件,如果有,就輸出此文件名稱
*
* 分析:
* A:封裝e判斷目錄
* B:獲取該目錄下所有文件或者文件夾的File數組
* C:遍歷該File數組,得到每一個File對象,然后判斷
* D:是否是文件
* 是:繼續判斷是否以.jpg結尾
* 是:就輸出該文件名稱
* 否:不搭理它
* 否:不搭理它
*/
public class FileDemo {
public static void main(String[] args) {
// 封裝e判斷目錄
File file = new File("e:\\");
// 獲取該目錄下所有文件或者文件夾的File數組
File[] fileArray = file.listFiles();
// 遍歷該File數組,得到每一個File對象,然后判斷
for (File f : fileArray) {
// 是否是文件
if (f.isFile()) {
// 繼續判斷是否以.jpg結尾
if (f.getName().endsWith(".jpg")) {
// 就輸出該文件名稱
System.out.println(f.getName());
}
}
}
}
}
==========================================================
package cn.itcast_08;
import java.io.File;
import java.io.FilenameFilter;
/*
* 判斷E盤目錄下是否有后綴名為.jpg的文件,如果有,就輸出此文件名稱
* A:先獲取所有的,然后遍歷的時候,依次判斷,如果滿足條件就輸出。
* B:獲取的時候就已經是滿足條件的了,然后輸出即可。
*
* 要想實現這個效果,就必須學習一個接口:文件名稱過濾器
* public String[] list(FilenameFilter filter)
* public File[] listFiles(FilenameFilter filter)
*/
public class FileDemo2 {
public static void main(String[] args) {
// 封裝e判斷目錄
File file = new File("e:\\");
// 獲取該目錄下所有文件或者文件夾的String數組
// public String[] list(FilenameFilter filter)
String[] strArray = file.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
// return false;
// return true;
// 通過這個測試,我們就知道了,到底把這個文件或者文件夾的名稱加不加到數組中,取決於這里的返回值是true還是false
// 所以,這個的true或者false應該是我們通過某種判斷得到的
// System.out.println(dir + "---" + name);
// File file = new File(dir, name);
// // System.out.println(file);
// boolean flag = file.isFile();
// boolean flag2 = name.endsWith(".jpg");
// return flag && flag2;
return new File(dir, name).isFile() && name.endsWith(".jpg");
}
});
// 遍歷
for (String s : strArray) {
System.out.println(s);
}
}
}
=======================================================
package cn.itcast_09;
import java.io.File;
/*
* 需求:把E:\評書\三國演義下面的視頻名稱修改為
* 00?_介紹.avi
*
* 思路:
* A:封裝目錄
* B:獲取該目錄下所有的文件的File數組
* C:遍歷該File數組,得到每一個File對象
* D:拼接一個新的名稱,然后重命名即可。
*/
public class FileDemo {
public static void main(String[] args) {
// 封裝目錄
File srcFolder = new File("E:\\評書\\三國演義");
// 獲取該目錄下所有的文件的File數組
File[] fileArray = srcFolder.listFiles();
// 遍歷該File數組,得到每一個File對象
for (File file : fileArray) {
// System.out.println(file);
// E:\評書\三國演義\三國演義_001_[評書網-今天很高興,明天就IO了]_桃園三結義.avi
// 改后:E:\評書\三國演義\001_桃園三結義.avi
String name = file.getName(); // 三國演義_001_[評書網-今天很高興,明天就IO了]_桃園三結義.avi
int index = name.indexOf("_");
String numberString = name.substring(index + 1, index + 4);
// System.out.println(numberString);
// int startIndex = name.lastIndexOf('_');
// int endIndex = name.lastIndexOf('.');
// String nameString = name.substring(startIndex + 1, endIndex);
// System.out.println(nameString);
int endIndex = name.lastIndexOf('_');
String nameString = name.substring(endIndex);
String newName = numberString.concat(nameString); // 001_桃園三結義.avi
// System.out.println(newName);
File newFile = new File(srcFolder, newName); // E:\\評書\\三國演義\\001_桃園三結義.avi
// 重命名即可
file.renameTo(newFile);
}
}
}
二.
package cn.itcast_01;
/*
* 遞歸:方法定義中調用方法本身的現象
*
* 方法的嵌套調用,這不是遞歸。
* Math.max(Math.max(a,b),c);
*
* public void show(int n) {
* if(n <= 0) {
* System.exit(0);
* }
* System.out.println(n);
* show(--n);
* }
*
* 注意事項:
* A:遞歸一定要有出口,否則就是死遞歸
* B:遞歸的次數不能太多,否則就內存溢出
* C:構造方法不能遞歸使用
*
* 舉例:
* A:從前有座山,山里有座廟,廟里有個老和尚和小和尚,老和尚在給小和尚講故事,故事是:
* 從前有座山,山里有座廟,廟里有個老和尚和小和尚,老和尚在給小和尚講故事,故事是:
* 從前有座山,山里有座廟,廟里有個老和尚和小和尚,老和尚在給小和尚講故事,故事是:
* 從前有座山,山里有座廟,廟里有個老和尚和小和尚,老和尚在給小和尚講故事,故事是:
* ...
* 廟掛了,或者山崩了
* B:學編程 -- 高薪就業 -- 掙錢 -- 娶媳婦 -- 生娃娃 -- 放羊 -- 掙學費
* 學編程 -- 高薪就業 -- 掙錢 -- 娶媳婦 -- 生娃娃 -- 放羊 -- 掙學費
* 學編程 -- 高薪就業 -- 掙錢 -- 娶媳婦 -- 生娃娃 -- 放羊 -- 掙學費
* 學編程 -- 高薪就業 -- 掙錢 -- 娶媳婦 -- 生娃娃 -- 放羊 -- 掙學費
* ...
* 娶不到媳婦或者生不了娃娃
*/
public class DiGuiDemo {
// public DiGuiDemo() {
// DiGuiDemo();
// }
}
=====================================================
package cn.itcast_02;
/*
* 需求:請用代碼實現求5的階乘。
* 下面的知識要知道:
* 5! = 1*2*3*4*5
* 5! = 5*4!
*
* 有幾種方案實現呢?
* A:循環實現
* B:遞歸實現
* a:做遞歸要寫一個方法
* b:出口條件
* c:規律
*/
public class DiGuiDemo {
public static void main(String[] args) {
int jc = 1;
for (int x = 2; x <= 5; x++) {
jc *= x;
}
System.out.println("5的階乘是:" + jc);
System.out.println("5的階乘是:"+jieCheng(5));
}
/*
* 做遞歸要寫一個方法:
* 返回值類型:int
* 參數列表:int n
* 出口條件:
* if(n == 1) {return 1;}
* 規律:
* if(n != 1) {return n*方法名(n-1);}
*/
public static int jieCheng(int n){
if(n==1){
return 1;
}else {
return n*jieCheng(n-1);
}
}
}
===============================================
package cn.itcast_02;
/*
* 有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔子,假如兔子都不死,問第二十個月的兔子對數為多少?
* 分析:我們要想辦法找規律
* 兔子對數
* 第一個月: 1
* 第二個月: 1
* 第三個月: 2
* 第四個月: 3
* 第五個月: 5
* 第六個月: 8
* ...
*
* 由此可見兔子對象的數據是:
* 1,1,2,3,5,8...
* 規則:
* A:從第三項開始,每一項是前兩項之和
* B:而且說明前兩項是已知的
*
* 如何實現這個程序呢?
* A:數組實現
* B:變量的變化實現
* C:遞歸實現
*
* 假如相鄰的兩個月的兔子對數是a,b
* 第一個相鄰的數據:a=1,b=1
* 第二個相鄰的數據:a=1,b=2
* 第三個相鄰的數據:a=2,b=3
* 第四個相鄰的數據:a=3,b=5
* 看到了:下一次的a是以前的b,下一次是以前的a+b
*/
public class DiGuiDemo2 {
public static void main(String[] args) {
// 定義一個數組
int[] arr = new int[20];
arr[0] = 1;
arr[1] = 1;
// arr[2] = arr[0] + arr[1];
// arr[3] = arr[1] + arr[2];
// ...
for (int x = 2; x < arr.length; x++) {
arr[x] = arr[x - 2] + arr[x - 1];
}
System.out.println(arr[19]);// 6765
System.out.println("----------------");
int a = 1;
int b = 1;
for (int x = 0; x < 18; x++) {
// 臨時變量存儲上一次的a
int temp = a;
a = b;
b = temp + b;
}
System.out.println(b);
System.out.println("----------------");
System.out.println(fib(20));
}
/*
* 方法: 返回值類型:int 參數列表:int n 出口條件: 第一個月是1,第二個月是1 規律: 從第三個月開始,每一個月是前兩個月之和
*/
public static int fib(int n) {
if (n == 1 || n == 2) {
return 1;
} else {
return fib(n - 1) + fib(n - 2);
}
}
}
6765
===============
6765
===============
6765
=========================================================
package cn.itcast_03;
import java.io.File;
/*
* 需求:遞歸刪除帶內容的目錄
*
* 目錄我已經給定:demo
*
* 分析:
* A:封裝目錄
* B:獲取該目錄下的所有文件或者文件夾的File數組
* C:遍歷該File數組,得到每一個File對象
* D:判斷該File對象是否是文件夾
* 是:回到B
* 否:就刪除
*/
public class FileDeleteDemo {
public static void main(String[] args) {
// 封裝目錄
File srcFolder = new File("demo");
// 遞歸實現
deleteFolder(srcFolder);
}
private static void deleteFolder(File srcFolder) {
// 獲取該目錄下的所有文件或者文件夾的File數組
File[] fileArray = srcFolder.listFiles();
if (fileArray != null) {
// 遍歷該File數組,得到每一個File對象
for (File file : fileArray) {
// 判斷該File對象是否是文件夾
if (file.isDirectory()) {
deleteFolder(file);
} else {
System.out.println(file.getName() + "---" + file.delete());
}
}
System.out.println(srcFolder.getName() + "---" + srcFolder.delete());
}
}
==========================================================
package cn.itcast_03;
import java.io.File;
/*
* 需求:請大家把E:\JavaSE目錄下所有的java結尾的文件的絕對路徑給輸出在控制台。
*
* 分析:
* A:封裝目錄
* B:獲取該目錄下所有的文件或者文件夾的File數組
* C:遍歷該File數組,得到每一個File對象
* D:判斷該File對象是否是文件夾
* 是:回到B
* 否:繼續判斷是否以.java結尾
* 是:就輸出該文件的絕對路徑
* 否:不搭理它
*/
public class FilePathDemo {
public static void main(String[] args) {
// 封裝目錄
File srcFolder = new File("E:\\JavaSE");
// 遞歸功能實現
getAllJavaFilePaths(srcFolder);
}
private static void getAllJavaFilePaths(File srcFolder) {
// 獲取該目錄下所有的文件或者文件夾的File數組
File[] fileArray = srcFolder.listFiles();
// 遍歷該File數組,得到每一個File對象
for (File file : fileArray) {
// 判斷該File對象是否是文件夾
if (file.isDirectory()) {
getAllJavaFilePaths(file);
} else {
// 繼續判斷是否以.java結尾
if (file.getName().endsWith(".java")) {
// 就輸出該文件的絕對路徑
System.out.println(file.getAbsolutePath());
}
}
}
}
}
}
=======================================================================
package cn.itcast_01;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* IO流的分類:
* 流向:
* 輸入流 讀取數據
* 輸出流 寫出數據
* 數據類型:
* 字節流
* 字節輸入流 讀取數據 InputStream
* 字節輸出流 寫出數據 OutputStream
* 字符流
* 字符輸入流 讀取數據 Reader
* 字符輸出流 寫出數據 Writer
*
* 注意:一般我們在探討IO流的時候,如果沒有明確說明按哪種分類來說,默認情況下是按照數據類型來分的。
*
* 需求:我要往一個文本文件中輸入一句話:"hello,io"
*
* 分析:
* A:這個操作最好是采用字符流來做,但是呢,字符流是在字節流之后才出現的,所以,今天我先講解字節流如何操作。
* B:由於我是要往文件中寫一句話,所以我們要采用字節輸出流。
*
* 通過上面的分析后我們知道要使用:OutputStream
* 但是通過查看API,我們發現該流對象是一個抽象類,不能實例化。
* 所以,我們要找一個具體的子類。
* 而我們要找的子類是什么名字的呢?這個時候,很簡單,我們回想一下,我們是不是要往文件中寫東西。
* 文件是哪個單詞:File
* 然后用的是字節輸出流,聯起來就是:FileOutputStream
* 注意:每種基類的子類都是以父類名作為后綴名。
* XxxOutputStream
* XxxInputStream
* XxxReader
* XxxWriter
* 查看FileOutputStream的構造方法:
* FileOutputStream(File file)
* FileOutputStream(String name)
*
* 字節輸出流操作步驟:
* A:創建字節輸出流對象
* B:寫數據
* C:釋放資源
*/
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
// 創建字節輸出流對象
// FileOutputStream(File file)
// File file = new File("fos.txt");
// FileOutputStream fos = new FileOutputStream(file);
// FileOutputStream(String name)
FileOutputStream fos = new FileOutputStream("fos.txt");
/*
* 創建字節輸出流對象了做了幾件事情:
* A:調用系統功能去創建文件
* B:創建fos對象
* C:把fos對象指向這個文件
*/
//寫數據
fos.write("hello,IO".getBytes());
fos.write("java".getBytes());
//釋放資源
//關閉此文件輸出流並釋放與此流有關的所有系統資源。
fos.close();
/*
* 為什么一定要close()呢?
* A:讓流對象變成垃圾,這樣就可以被垃圾回收器回收了
* B:通知系統去釋放跟該文件相關的資源
*/
//java.io.IOException: Stream Closed
//fos.write("java".getBytes());
}
}
===========================================================================package cn.itcast_01;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 字節輸出流操作步驟:
* A:創建字節輸出流對象
* B:調用write()方法
* C:釋放資源
*
* public void write(int b):寫一 個字節
* public void write(byte[] b):寫一個字節數組
* public void write(byte[] b,int off,int len):寫一個字節數組的一部分
*/
public class FileOutputStreamDemo2 {
public static void main(String[] args) throws IOException {
// 創建字節輸出流對象
// OutputStream os = new FileOutputStream("fos2.txt"); // 多態
FileOutputStream fos = new FileOutputStream("fos2.txt");
// 調用write()方法
//fos.write(97); / /97 -- 底層二進制數據 -- 通過記事本打開 -- 找97對應的字符值 -- a
// fos.write(57);
// fos.write(55);
//public void write(byte[] b):寫一個字節數組
byte[] bys={97,98,99,100,101};
fos.write(bys);
//public void write(byte[] b,int off,int len):寫一個字節數組的一部分
fos.write(bys,1,3);
//釋放資源
fos.close();
}
}
================================================================
package cn.itcast_01;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 如何實現數據的換行?
* 為什么現在沒有換行呢?因為你值寫了字節數據,並沒有寫入換行符號。
* 如何實現呢?寫入換行符號即可唄。
* 剛才我們看到了有寫文本文件打開是可以的,通過windows自帶的那個不行,為什么呢?
* 因為不同的系統針對不同的換行符號識別是不一樣的?
* windows:\r\n
* linux:\n
* Mac:\r
* 而一些常見的個高級記事本,是可以識別任意換行符號的。
*
* 如何實現數據的追加寫入?
* 用構造方法帶第二個參數是true的情況即可
*/
public class FileOutputStreamDemo3 {
public static void main(String[] args) throws IOException {
// 創建字節輸出流對象
// FileOutputStream fos = new FileOutputStream("fos3.txt");
// 創建一個向具有指定 name 的文件中寫入數據的輸出文件流。如果第二個參數為 true,則將字節寫入文件末尾處,而不是寫入文件開始處。
FileOutputStream fos = new FileOutputStream("fos3.txt", true);
// 寫數據
for (int x = 0; x < 10; x++) {
fos.write(("hello" + x).getBytes());
fos.write("\r\n".getBytes());
}
// 釋放資源
fos.close();
}
}
==================================================================
package cn.itcast_01;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 加入異常處理的字節輸出流操作
*/
public class FileOutputStreamDemo4 {
public static void main(String[] args) {
// 分開做異常處理
// FileOutputStream fos = null;
// try {
// fos = new FileOutputStream("fos4.txt");
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
//
// try {
// fos.write("java".getBytes());
// } catch (IOException e) {
// e.printStackTrace();
// }
//
// try {
// fos.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// 一起做異常處理
// try {
// FileOutputStream fos = new FileOutputStream("fos4.txt");
// fos.write("java".getBytes());
// fos.close();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
// 改進版
// 為了在finally里面能夠看到該對象就必須定義到外面,為了訪問不出問題,還必須給初始化值
FileOutputStream fos = null;
try {
// fos = new FileOutputStream("z:\\fos4.txt");
fos = new FileOutputStream("fos4.txt");
fos.write("java".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 如果fos不是null,才需要close()
if (fos != null) {
// 為了保證close()一定會執行,就放到這里了
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
===========================================================
package cn.itcast_02;
import java.io.FileInputStream;
import java.io.IOException;
/*
* 字節輸入流操作步驟:
* A:創建字節輸入流對象
* B:調用read()方法讀取數據,並把數據顯示在控制台
* C:釋放資源
*
* 讀取數據的方式:
* A:int read():一次讀取一個字節
* B:int read(byte[] b):一次讀取一個字節數組
*/
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
// FileInputStream(String name)
// FileInputStream fis = new FileInputStream("fis.txt");
FileInputStream fis = new FileInputStream("FileOutputStreamDemo.java");
// // 調用read()方法讀取數據,並把數據顯示在控制台
// // 第一次讀取
// int by = fis.read();
// System.out.println(by);
// System.out.println((char) by);
//
// // 第二次讀取
// by = fis.read();
// System.out.println(by);
// System.out.println((char) by);
//
// // 第三次讀取
// by = fis.read();
// System.out.println(by);
// System.out.println((char) by);
// // 我們發現代碼的重復度很高,所以我們要用循環改進
// // 而用循環,最麻煩的事情是如何控制循環判斷條件呢?
// // 第四次讀取
// by = fis.read();
// System.out.println(by);
// // 第五次讀取
// by = fis.read();
// System.out.println(by);
// //通過測試,我們知道如果你讀取的數據是-1,就說明已經讀取到文件的末尾了
// 用循環改進
// int by = fis.read();
// while (by != -1) {
// System.out.print((char) by);
// by = fis.read();
// }
// 最終版代碼
int by = 0;
// 讀取,賦值,判斷
while ((by = fis.read()) != -1) {
System.out.print((char) by);
}
// 釋放資源
fis.close();
}
}
===============================================
package cn.itcast_02;
import java.io.FileInputStream;
import java.io.IOException;
/*
* 一次讀取一個字節數組:int read(byte[] b)
* 返回值其實是實際讀取的字節個數。
*/
public class FileInputStreamDemo2 {
public static void main(String[] args) throws IOException {
// 創建字節輸入流對象
// FileInputStream fis = new FileInputStream("fis2.txt");
FileInputStream fis = new FileInputStream("FileOutputStreamDemo.java");
// 讀取數據
// 定義一個字節數組
// 第一次讀取
// byte[] bys = new byte[5];
// int len = fis.read(bys);
// // System.out.println(len);
// // System.out.println(new String(bys));
// // System.out.println(new String(bys, 0, len));
// System.out.print(new String(bys, 0, len));
//
// // 第二次讀取
// len = fis.read(bys);
// // System.out.println(len);
// // System.out.println(new String(bys));
// // System.out.println(new String(bys, 0, len));
// System.out.print(new String(bys, 0, len));
//
// // 第三次讀取
// len = fis.read(bys);
// // System.out.println(len);
// // System.out.println(new String(bys));
// // System.out.println(new String(bys, 0, len));
// System.out.print(new String(bys, 0, len));
//
// // 第四次讀取
// len = fis.read(bys);
// // System.out.println(len);
// // System.out.println(new String(bys, 0, len));
// System.out.print(new String(bys, 0, len));
// // 代碼重復了,用循環改進
// // 但是,我不知道結束條件
// // len = fis.read(bys);
// // System.out.println(len);
// // len = fis.read(bys);
// // System.out.println(len);
// // 如果讀取到的實際長度是-1,就說明沒有數據了
// byte[] bys = new byte[115]; // 0
// int len = 0;
// while ((len = fis.read(bys)) != -1) {
// System.out.print(new String(bys, 0, len));
// // System.out.print(new String(bys)); //千萬要帶上len的使用
// }
// 最終版代碼
// 數組的長度一般是1024或者1024的整數倍
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
System.out.print(new String(bys, 0, len));
}
// 釋放資源
fis.close();
}
==========================================
package cn.itcast_03;
import java.util.Arrays;
/*
* 計算機是如何識別什么時候該把兩個字節轉換為一個中文呢?
* 在計算機中中文的存儲分兩個字節:
* 第一個字節肯定是負數。
* 第二個字節常見的是負數,可能有正數。但是沒影響。
*/
public class StringDemo {
public static void main(String[] args) {
// String s = "abcde";
// // [97, 98, 99, 100, 101]
String s = "我愛你中國";
// [-50, -46, -80, -82, -60, -29, -42, -48, -71, -6]
byte[] bys = s.getBytes();
System.out.println(Arrays.toString(bys));
}
}
=======================================
package cn.itcast_03;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 需求:把e:\\哥有老婆.mp4復制到當前項目目錄下的copy.mp4中
*
* 數據源:
* e:\\哥有老婆.mp4--讀取數據--FileInputStream
* 目的地:
* copy.mp4--寫出數據--FileOutputStream
*/
public class CopyMp4Demo {
public static void main(String[] args) throws IOException {
// 封裝數據源
FileInputStream fis = new FileInputStream("e:\\哥有老婆.mp4");
// 封裝目的地
FileOutputStream fos = new FileOutputStream("copy.mp4");
// 復制數據
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
// 釋放資源
fos.close();
fis.close();
}
}
=========================================================
package cn.itcast_03;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 需求:把c盤下的a.txt的內容復制到d盤下的b.txt中
*
* 數據源:
* c:\\a.txt -- 讀取數據-- FileInputStream
* 目的地:
* d:\\b.txt -- 寫出數據 -- FileOutputStream
*/
public class CopyFileDemo2 {
public static void main(String[] args) throws IOException {
// 封裝數據源
FileInputStream fis = new FileInputStream("c:\\a.txt");
// 封裝目的地
FileOutputStream fos = new FileOutputStream("d:\\b.txt");
// 復制數據
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
// 釋放資源
fos.close();
fis.close();
}
}
=====================================================
package cn.itcast_03;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 復制文本文件。
*
* 數據源:從哪里來
* a.txt -- 讀取數據 -- FileInputStream
*
* 目的地:到哪里去
* b.txt -- 寫數據 -- FileOutputStream
*
* java.io.FileNotFoundException: a.txt (系統找不到指定的文件。)
*
* 這一次復制中文沒有出現任何問題,為什么呢?
* 上一次我們出現問題的原因在於我們每次獲取到一個字節數據,就把該字節數據轉換為了字符數據,然后輸出到控制台。
* 而這一次呢?確實通過IO流讀取數據,寫到文本文件,你讀取一個字節,我就寫入一個字節,你沒有做任何的轉換。
* 它會自己做轉換。
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
// 封裝數據源
FileInputStream fis = new FileInputStream("a.txt");
// 封裝目的地
FileOutputStream fos = new FileOutputStream("b.txt");
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
// 釋放資源(先關誰都行)
fos.close();
fis.close();
}
}
=====================================================================
package cn.itcast_04;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 需求:把c:\\a.txt內容復制到d:\\b.txt中
*
* 數據源:
* c:\\a.txt -- 讀取數據 -- FileInputStream
* 目的地:
* d:\\b.txt -- 寫出數據 -- FileOutputStream
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
// 封裝數據源
FileInputStream fis = new FileInputStream("c:\\a.txt");
FileOutputStream fos = new FileOutputStream("d:\\b.txt");
// 復制數據
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
將指定 byte 數組中從偏移量 off 開始的 len 個字節寫入此文件輸出流。 |
}
// 釋放資源
fos.close();
fis.close();
}
}
====================================================
package cn.itcast_04;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 需求:把e:\\哥有老婆.mp4復制到當前項目目錄下的copy.mp4中
*
* 數據源:
* e:\\哥有老婆.mp4--讀取數據--FileInputStream
* 目的地:
* copy.mp4--寫出數據--FileOutputStream
*/
public class CopyMp4Demo {
public static void main(String[] args) throws IOException {
// 封裝數據源
FileInputStream fis = new FileInputStream("e:\\哥有老婆.mp4");
// 封裝目的地
FileOutputStream fos = new FileOutputStream("copy.mp4");
// 復制數據
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
// 釋放資源
fos.close();
fis.close();
}
}
======================================================
package cn.itcast_05;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
/*
* 注意:雖然我們有兩種方式可以讀取,但是,請注意,這兩種方式針對同一個對象在一個代碼中只能使用一個。
*/
public class BufferedInputStreamDemo {
public static void main(String[] args) throws IOException {
// BufferedInputStream(InputStream in)
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
"bos.txt"));
// 讀取數據
// int by = 0;
// while ((by = bis.read()) != -1) {
// System.out.print((char) by);
// }
// System.out.println("---------");
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
System.out.print(new String(bys, 0, len));
}
// 釋放資源
bis.close();
}
}
===========================================================================package cn.itcast_05;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 通過定義數組的方式確實比以前一次讀取一個字節的方式快很多,所以,看來有一個緩沖區還是非常好的。
* 既然是這樣的話,那么,java開始在設計的時候,它也考慮到了這個問題,就專門提供了帶緩沖區的字節類。
* 這種類被稱為:緩沖區類(高效類)
* 寫數據:BufferedOutputStream
* 讀數據:BufferedInputStream
*
* 構造方法可以指定緩沖區的大小,但是我們一般用不上,因為默認緩沖區大小就足夠了。
*
* 為什么不傳遞一個具體的文件或者文件路徑,而是傳遞一個OutputStream對象呢?
* 原因很簡單,
字節緩沖區流僅僅提供緩沖區,為高效而設計的。但是呢,真正的讀寫操作還得靠基本的流對象實現。
*/
public class BufferedOutputStreamDemo {
public static void main(String[] args) throws IOException {
// BufferedOutputStream(OutputStream out)
創建一個新的緩沖輸出流,以將數據寫入指定的底層輸出流。 |
// FileOutputStream fos = new FileOutputStream("bos.txt");
// BufferedOutputStream bos = new BufferedOutputStream(fos);
// 簡單寫法
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("bos.txt"));
// 寫數據
bos.write("hello".getBytes());
// 釋放資源
bos.close();
}
}
==========================================================================
package cn.itcast_06;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 需求:把e:\\哥有老婆.mp4復制到當前項目目錄下的copy.mp4中
*
* 字節流四種方式復制文件:
* 基本字節流一次讀寫一個字節: 共耗時:117235毫秒
* 基本字節流一次讀寫一個字節數組: 共耗時:156毫秒
* 高效字節流一次讀寫一個字節: 共耗時:1141毫秒
* 高效字節流一次讀寫一個字節數組: 共耗時:47毫秒
*/
public class CopyMp4Demo {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
// method1("e:\\哥有老婆.mp4", "copy1.mp4");
// method2("e:\\哥有老婆.mp4", "copy2.mp4");
// method3("e:\\哥有老婆.mp4", "copy3.mp4");
method4("e:\\哥有老婆.mp4", "copy4.mp4");
long end = System.currentTimeMillis();
System.out.println("共耗時:" + (end - start) + "毫秒");
}
// 高效字節流一次讀寫一個字節數組:
public static void method4(String srcString, String destString)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcString));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destString));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) { //從輸入流中讀取數據的下一個字節。返回
0 到 255 范圍內的 int 字節值。如果因為已經到達流末尾而沒有可用的字節,則返回值 -1。
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
// 高效字節流一次讀寫一個字節:
public static void method3(String srcString, String destString)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcString));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destString));
int by = 0;
while ((by = bis.read()) != -1) {
bos.write(by);
}
bos.close();
bis.close();
}
// 基本字節流一次讀寫一個字節數組
public static void method2(String srcString, String destString)
throws IOException {
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(destString);
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
// 基本字節流一次讀寫一個字節
public static void method1(String srcString, String destString)
throws IOException {
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(destString);
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fos.close();
fis.close();
}
}
二.
package cn.itcast_01;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
/*
* String(byte[] bytes, String charsetName):通過指定的字符集
解碼字節數組
*
byte[] getBytes(String charsetName):使用指定的字符集合把字符串編碼為字節數組
*
* 編碼:把看得懂的變成看不懂的
* String -- byte[]
*
* 解碼:把看不懂的變成看得懂的
* byte[] -- String
*
* 舉例:諜戰片(發電報,接電報)
*
* 碼表:小本子
* 字符 數值
*
* 要發送一段文字:
* 今天晚上在老地方見
*
* 發送端:今 -- 數值 -- 二進制 -- 發出去
* 接收端:接收 -- 二進制 -- 十進制 -- 數值 -- 字符 -- 今
*
* 今天晚上在老地方見
*
* 編碼問題簡單,只要編碼解碼的格式是一致的。
*/
public class StringDemo {
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "你好";
// String -- byte[] 使用指定的字符集合把字符串
編碼為字節數組
byte[] bys = s.getBytes(); // [-60, -29, -70, -61]
// byte[] bys = s.getBytes("GBK");// [-60, -29, -70, -61]
// byte[] bys = s.getBytes("UTF-8");// [-28, -67, -96, -27, -91, -67]
System.out.println(Arrays.toString(bys));//
返回指定數組內容的字符串表示形式。字符串表示形式由數組的元素列表組成,括在方括號("[]")中。相鄰元素用字符 ", "(逗號加空格)分隔。這些元素通過 String.valueOf(char) 轉換為字符串。如果 a 為 null,則返回 "null"。
// byte[] -- String通過指定的字符集
解碼字節數組
String ss = new String(bys); // 你好
// String ss = new String(bys, "GBK"); // 你好
// String ss = new String(bys, "UTF-8"); // ???
System.out.println(ss);
}
}
==================================================
package cn.itcast_01;
import java.io.FileInputStream;
import java.io.IOException;
/*
* 字節流讀取中文可能出現的小問題:
*/
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
// 創建字節輸入流對象
FileInputStream fis = new FileInputStream("a.txt");
// 讀取數據
// int by = 0;
// while ((by = fis.read()) != -1) {
// System.out.print((char) by);
// }
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
System.out.print(new String(bys, 0, len));
}
// 釋放資源
fis.close();
}
}
===========================
package cn.itcast_02;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/*
* InputStreamReader(InputStream is):用默認的編碼讀取數據
* InputStreamReader(InputStream is,String charsetName):用指定的編碼讀取數據
*/
InputStreamReader 是字節流通向字符流的橋梁:它使用指定的 charset 讀取字節並將其解碼為字符。它使用的字符集可以由名稱指定或顯式給定,或者可以接受平台默認的字符集
public class InputStreamReaderDemo {
public static void main(String[] args) throws IOException {
// 創建對象
// InputStreamReader isr = new InputStreamReader(new FileInputStream(
// "osw.txt"));
// InputStreamReader isr = new InputStreamReader(new FileInputStream(
// "osw.txt"), "GBK");
InputStreamReader isr = new InputStreamReader(new FileInputStream(
"osw.txt"), "UTF-8");
// 讀取數據
// 一次讀取一個字符
int ch = 0;
while ((ch = isr.read()) != -1) {
System.out.print((char) ch);
}
// 釋放資源
isr.close();
}
}
=========================================
package cn.itcast_03;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/*
* InputStreamReader的方法:
* int read():一次讀取一個字符
* int read(char[] chs):一次讀取一個字符數組
*/
public class InputStreamReaderDemo {
public static void main(String[] args) throws IOException {
// 創建對象
InputStreamReader isr = new InputStreamReader(new FileInputStream(
"StringDemo.java"));
// 一次讀取一個字符
// int ch = 0;
// while ((ch = isr.read()) != -1) {
// System.out.print((char) ch);
// }
// 一次讀取一個字符數組
char[] chs = new char[1024];
int len = 0;
while ((len = isr.read(chs)) != -1) {
System.out.print(new String(chs, 0, len));
}
// 釋放資源
isr.close();
}
}
=========================================================
package cn.itcast_03;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
/*
* OutputStreamWriter的方法:
* public void write(int c):寫一個字符
* public void write(char[] cbuf):寫一個字符數組
* public void write(char[] cbuf,int off,int len):寫一個字符數組的一部分
* public void write(String str):寫一個字符串
* public void write(String str,int off,int len):寫一個字符串的一部分
*
OutputStreamWriter 是字符流通向字節流的橋梁:可使用指定的 charset 將要寫入流中的字符編碼成字節。它使用的字符集可以由名稱指定或顯式給定,否則將接受平台默認的字符集
* 面試題:close()和flush()的區別?
* A:close()關閉流對象,但是先刷新一次緩沖區。關閉之后,流對象不可以繼續再使用了。
* B:flush()僅僅刷新緩沖區,刷新之后,流對象還可以繼續使用。
*/
public class OutputStreamWriterDemo {
public static void main(String[] args) throws IOException {
// 創建對象
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
"osw2.txt"));
// 寫數據
// public void write(int c):寫一個字符
// osw.write('a');
// osw.write(97);
// 為什么數據沒有進去呢?
// 原因是:字符 = 2字節
// 文件中數據存儲的基本單位是字節。
// void flush()
// public void write(char[] cbuf):寫一個字符數組
// char[] chs = {'a','b','c','d','e'};
// osw.write(chs);
// public void write(char[] cbuf,int off,int len):寫一個字符數組的一部分
// osw.write(chs,1,3);
// public void write(String str):寫一個字符串
// osw.write("我愛林青霞");
// public void write(String str,int off,int len):寫一個字符串的一部分
osw.write("我愛林青霞", 2, 3);
// 刷新緩沖區
osw.flush();
// osw.write("我愛林青霞", 2, 3);
// 釋放資源
osw.close();
// java.io.IOException: Stream closed
// osw.write("我愛林青霞", 2, 3);//報錯
}
}
=========================================================
package cn.itcast_04;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/*
* 需求:把當前項目目錄下的a.txt內容復制到當前項目目錄下的b.txt中
*
* 數據源:
* a.txt -- 讀取數據 -- 字符轉換流 -- InputStreamReader
* 目的地:
* b.txt -- 寫出數據 -- 字符轉換流 -- OutputStreamWriter
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
// 封裝數據源
InputStreamReader isr = new InputStreamReader(new FileInputStream(
"a.txt"));//
讀取字節並將其解碼為字符
// 封裝目的地
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
"b.txt"));//
將要寫入流中的字符編碼成字節
// 讀寫數據
// 方式1
// int ch = 0;
// while ((ch = isr.read()) != -1) {
// osw.write(ch);
// }
// 方式2
char[] chs = new char[1024];
int len = 0;
while ((len = isr.read(chs)) != -1) {
osw.write(chs, 0, len);
// osw.flush();
}
// 釋放資源
osw.close();
isr.close();
}
}
===================================================================
public class
FileReader
extends
InputStreamReader
用來讀取字符文件的便捷類。此類的構造方法假定默認字符編碼和默認字節緩沖區大小都是適當的。要自己指定這些值,可以先在 FileInputStream 上構造一個 InputStreamReader。
FileReader 用於讀取字符流。要讀取原始字節流,請考慮使用 FileInputStream。
package cn.itcast_04;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
* 需求:把c:\\a.txt內容復制到d:\\b.txt中
*
* 數據源:
* c:\\a.txt -- FileReader
* 目的地:
* d:\\b.txt -- FileWriter
*/
public class CopyFileDemo3 {
public static void main(String[] args) throws IOException {
// 封裝數據源
FileReader fr = new FileReader("c:\\a.txt");
// 封裝目的地
FileWriter fw = new FileWriter("d:\\b.txt");
// 讀寫數據
// int ch = 0;
int ch;
while ((ch = fr.read()) != -1) {
fw.write(ch);
}
//釋放資源
fw.close();
fr.close();
}
}
=================================================
package cn.itcast_05;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
BufferedWriter將文本寫入字符輸出流,緩沖各個字符,從而提供單個字符、數組和字符串的高效寫入。可以指定緩沖區的大小,或者接受默認的大小。在大多數情況下,默認值就足夠大了。
* 字符緩沖流的特殊方法:
* BufferedWriter:
* public void newLine():根據系統來決定換行符
* BufferedReader:
* public String readLine():一次讀取一行數據
* 包含該行內容的字符串,不包含任何行終止符,如果已到達流末尾,則返回 null
*/
public class BufferedDemo {
public static void main(String[] args) throws IOException {
// write();
read();
}
private static void read() throws IOException {
// 創建字符緩沖輸入流對象
BufferedReader br = new BufferedReader(new FileReader("bw2.txt"));
// public String readLine():一次讀取一行數據
// String line = br.readLine();
// System.out.println(line);
// line = br.readLine();
// System.out.println(line);
// 最終版代碼
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
//釋放資源
br.close();
}
private static void write() throws IOException {
// 創建字符緩沖輸出流對象
BufferedWriter bw = new BufferedWriter(new FileWriter("bw2.txt"));
for (int x = 0; x < 10; x++) {
bw.write("hello" + x);
// bw.write("\r\n");
bw.newLine();
bw.flush();
}
bw.close();
}
}
=================================================
package cn.itcast_05;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/*
* BufferedReader
*
從字符輸入流中讀取文本,緩沖各個字符,從而實現字符、數組和行的高效讀取。
* 可以指定緩沖區的大小,或者可使用默認的大小。大多數情況下,默認值就足夠大了。
*
* BufferedReader(Reader in)
*/
public class BufferedReaderDemo {
public static void main(String[] args) throws IOException {
// 創建字符緩沖輸入流對象
BufferedReader br = new BufferedReader(new FileReader("bw.txt"));
// 方式1
// int ch = 0;
// while ((ch = br.read()) != -1) {
// System.out.print((char) ch);
// }
// 方式2
char[] chs = new char[1024];
int len = 0;
while ((len = br.read(chs)) != -1) {
System.out.print(new String(chs, 0, len));
}
// 釋放資源
br.close();
}
}
==================================================================
package cn.itcast_05;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/*
* 字符流為了高效讀寫,也提供了對應的字符緩沖流。
* BufferedWriter:字符緩沖輸出流
* BufferedReader:字符緩沖輸入流
*
* BufferedWriter:字符緩沖輸出流
* 將文本寫入字符輸出流,緩沖各個字符,從而提供單個字符、數組和字符串的高效寫入。
* 可以指定緩沖區的大小,或者接受默認的大小。在大多數情況下,默認值就足夠大了。
*/
public class BufferedWriterDemo {
public static void main(String[] args) throws IOException {
// BufferedWriter(Writer out)
// BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
// new FileOutputStream("bw.txt")));
BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));
bw.write("hello");
bw.write("world");
bw.write("java");
bw.flush();
bw.close();
}
}
==============================================
package cn.itcast_06;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
* 需求:把當前項目目錄下的a.txt內容復制到當前項目目錄下的b.txt中
*
* 數據源:
* a.txt -- 讀取數據 -- 字符轉換流 -- InputStreamReader -- FileReader -- BufferedReader
* 目的地:
* b.txt -- 寫出數據 -- 字符轉換流 -- OutputStreamWriter -- FileWriter -- BufferedWriter
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
// 封裝數據源
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
// 封裝目的地
BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));
// 兩種方式其中的一種一次讀寫一個字符數組
char[] chs = new char[1024];
int len = 0;
while ((len = br.read(chs)) != -1) {
bw.write(chs, 0, len);
bw.flush();
}
// 釋放資源
bw.close();
br.close();
}
}
==========================================
package cn.itcast_06;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
* 需求:把當前項目目錄下的a.txt內容復制到當前項目目錄下的b.txt中
*
* 數據源:
* a.txt -- 讀取數據 -- 字符轉換流 -- InputStreamReader -- FileReader -- BufferedReader
* 目的地:
* b.txt -- 寫出數據 -- 字符轉換流 -- OutputStreamWriter -- FileWriter -- BufferedWriter
*/
public class CopyFileDemo2 {
public static void main(String[] args) throws IOException {
// 封裝數據源
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
// 封裝目的地
BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));
// 讀寫數據
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
// 釋放資源
bw.close();
br.close();
}
}
======================================================
package cn.itcast_01;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
* 復制文本文件
*
* 分析:
* 復制數據,如果我們知道用記事本打開並能夠讀懂,就用字符流,否則用字節流。
* 通過該原理,我們知道我們應該采用字符流更方便一些。
* 而字符流有5種方式,所以做這個題目我們有5種方式。推薦掌握第5種。
* 數據源:
* c:\\a.txt -- FileReader -- BufferdReader
* 目的地:
* d:\\b.txt -- FileWriter -- BufferedWriter
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
String srcString = "c:\\a.txt";
String destString = "d:\\b.txt";
// method1(srcString, destString);
// method2(srcString, destString);
// method3(srcString, destString);
// method4(srcString, destString);
method5(srcString, destString);
}
// 字符緩沖流一次讀寫一個字符串
private static void method5(String srcString, String destString)
throws IOException {
BufferedReader br = new BufferedReader(new FileReader(srcString));
BufferedWriter bw = new BufferedWriter(new FileWriter(destString));
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
// 字符緩沖流一次讀寫一個字符數組
private static void method4(String srcString, String destString)
throws IOException {
BufferedReader br = new BufferedReader(new FileReader(srcString));
BufferedWriter bw = new BufferedWriter(new FileWriter(destString));
char[] chs = new char[1024];
int len = 0;
while ((len = br.read(chs)) != -1) {
bw.write(chs, 0, len);
}
bw.close();
br.close();
}
// 字符緩沖流一次讀寫一個字符
private static void method3(String srcString, String destString)
throws IOException {
BufferedReader br = new BufferedReader(new FileReader(srcString));
BufferedWriter bw = new BufferedWriter(new FileWriter(destString));
int ch = 0;
while ((ch = br.read()) != -1) {
bw.write(ch);
}
bw.close();
br.close();
}
// 基本字符流一次讀寫一個字符數組
private static void method2(String srcString, String destString)
throws IOException {
FileReader fr = new FileReader(srcString);
FileWriter fw = new FileWriter(destString);
char[] chs = new char[1024];
int len = 0;
while ((len = fr.read(chs)) != -1) {
fw.write(chs, 0, len);
}
fw.close();
fr.close();
}
// 基本字符流一次讀寫一個字符
private static void method1(String srcString, String destString)
throws IOException {
FileReader fr = new FileReader(srcString);
FileWriter fw = new FileWriter(destString);
int ch = 0;
while ((ch = fr.read()) != -1) {
fw.write(ch);
}
fw.close();
fr.close();
}
}
===================================================
package cn.itcast_01;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 復制圖片
*
* 分析:
* 復制數據,如果我們知道用記事本打開並能夠讀懂,就用字符流,否則用字節流。
* 通過該原理,我們知道我們應該采用字節流。
* 而字節流有4種方式,所以做這個題目我們有4種方式。推薦掌握第4種。
*
* 數據源:
* c:\\a.jpg -- FileInputStream -- BufferedInputStream
* 目的地:
* d:\\b.jpg -- FileOutputStream -- BufferedOutputStream
*/
public class CopyImageDemo {
public static void main(String[] args) throws IOException {
// 使用字符串作為路徑
// String srcString = "c:\\a.jpg";
// String destString = "d:\\b.jpg";
// 使用File對象做為參數
File srcFile = new File("c:\\a.jpg");
File destFile = new File("d:\\b.jpg");
// method1(srcFile, destFile);
// method2(srcFile, destFile);
// method3(srcFile, destFile);
method4(srcFile, destFile);
}
// 字節緩沖流一次讀寫一個字節數組
private static void method4(File srcFile, File destFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcFile));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destFile));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
// 字節緩沖流一次讀寫一個字節
private static void method3(File srcFile, File destFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcFile));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destFile));
int by = 0;
while ((by = bis.read()) != -1) {
bos.write(by);
}
bos.close();
bis.close();
}
// 基本字節流一次讀寫一個字節數組
private static void method2(File srcFile, File destFile) throws IOException {
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
// 基本字節流一次讀寫一個字節
private static void method1(File srcFile, File destFile) throws IOException {
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fos.close();
fis.close();
}
}
===========================================================================
package cn.itcast_02;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
/*
* 需求:我有一個文本文件中存儲了幾個名稱,請大家寫一個程序實現隨機獲取一個人的名字。
*
* 分析:
* A:把文本文件中的數據存儲到集合中
* B:隨機產生一個索引
* C:根據該索引獲取一個值
*/
public class GetName {
public static void main(String[] args) throws IOException {
// 把文本文件中的數據存儲到集合中
BufferedReader br = new BufferedReader(new FileReader("b.txt"));
ArrayList<String> array = new ArrayList<String>();
String line = null;
while ((line = br.readLine()) != null) {
array.add(line);
}
br.close();
// 隨機產生一個索引
Random r = new Random();
int index = r.nextInt(array.size());
// 根據該索引獲取一個值
String name = array.get(index);
System.out.println("該幸運者是:" + name);
}
}
==========================================
package cn.itcast_02;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/*
* 需求:從文本文件中讀取數據(每一行為一個字符串數據)到集合中,並遍歷集合
*
* 分析:
* 通過題目的意思我們可以知道如下的一些內容,
* 數據源是一個文本文件。
* 目的地是一個集合。
* 而且元素是字符串。
*
* 數據源:
* b.txt -- FileReader -- BufferedReader
* 目的地:
* ArrayList<String>
*/
public class FileToArrayListDemo {
public static void main(String[] args) throws IOException {
// 封裝數據源
BufferedReader br = new BufferedReader(new FileReader("b.txt"));
// 封裝目的地(創建集合對象)
ArrayList<String> array = new ArrayList<String>();
// 讀取數據存儲到集合中
String line = null;
while ((line = br.readLine()) != null) {
array.add(line);
}
// 釋放資源
br.close();
// 遍歷集合
for (String s : array) {
System.out.println(s);
}
}
}
=============================================================
package cn.itcast_02;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
/*
* 需求:把ArrayList集合中的字符串數據存儲到文本文件
*
* 分析:
* 通過題目的意思我們可以知道如下的一些內容,
* ArrayList集合里存儲的是字符串。
* 遍歷ArrayList集合,把數據獲取到。
* 然后存儲到文本文件中。
* 文本文件說明使用字符流。
*
* 數據源:
* ArrayList<String> -- 遍歷得到每一個字符串數據
* 目的地:
* a.txt -- FileWriter -- BufferedWriter
*/
public class ArrayListToFileDemo {
public static void main(String[] args) throws IOException {
// 封裝數據與(創建集合對象)
ArrayList<String> array = new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("java");
// 封裝目的地
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
// 遍歷集合
for (String s : array) {
// 寫數據
bw.write(s);
bw.newLine();
bw.flush();
}
// 釋放資源
bw.close();
}
}
========================================
package cn.itcast_03;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 需求:復制單極文件夾
*
* 數據源:e:\\demo
* 目的地:e:\\test
*
* 分析:
* A:封裝目錄
* B:獲取該目錄下的所有文本的File數組
* C:遍歷該File數組,得到每一個File對象
* D:把該File進行復制
*/
public class CopyFolderDemo {
public static void main(String[] args) throws IOException {
目錄 封裝
File srcFolder = new File("e:\\demo");
// 封裝目的地
File destFolder = new File("e:\\test");
// 如果目的地文件夾不存在,就創建
if (!destFolder.exists()) {
destFolder.mkdir();
}
// 獲取該目錄下的所有文本的File數組
File[] fileArray = srcFolder.listFiles();
// 遍歷該File數組,得到每一個File對象
for (File file : fileArray) {
// System.out.println(file);
// 數據源:e:\\demo\\e.mp3
// 目的地:e:\\test\\e.mp3
String name = file.getName(); // e.mp3
File newFile = new File(destFolder, name); // e:\\test\\e.mp3
copyFile(file, newFile);
}
}
private static void copyFile(File file, File newFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
file));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(newFile));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}
==============================================================
package cn.itcast_04;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
/*
* 需求:復制指定目錄下的指定文件,並修改后綴名。
* 指定的文件是:.java文件。
* 指定的后綴名是:.jad
* 指定的目錄是:jad
*
* 數據源:e:\\java\\A.java
* 目的地:e:\\jad\\A.jad
*
* 分析:
* A:封裝目錄
* B:獲取該目錄下的java文件的File數組
* C:遍歷該File數組,得到每一個File對象
* D:把該File進行復制
* E:在目的地目錄下改名
*/
public class CopyFolderDemo {
public static void main(String[] args) throws IOException {
// 封裝目錄
File srcFolder = new File("e:\\java");
// 封裝目的地
File destFolder = new File("e:\\jad");
// 如果目的地目錄不存在,就創建
if (!destFolder.exists()) {
destFolder.mkdir();
}
// 獲取該目錄下的java文件的File數組
File[] fileArray = srcFolder.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return new File(dir, name).isFile() && name.endsWith(".java");
}
});
// 遍歷該File數組,得到每一個File對象
for (File file : fileArray) {
// System.out.println(file);
// 數據源:e:\java\DataTypeDemo.java
// 目的地:e:\\jad\DataTypeDemo.java
String name = file.getName();
File newFile = new File(destFolder, name);
copyFile(file, newFile);
}
// 在目的地目錄下改名
File[] destFileArray = destFolder.listFiles();
for (File destFile : destFileArray) {
// System.out.println(destFile);
// e:\jad\DataTypeDemo.java
// e:\\jad\\DataTypeDemo.jad
String name =destFile.getName(); //DataTypeDemo.java
String newName = name.replace(".java", ".jad");//DataTypeDemo.jad
File newFile = new File(destFolder,newName);
destFile.renameTo(newFile);
}
}
private static void copyFile(File file, File newFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
file));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(newFile));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}
=================================
package cn.itcast_05;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 需求:復制多極文件夾
*
* 數據源:E:\JavaSE\day21\code\demos
* 目的地:E:\\
*
* 分析:
* A:封裝數據源File
* B:封裝目的地File
* C:判斷該File是文件夾還是文件
* a:是文件夾
* 就在目的地目錄下創建該文件夾
* 獲取該File對象下的所有文件或者文件夾File對象
* 遍歷得到每一個File對象
* 回到C
* b:是文件
* 就復制(字節流)
*/
public class CopyFoldersDemo {
public static void main(String[] args) throws IOException {
// 封裝數據源File
File srcFile = new File("E:\\JavaSE\\day21\\code\\demos");
// 封裝目的地File
File destFile = new File("E:\\");
// 復制文件夾的功能
copyFolder(srcFile, destFile);
}
private static void copyFolder(File srcFile, File destFile)
throws IOException {
// 判斷該File是文件夾還是文件
if (srcFile.isDirectory()) {
// 文件夾
File newFolder = new File(destFile, srcFile.getName());
newFolder.mkdir();
// 獲取該File對象下的所有文件或者文件夾File對象
File[] fileArray = srcFile.listFiles();
for (File file : fileArray) {
copyFolder(file, newFolder);
}
} else {
// 文件
File newFile = new File(destFile, srcFile.getName());
copyFile(srcFile, newFile);
}
}
private static void copyFile(File srcFile, File newFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcFile));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(newFile));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}
===============================
package cn.itcast_06;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;
/*
* 鍵盤錄入5個學生信息(姓名,語文成績,數學成績,英語成績),按照總分從高到低存入文本文件
*
* 分析:
* A:創建學生類
* B:創建集合對象
* TreeSet<Student>
* C:鍵盤錄入學生信息存儲到集合
* D:遍歷集合,把數據寫到文本文件
*/
public class StudentDemo {
public static void main(String[] args) throws IOException {
// 創建集合對象
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s2.getSum() - s1.getSum();
int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;
int num3 = num2 == 0 ? s1.getMath() - s2.getMath() : num2;
int num4 = num3 == 0 ? s1.getEnglish() - s2.getEnglish() : num3;
int num5 = num4 == 0 ? s1.getName().compareTo(s2.getName())
: num4;
return num5;
}
});
// 鍵盤錄入學生信息存儲到集合
for (int x = 1; x <= 5; x++) {
Scanner sc = new Scanner(System.in);
System.out.println("請錄入第" + x + "個的學習信息");
System.out.println("姓名:");
String name = sc.nextLine();
System.out.println("語文成績:");
int chinese = sc.nextInt();
System.out.println("數學成績:");
int math = sc.nextInt();
System.out.println("英語成績:");
int english = sc.nextInt();
// 創建學生對象
Student s = new Student();
s.setName(name);
s.setChinese(chinese);
s.setMath(math);
s.setEnglish(english);
// 把學生信息添加到集合
ts.add(s);
}
// 遍歷集合,把數據寫到文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter("students.txt"));
bw.write("學生信息如下:");
bw.newLine();
bw.flush();
bw.write("姓名,語文成績,數學成績,英語成績");
bw.newLine();
bw.flush();
for (Student s : ts) {
StringBuilder sb = new StringBuilder();
sb.append(s.getName()).append(",").append(s.getChinese())
.append(",").append(s.getMath()).append(",")
.append(s.getEnglish());
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
// 釋放資源
bw.close();
System.out.println("學習信息存儲完畢");
}
}
package cn.itcast_06;
public class Student {
// 姓名
private String name;
// 語文成績
private int chinese;
// 數學成績
private int math;
// 英語成績
private int english;
public Student() {
super();
}
public Student(String name, int chinese, int math, int english) {
super();
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getSum() {
return this.chinese + this.math + this.english;
}
}
============================================================
package cn.itcast_07;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
/*
* 已知s.txt文件中有這樣的一個字符串:“hcexfgijkamdnoqrzstuvwybpl”
* 請編寫程序讀取數據內容,把數據排序后寫入ss.txt中。
*
* 分析:
* A:把s.txt這個文件給做出來
* B:讀取該文件的內容,存儲到一個字符串中
* C:把字符串轉換為字符數組
* D:對字符數組進行排序
* E:把排序后的字符數組轉換為字符串
* F:把字符串再次寫入ss.txt中
*/
public class StringDemo {
public static void main(String[] args) throws IOException {
// 讀取該文件的內容,存儲到一個字符串中
BufferedReader br = new BufferedReader(new FileReader("s.txt"));
String line = br.readLine();
br.close();
// 把字符串轉換為字符數組
char[] chs = line.toCharArray();
// 對字符數組進行排序
Arrays.sort(chs);
// 把排序后的字符數組轉換為字符串
String s = new String(chs);
// 把字符串再次寫入ss.txt中
BufferedWriter bw = new BufferedWriter(new FileWriter("ss.txt"));
bw.write(s);
bw.newLine();
bw.flush();
bw.close();
}
}
===========================
package cn.itcast_08;
import java.io.FileReader;
import java.io.IOException;
/*
* 測試MyBufferedReader的時候,你就把它當作BufferedReader一樣的使用
*/
public class MyBufferedReaderDemo {
public static void main(String[] args) throws IOException {
MyBufferedReader mbr = new MyBufferedReader(new FileReader("my.txt"));
String line = null;
while ((line = mbr.readLine()) != null) {
System.out.println(line);
}
mbr.close();
// System.out.println('\r' + 0); // 13
// System.out.println('\n' + 0);// 10
}
}
package cn.itcast_08;
import java.io.IOException;
import java.io.Reader;
/*
* 用Reader模擬BufferedReader的readLine()功能
*
* readLine():一次讀取一行,根據換行符判斷是否結束,只返回內容,不返回換行符
*/
public class MyBufferedReader {
private Reader r;
public MyBufferedReader(Reader r) {
this.r = r;
}
/*
* 思考:寫一個方法,返回值是一個字符串。
*/
public String readLine() throws IOException {
/*
* 我要返回一個字符串,我該怎么辦呢? 我們必須去看看r對象能夠讀取什么東西呢? 兩個讀取方法,一次讀取一個字符或者一次讀取一個字符數組
* 那么,我們要返回一個字符串,用哪個方法比較好呢? 我們很容易想到字符數組比較好,但是問題來了,就是這個數組的長度是多長呢?
* 根本就沒有辦法定義數組的長度,你定義多長都不合適。 所以,只能選擇一次讀取一個字符。
* 但是呢,這種方式的時候,我們再讀取下一個字符的時候,上一個字符就丟失了 所以,我們又應該定義一個臨時存儲空間把讀取過的字符給存儲起來。
* 這個用誰比較和是呢?數組,集合,字符串緩沖區三個可供選擇。
* 經過簡單的分析,最終選擇使用字符串緩沖區對象。並且使用的是StringBuilder
*/
StringBuilder sb = new StringBuilder();
一個可變的字符序列。此類提供一個與 StringBuffer 兼容的 API,但不保證同步。該類被設計用作 StringBuffer 的一個簡易替換,用在字符串緩沖區被單個線程使用的時候(這種情況很普遍)。如果可能,建議優先采用該類,因為在大多數實現中,它比 StringBuffer 要快
// 做這個讀取最麻煩的是判斷結束,但是在結束之前應該是一直讀取,直到-1
/*
hello
world
java
104101108108111
119111114108100
1069711897
*/
int ch = 0;
while ((ch = r.read()) != -1) { //104,101,108,108,111
if (ch == '\r') {
continue;
}
if (ch == '\n') {
return sb.toString(); //hello
} else {
sb.append((char)ch); //hello
}
}
// 為了防止數據丟失,判斷sb的長度不能大於0
if (sb.length() > 0) {
return sb.toString();
}
return null;
}
/*
* 先寫一個關閉方法
*/
public void close() throws IOException {
this.r.close();
}
}
===============================================
package cn.itcast_09;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
/*
* BufferedReader
* |--LineNumberReader
* public int getLineNumber()獲得當前行號。
* public void setLineNumber(int lineNumber)
*/
public class LineNumberReaderDemo {
public static void main(String[] args) throws IOException {
LineNumberReader lnr = new LineNumberReader(new FileReader("my.txt"));
// 從10開始才比較好
// lnr.setLineNumber(10);
// System.out.println(lnr.getLineNumber());
// System.out.println(lnr.getLineNumber());
// System.out.println(lnr.getLineNumber());
String line = null;
while ((line = lnr.readLine()) != null) {
System.out.println(lnr.getLineNumber() + ":" + line);
}
lnr.close();
}
}=========================================================================
package cn.itcast_09;
import java.io.FileReader;
import java.io.IOException;
public class MyLineNumberReaderTest {
public static void main(String[] args) throws IOException {
// MyLineNumberReader mlnr = new MyLineNumberReader(new FileReader(
// "my.txt"));
MyLineNumberReader2 mlnr = new MyLineNumberReader2(new FileReader(
"my.txt"));
// mlnr.setLineNumber(10);
// System.out.println(mlnr.getLineNumber());
// System.out.println(mlnr.getLineNumber());
// System.out.println(mlnr.getLineNumber());
String line = null;
while ((line = mlnr.readLine()) != null) {
System.out.println(mlnr.getLineNumber() + ":" + line);
}
mlnr.close();
}
}
package cn.itcast_09;
import java.io.IOException;
import java.io.Reader;
public class MyLineNumberReader {
private Reader r;
private int lineNumber = 0;
public MyLineNumberReader(Reader r) {
this.r = r;
}
public int getLineNumber() {
// lineNumber++;
return lineNumber;
}
public void setLineNumber(int lineNumber) {
this.lineNumber = lineNumber;
}
public String readLine() throws IOException {
lineNumber++;
StringBuilder sb = new StringBuilder();
int ch = 0;
while ((ch = r.read()) != -1) {
if (ch == '\r') {
continue;
}
if (ch == '\n') {
return sb.toString();
} else {
sb.append((char) ch);
}
}
if (sb.length() > 0) {
return sb.toString();
}
return null;
}
public void close() throws IOException {
this.r.close();
}
}
四.其它相關的流
package cn.itcast_01;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
數據輸入流允許應用程序以與機器無關方式從底層輸入流中讀取基本 Java 數據類型。應用程序可以使用數據輸出流寫入稍后由數據輸入流讀取的數據。
DataInputStream 對於多線程訪問不一定是安全的。 線程安全是可選的,它由此類方法的使用者負責。
* 可以讀寫基本數據類型的數據
* 數據輸入流:DataInputStream
* DataInputStream(InputStream in)
使用指定的底層 InputStream 創建一個 DataInputStream。 |
* 數據輸出流:DataOutputStream
* DataOutputStream(OutputStream out)
*/
public class DataStreamDemo {
public static void main(String[] args) throws IOException {
// 寫
// write();
// 讀
read();
}
private static void read() throws IOException {
// DataInputStream(InputStream in)
// 創建數據輸入流對象
DataInputStream dis = new DataInputStream(
new FileInputStream("dos.txt"));
// 讀數據
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 bb = dis.readBoolean();
// 釋放資源
dis.close();
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(bb);
}
private static void write() throws IOException {
// DataOutputStream(OutputStream out)
// 創建數據輸出流對象
DataOutputStream dos = new DataOutputStream(new FileOutputStream(
"dos.txt"));
// 寫數據了
dos.writeByte(10);
dos.writeShort(100);
dos.writeInt(1000);
dos.writeLong(10000);
dos.writeFloat(12.34F);
dos.writeDouble(12.56);
dos.writeChar('a');
dos.writeBoolean(true);
// 釋放資源
dos.close();
}
}
===================================================
package cn.itcast_02;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/*
* 內存操作流:用於處理臨時存儲信息的,程序結束,數據就從內存中消失。
* 字節數組:
ByteArrayInputStream 包含一個內部緩沖區,該緩沖區包含從流中讀取的字節。內部計數器跟蹤 read 方法要提供的下一個字節。關閉 ByteArrayInputStream 無效。此類中的方法在關閉此流后仍可被調用,而不會產生任何 IOException。
* ByteArrayInputStream
* ByteArrayOutputStream
* 字符數組:
* CharArrayReader
* CharArrayWriter
* 字符串:
* StringReader
* StringWriter
*/
public class ByteArrayStreamDemo {
public static void main(String[] args) throws IOException {
// 寫數據
// ByteArrayOutputStream()
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 寫數據
for (int x = 0; x < 10; x++) {
baos.write(("hello" + x).getBytes());
}
// 釋放資源
// 通過查看源碼我們知道這里什么都沒做,所以根本需要close()
// baos.close();
// public byte[] toByteArray()
byte[] bys = baos.toByteArray();
// 讀數據
// ByteArrayInputStream(byte[] buf)
ByteArrayInputStream bais = new ByteArrayInputStream(bys);
int by = 0;
while ((by = bais.read()) != -1) {
System.out.print((char) by);
}
// bais.close();
}
}
===================================================================
package cn.itcast_03;
import java.io.IOException;
import java.io.PrintWriter;
/*
* 打印流
* 字節流打印流 PrintStream
PrintStream 為其他輸出流添加了功能,使它們能夠方便地打印各種數據值表示形式。
* 字符打印流 PrintWriter
*
向文本輸出流打印對象的格式化表示形式。此類實現在 PrintStream 中的所有 print 方法。它不包含用於寫入原始字節的方法,對於這些字節,程序應該使用未編碼的字節流進行寫入。
* 打印流的特點:
* A:只有寫數據的,沒有讀取數據。只能操作目的地,不能操作數據源。
* B:可以操作任意類型的數據。
* C:如果啟動了自動刷新,能夠自動刷新。
* D:該流是可以直接操作文本文件的。
* 哪些流對象是可以直接操作文本文件的呢?
* FileInputStream
* FileOutputStream
* FileReader
* FileWriter
* PrintStream
* PrintWriter
* 看API,查流對象的構造方法,如果同時有File類型和String類型的參數,一般來說就是可以直接操作文件的。
*
* 流:
* 基本流:就是能夠直接讀寫文件的
* 高級流:在基本流基礎上提供了一些其他的功能
*/
public class PrintWriterDemo {
public static void main(String[] args) throws IOException {
// 作為Writer的子類使用
PrintWriter pw = new PrintWriter("pw.txt");
pw.write("hello");
pw.write("world");
pw.write("java");
pw.close();
}
}
===============================================================================
package cn.itcast_03;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
/*
* 1:可以操作任意類型的數據。
* print()
* println()
* 2:啟動自動刷新
* PrintWriter pw = new PrintWriter(new FileWriter("pw2.txt"), true);
* 還是應該調用println()的方法才可以
* 這個時候不僅僅自動刷新了,還實現了數據的換行。
*
* println()
* 其實等價於於:
* bw.write();
* bw.newLine();
* bw.flush();
*/
public class PrintWriterDemo2 {
public static void main(String[] args) throws IOException {
// 創建打印流對象
// PrintWriter pw = new PrintWriter("pw2.txt");
PrintWriter pw = new PrintWriter(new FileWriter("pw2.txt"), true);
// write()是搞不定的,怎么辦呢?
// 我們就應該看看它的新方法
// pw.print(true);
// pw.print(100);
// pw.print("hello");
pw.println("hello");
pw.println(true);
pw.println(100);
pw.close();
}
}
hello
true
100
-====================================================================================
package cn.itcast_03;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
/*
* 需求:DataStreamDemo.java復制到Copy.java中
* 數據源:
* DataStreamDemo.java -- 讀取數據 -- FileReader -- BufferedReader
* 目的地:
* Copy.java -- 寫出數據 -- FileWriter -- BufferedWriter -- PrintWriter
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
// 以前的版本
// 封裝數據源
// BufferedReader br = new BufferedReader(new FileReader(
// "DataStreamDemo.java"));
// // 封裝目的地
// BufferedWriter bw = new BufferedWriter(new FileWriter("Copy.java"));
//
// String line = null;
// while ((line = br.readLine()) != null) {
// bw.write(line);
// bw.newLine();
// bw.flush();
// }
//
// bw.close();
// br.close();
// 打印流的改進版
// 封裝數據源
BufferedReader br = new BufferedReader(new FileReader(
"DataStreamDemo.java"));
// 封裝目的地
PrintWriter pw = new PrintWriter(new FileWriter("Copy.java"), true);
String line = null;
while((line=br.readLine())!=null){
pw.println(line);
}
pw.close();
br.close();
}
}
===========================================================================
package cn.itcast_04;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
* System.in 標准輸入流。是從鍵盤獲取數據的
*
System 類包含一些有用的類字段和方法。它不能被實例化。
* 鍵盤錄入數據:
* A:main方法的args接收參數。
* java HelloWorld hello world java
* B:Scanner(JDK5以后的)
* Scanner sc = new Scanner(System.in);
* String s = sc.nextLine();
* int x = sc.nextInt()
* C:通過字符緩沖流包裝標准輸入流實現
* BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
*/
public class SystemInDemo {
public static void main(String[] args) throws IOException {
// //獲取標准輸入流
// InputStream is = System.in;
// //我要一次獲取一行行不行呢?
// //行。
// //怎么實現呢?
// //要想實現,首先你得知道一次讀取一行數據的方法是哪個呢?
// //readLine()
// //而這個方法在哪個類中呢?
// //BufferedReader
// //所以,你這次應該創建BufferedReader的對象,但是底層還是的使用標准輸入流
// // BufferedReader br = new BufferedReader(is);
// //按照我們的推想,現在應該可以了,但是卻報錯了
// //原因是:字符緩沖流只能針對字符流操作,而你現在是字節流,所以不能是用?
// //那么,我還就想使用了,請大家給我一個解決方案?
// //把字節流轉換為字符流,然后在通過字符緩沖流操作
// InputStreamReader isr = new InputStreamReader(is);
// BufferedReader br= new BufferedReader(isr);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("請輸入一個字符串:");
String line = br.readLine();
System.out.println("你輸入的字符串是:" + line);
System.out.println("請輸入一個整數:");
// int i = Integer.parseInt(br.readLine());
line = br.readLine();
int i = Integer.parseInt(line);
System.out.println("你輸入的整數是:" + i);
}
}
=============================================
package cn.itcast_04;
import java.io.PrintStream;
/*
* 標准輸入輸出流
* System類中的兩個成員變量:
* public static final InputStream in “標准”輸入流。
* public static final PrintStream out “標准”輸出流。
*
* InputStream is = System.in;
* PrintStream ps = System.out;
*/
public class SystemOutDemo {
public static void main(String[] args) {
// 有這里的講解我們就知道了,這個輸出語句其本質是IO流操作,把數據輸出到控制台。
System.out.println("helloworld");
// 獲取標准輸出流對象
PrintStream ps = System.out;
ps.println("helloworld");
ps.println();
// ps.print();//這個方法不存在
// System.out.println();
// System.out.print();
}
}
=====================================================================
package cn.itcast_04;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
/*
* 轉換流的應用。
*/
public class SystemOutDemo2 {
public static void main(String[] args) throws IOException {
// 獲取標准輸入流
// // PrintStream ps = System.out;
// // OutputStream os = ps;
// OutputStream os = System.out; // 多態
// // 我能不能按照剛才使用標准輸入流的方式一樣把數據輸出到控制台呢?
// OutputStreamWriter osw = new OutputStreamWriter(os);
// BufferedWriter bw = new BufferedWriter(osw);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
System.out));
bw.write("hello");
bw.newLine();
// bw.flush();
bw.write("world");
bw.newLine();
// bw.flush();
bw.write("java");
bw.newLine();
bw.flush();
bw.close();
}
}
=======================================
package cn.itcast_05;
import java.io.IOException;
import java.io.RandomAccessFile;
/*
* 隨機訪問流:
* RandomAccessFile類不屬於流,是Object類的子類。
* 但它融合了InputStream和OutputStream的功能。
* 支持對文件的隨機訪問讀取和寫入。
* 實例支持對隨機訪問文件的讀取和寫入。隨機訪問文件的行為類似存儲在文件系統中的一個大型 byte 數組。存在指向該隱含數組的光標或索引,稱為文件指針;輸入操作從文件指針開始讀取字節,並隨着對字節的讀取而前移此文件指針。如果隨機訪問文件以讀取/寫入模式創建,則輸出操作也可用;輸出操作從文件指針開始寫入字節,並隨着對字節的寫入而前移此文件指針。寫入隱含數組的當前末尾之后的輸出操作導致該數組擴展。該文件指針可以通過 getFilePointer 方法讀取,並通過 seek 方法設置。
* public RandomAccessFile(String name,String mode):第一個參數是文件路徑,第二個參數是操作文件的模式。
* 模式有四種,我們最常用的一種叫"rw",這種方式表示我既可以寫數據,也可以讀取數據
*/
public class RandomAccessFileDemo {
public static void main(String[] args) throws IOException {
// write();
read();
}
private static void read() throws IOException {
// 創建隨機訪問流對象
RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");
int i = raf.readInt();
System.out.println(i);
// 該文件指針可以通過 getFilePointer方法讀取,並通過 seek 方法設置。
System.out.println("當前文件的指針位置是:" + raf.getFilePointer());
char ch = raf.readChar();
System.out.println(ch);
System.out.println("當前文件的指針位置是:" + raf.getFilePointer());
String s = raf.readUTF();
System.out.println(s);
System.out.println("當前文件的指針位置是:" + raf.getFilePointer());
// 我不想重頭開始了,我就要讀取a,怎么辦呢?
raf.seek(4);
ch = raf.readChar();
System.out.println(ch);
}
private static void write() throws IOException {
// 創建隨機訪問流對象
RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");
// 怎么玩呢?
raf.writeInt(100);
raf.writeChar('a');
raf.writeUTF("中國");
raf.close();
}
}
======================================
package cn.itcast_06;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
/*
* 以前的操作:
* a.txt -- b.txt
* c.txt -- d.txt
*
* 現在想要:
* a.txt+b.txt -- c.txt
*/
public class SequenceInputStreamDemo {
public static void main(String[] args) throws IOException {
// SequenceInputStream(InputStream s1, InputStream s2)
// 需求:把ByteArrayStreamDemo.java和DataStreamDemo.java的內容復制到Copy.java中
InputStream s1 = new FileInputStream("ByteArrayStreamDemo.java");
InputStream s2 = new FileInputStream("DataStreamDemo.java");
SequenceInputStream sis = new SequenceInputStream(s1, s2);
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("Copy.java"));
// 如何寫讀寫呢,其實很簡單,你就按照以前怎么讀寫,現在還是怎么讀寫
byte[] bys = new byte[1024];
int len = 0;
while ((len = sis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
sis.close();
}
}
====================================================================
package cn.itcast_06;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;
/*
* 以前的操作:
* a.txt -- b.txt
* c.txt -- d.txt
* e.txt -- f.txt
*
* 現在想要:
* a.txt+b.txt+c.txt -- d.txt
*/
public class SequenceInputStreamDemo2 {
public static void main(String[] args) throws IOException {
// 需求:把下面的三個文件的內容復制到Copy.java中
// ByteArrayStreamDemo.java,CopyFileDemo.java,DataStreamDemo.java
// SequenceInputStream(Enumeration e)
// 通過簡單的回顧我們知道了Enumeration是Vector中的一個方法的返回值類型。
// Enumeration<E> elements()
Vector<InputStream> v = new Vector<InputStream>();
InputStream s1 = new FileInputStream("ByteArrayStreamDemo.java");
InputStream s2 = new FileInputStream("CopyFileDemo.java");
InputStream s3 = new FileInputStream("DataStreamDemo.java");
v.add(s1);
v.add(s2);
v.add(s3);
Enumeration<InputStream> en = v.elements();
SequenceInputStream sis = new SequenceInputStream(en);
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("Copy.java"));
// 如何寫讀寫呢,其實很簡單,你就按照以前怎么讀寫,現在還是怎么讀寫
byte[] bys = new byte[1024];
int len = 0;
while ((len = sis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
sis.close();
}
}
==================================================================
package cn.itcast_07;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/*
* 序列化流:把對象按照流一樣的方式存入文本文件或者在網絡中傳輸。對象 -- 流數據(ObjectOutputStream)
* 反序列化流:把文本文件中的流對象數據或者網絡中的流對象數據還原成對象。流數據 -- 對象(ObjectInputStream)
*/
public class ObjectStreamDemo {
public static void main(String[] args) throws IOException,
ClassNotFoundException {
// 由於我們要對對象進行序 列化,所以我們先自定義一個類
// 序列化數據其實就是把對象寫到文本文件
// write();
read();
}
private static void read() throws IOException, ClassNotFoundException {
// 創建反序列化對象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
"oos.txt"));
// 還原對象
Object obj = ois.readObject();
// 釋放資源
ois.close();
// 輸出對象
System.out.println(obj);
}
private static void write() throws IOException {
// 創建序列化流對象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
"oos.txt"));
// 創建對象
Person p = new Person("林青霞", 27);
// public final void writeObject(Object obj)
oos.writeObject(p);
// 釋放資源
oos.close();
}
}
============================================================
package cn.itcast_07;
import java.io.Serializable;
/*
* NotSerializableException:未序列化異常
* 當實例需要具有序列化接口時,拋出此異常。序列化運行時或實例的類會拋出此異常。參數應該為類的名稱
* 類通過實現 java.io.Serializable 接口以啟用其序列化功能。未實現此接口的類將無法使其任何狀態序列化或反序列化。
* 該接口居然沒有任何方法,類似於這種沒有方法的接口被稱為標記接口。
*
* java.io.InvalidClassException:
* cn.itcast_07.Person; local class incompatible:
* stream classdesc serialVersionUID = -2071565876962058344,
* local class serialVersionUID = -8345153069362641443
* 序列化運行時使用一個稱為 serialVersionUID 的版本號與每個可序列化類相關聯,該序列號在反序列化過程中用於驗證序列化對象的發送者和接收者是否為該對象加載了與序列化兼容的類。如果接收者加載的該對象的類的 serialVersionUID 與對應的發送者的類的版本號不同,則反序列化將會導致 InvalidClassException。可序列化類可以通過聲明名為 "serialVersionUID" 的字段(該字段必須是靜態 (static)、最終 (final) 的 long 型字段)顯式聲明其自己的 serialVersionUID:
ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
如果可序列化類未顯式聲明 serialVersionUID,則序列化運行時將基於該類的各個方面計算該類的默認 serialVersionUID 值,如“Java(TM) 對象序列化規范”中所述。不過,強烈建議 所有可序列化類都顯式聲明 serialVersionUID 值,原因是計算默認的 serialVersionUID 對類的詳細信息具有較高的敏感性,根據編譯器實現的不同可能千差萬別,這樣在反序列化過程中可能會導致意外的 InvalidClassException。因此,為保證 serialVersionUID 值跨不同 java 編譯器實現的一致性,序列化類必須聲明一個明確的 serialVersionUID 值。還強烈建議使用 private 修飾符顯示聲明 serialVersionUID(如果可能),原因是這種聲明僅應用於直接聲明類 -- serialVersionUID 字段作為繼承成員沒有用處。數組類不能聲明一個明確的 serialVersionUID,因此它們總是具有默認的計算值,但是數組類沒有匹配 serialVersionUID 值的要求。
* 為什么會有問題呢?
* Person類實現了序列化接口,那么它本身也應該有一個標記值。
* 這個標記值假設是100。
* 開始的時候:
* Person.class -- id=100
* wirte數據: oos.txt -- id=100
* read數據: oos.txt -- id=100
*
* 現在:
* Person.class -- id=200
* wirte數據: oos.txt -- id=100
* read數據: oos.txt -- id=100
* 我們在實際開發中,可能還需要使用以前寫過的數據,不能重新寫入。怎么辦呢?
* 回想一下原因是因為它們的id值不匹配。
* 每次修改java文件的內容的時候,class文件的id值都會發生改變。
* 而讀取文件的時候,會和class文件中的id值進行匹配。所以,就會出問題。
* 但是呢,如果我有辦法,讓這個id值在java文件中是一個固定的值,這樣,你修改文件的時候,這個id值還會發生改變嗎?
* 不會。現在的關鍵是我如何能夠知道這個id值如何表示的呢?
* 不用擔心,你不用記住,也沒關系,點擊鼠標即可。
* 你難道沒有看到黃色警告線嗎?
*
* 我們要知道的是:
* 看到類實現了序列化接口的時候,要想解決黃色警告線問題,就可以自動產生一個序列化id值。
* 而且產生這個值以后,我們對類進行任何改動,它讀取以前的數據是沒有問題的。
*
* 注意:
* 我一個類中可能有很多的成員變量,有些我不想進行序列化。請問該怎么辦呢?
* 使用transient關鍵字聲明不需要序列化的成員變量
*/
public class Person implements Serializable {
private static final long serialVersionUID = -2071565876962058344L;
private String name;
// private int age;
private transient int age;
// int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
======================================================
package cn.itcast_08;
import java.util.Properties;
import java.util.Set;
/*
* Properties:屬性集合類。是一個可以和IO流相結合使用的集合類。
* Properties 可保存在流中或從流中加載。屬性列表中每個鍵及其對應值都是一個字符串。
*
* 是Hashtable的子類,說明是一個Map集合。
*/
public class PropertiesDemo {
public static void main(String[] args) {
// 作為Map集合的使用
// 下面這種用法是錯誤的,一定要看API,如果沒有<>,就說明該類不是一個泛型類,在使用的時候就不能加泛型
// Properties<String, String> prop = new Properties<String, String>();
Properties prop = new Properties();
// 添加元素
prop.put("it002", "hello");
prop.put("it001", "world");
prop.put("it003", "java");
// System.out.println("prop:" + prop);
// 遍歷集合
Set<Object> set = prop.keySet();
for (Object key : set) {
Object value = prop.get(key);
System.out.println(key + "---" + value);
}
}
}
=============================================================================================package cn.itcast_08;
import java.util.Properties;
import java.util.Set;
/*
* 特殊功能:
* public Object setProperty(String key,String value):添加元素
* public String getProperty(String key):獲取元素
* public Set<String> stringPropertyNames():獲取所有的鍵的集合
*/
public class PropertiesDemo2 {
public static void main(String[] args) {
// 創建集合對象
Properties prop = new Properties();
// 添加元素
prop.setProperty("張三", "30");
prop.setProperty("李四", "40");
prop.setProperty("王五", "50");
// public Set<String> stringPropertyNames():獲取所有的鍵的集合
Set<String> set = prop.stringPropertyNames();
for (String key : set) {
String value = prop.getProperty(key);
System.out.println(key + "---" + value);
}
}
}
/*
* class Hashtalbe<K,V> { public V put(K key,V value) { ... } }
*
* class Properties extends Hashtable { public V setProperty(String key,String
* value) { return put(key,value); } }
*/
=====================================================================
package cn.itcast_08;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Properties;
/*
* 這里的集合必須是Properties集合:
* public void load(Reader reader):把文件中的數據讀取到集合中
* public void store(Writer writer,String comments):把集合中的數據存儲到文件
*
* 單機版游戲:
* 進度保存和加載。
* 三國群英傳,三國志,仙劍奇俠傳...
*
* 呂布=1
* 方天畫戟=1
*/
public class PropertiesDemo3 {
public static void main(String[] args) throws IOException {
// myLoad();
myStore();
}
private static void myStore() throws IOException {
// 創建集合對象
Properties prop = new Properties();
prop.setProperty("林青霞", "27");
prop.setProperty("武鑫", "30");
prop.setProperty("劉曉曲", "18");
//public void store(Writer writer,String comments):把集合中的數據存儲到文件
Writer w = new FileWriter("name.txt");
prop.store(w, "helloworld");
w.close();
}
private static void myLoad() throws IOException {
Properties prop = new Properties();
// public void load(Reader reader):把文件中的數據讀取到集合中
// 注意:這個文件的數據必須是鍵值對形式
Reader r = new FileReader("prop.txt");
prop.load(r);
r.close();
System.out.println("prop:" + prop);
}
}
============================================================================
package cn.itcast_08;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Properties;
import java.util.Set;
/*
* 我有一個文本文件(user.txt),我知道數據是鍵值對形式的,但是不知道內容是什么。
* 請寫一個程序判斷是否有“lisi”這樣的鍵存在,如果有就改變其實為”100”
*
* 分析:
* A:把文件中的數據加載到集合中
* B:遍歷集合,獲取得到每一個鍵
* C:判斷鍵是否有為"lisi"的,如果有就修改其值為"100"
* D:把集合中的數據重新存儲到文件中
*/
public class PropertiesTest {
public static void main(String[] args) throws IOException {
// 把文件中的數據加載到集合中
Properties prop = new Properties();
Reader r = new FileReader("user.txt");
prop.load(r);
r.close();
// 遍歷集合,獲取得到每一個鍵
Set<String> set = prop.stringPropertyNames();
for (String key : set) {
// 判斷鍵是否有為"lisi"的,如果有就修改其值為"100"
if ("lisi".equals(key)) {
prop.setProperty(key, "100");
break;
}
}
// 把集合中的數據重新存儲到文件中
Writer w = new FileWriter("user.txt");
prop.store(w, null);
w.close();
}
}
==========================================================================================
package cn.itcast_08;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Properties;
/*
* 我有一個猜數字小游戲的程序,請寫一個程序實現在測試類中只能用5次,超過5次提示:游戲試玩已結束,請付費。
*/
public class PropertiesTest2 {
public static void main(String[] args) throws IOException {
// 讀取某個地方的數據,如果次數不大於5,可以繼續玩。否則就提示"游戲試玩已結束,請付費。"
// 創建一個文件
// File file = new File("count.txt");
// if (!file.exists()) {
// file.createNewFile();
// }
// 把數據加載到集合中
Properties prop = new Properties();
Reader r = new FileReader("count.txt");
prop.load(r);
r.close();
// 我自己的程序,我當然知道里面的鍵是誰
String value = prop.getProperty("count");
int number = Integer.parseInt(value);
if (number > 5) {
System.out.println("游戲試玩已結束,請付費。");
System.exit(0);
} else {
number++;
prop.setProperty("count", String.valueOf(number));
Writer w = new FileWriter("count.txt");
prop.store(w, null);
w.close();
GuessNumber.start();
}
}
}
===================================================================================
package cn.itcast_09;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
/*
* nio包在JDK4出現,提供了IO流的操作效率。但是目前還不是大范圍的使用。
* 有空的話了解下,有問題再問我。
*
* JDK7的之后的nio:
* Path:路徑
* Paths:有一個靜態方法返回一個路徑
* public static Path get(URI uri)
* Files:提供了靜態方法供我們使用
* public static long copy(Path source,OutputStream out):復制文件
* public static Path write(Path path,Iterable<? extends CharSequence> lines,Charset cs,OpenOption... options)
*/
public class NIODemo {
public static void main(String[] args) throws IOException {
// public static long copy(Path source,OutputStream out)
// Files.copy(Paths.get("ByteArrayStreamDemo.java"), new
// FileOutputStream(
// "Copy.java"));
ArrayList<String> array = new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("java");
Files.write(Paths.get("array.txt"), array, Charset.forName("GBK"));
}
}
==================================================================
package cn.itcast.dao;
import cn.itcast.pojo.User;
/**
* 這是針對用戶進行操作的接口
*
* @author 風清揚
* @version V1.1
*
*/
public interface UserDao {
/**
* 這是用戶登錄功能
*
* @param username
* 用戶名
* @param password
* 密碼
* @return 返回登錄是否成功
*/
public abstract boolean isLogin(String username, String password);
/**
* 這是用戶注冊功能
*
* @param user
* 要注冊的用戶信息
*/
public abstract void regist(User user);
}
package cn.itcast.dao.impl;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import cn.itcast.dao.UserDao;
import cn.itcast.pojo.User;
/**
* 這是用戶操作的具體實現類(IO版)
*
* @author 風清揚
* @version V1.1
*
*/
public class UserDaoImpl implements UserDao {
// 為了保證文件一加載就創建
private static File file = new File("user.txt");
static {
try {
file.createNewFile();
} catch (IOException e) {
System.out.println("創建文件失敗");
// e.printStackTrace();
}
}
@Override
public boolean isLogin(String username, String password) {
boolean flag = false;
BufferedReader br = null;
try {
// br = new BufferedReader(new FileReader("user.txt"));
br = new BufferedReader(new FileReader(file));
String line = null;
while ((line = br.readLine()) != null) {
// 用戶名=密碼
String[] datas = line.split("=");
if (datas[0].equals(username) && datas[1].equals(password)) {
flag = true;
break;
}
}
} catch (FileNotFoundException e) {
System.out.println("用戶登錄找不到信息所在的文件");
// e.printStackTrace();
} catch (IOException e) {
System.out.println("用戶登錄失敗");
// e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
System.out.println("用戶登錄釋放資源失敗");
// e.printStackTrace();
}
}
}
return flag;
}
@Override
public void regist(User user) {
/*
* 為了讓注冊的數據能夠有一定的規則,我就自己定義了一個規則: 用戶名=密碼
*/
BufferedWriter bw = null;
try {
// bw = new BufferedWriter(new FileWriter("user.txt"));
// bw = new BufferedWriter(new FileWriter(file));
// 為了保證數據是追加寫入,必須加true
bw = new BufferedWriter(new FileWriter(file, true));
bw.write(user.getUsername() + "=" + user.getPassword());
bw.newLine();
bw.flush();
} catch (IOException e) {
System.out.println("用戶注冊失敗");
// e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
System.out.println("用戶注冊釋放資源失敗");
// e.printStackTrace();
}
}
}
}
}
package cn.itcast.game;
import java.util.Scanner;
/**
* 這是猜數字小游戲
*
* @author 風清揚
* @version V1.1
*
*/
public class GuessNumber {
private GuessNumber() {
}
public static void start() {
// 產生一個隨機數
int number = (int) (Math.random() * 100) + 1;
// 定義一個統計變量
int count = 0;
while (true) {
// 鍵盤錄入一個數據
Scanner sc = new Scanner(System.in);
System.out.println("請輸入數據(1-100):");
int guessNumber = sc.nextInt();
count++;
// 判斷
if (guessNumber > number) {
System.out.println("你猜的數據" + guessNumber + "大了");
} else if (guessNumber < number) {
System.out.println("你猜的數據" + guessNumber + "小了");
} else {
System.out.println("恭喜你," + count + "次就猜中了");
break;
}
}
}
}
package cn.itcast.pojo;
/**
* 這是用戶基本描述類
*
* @author 風清揚
* @version V1.1
*
*/
public class User {
// 用戶名
private String username;
// 密碼
private String password;
public User() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package cn.itcast.test;
import java.util.Scanner;
import cn.itcast.dao.UserDao;
import cn.itcast.dao.impl.UserDaoImpl;
import cn.itcast.game.GuessNumber;
import cn.itcast.pojo.User;
/**
* 用戶測試類
*
* @author 風清揚
* @version V1.1
*
*/
public class UserTest {
public static void main(String[] args) {
// 為了能夠回來
while (true) {
// 歡迎界面,給出選擇項
System.out.println("--------------歡迎光臨--------------");
System.out.println("1 登錄");
System.out.println("2 注冊");
System.out.println("3 退出");
System.out.println("請輸入你的選擇:");
// 鍵盤錄入選擇,根據選擇做不同的操作
Scanner sc = new Scanner(System.in);
// 為了后面的錄入信息的方便,我所有的數據錄入全部用字符接收
String choiceString = sc.nextLine();
// switch語句的多個地方要使用,我就定義到外面
UserDao ud = new UserDaoImpl();
// 經過簡單的思考,我選擇了switch
switch (choiceString) {
case "1":
// 登錄界面,請輸入用戶名和密碼
System.out.println("--------------登錄界面--------------");
System.out.println("請輸入用戶名:");
String username = sc.nextLine();
System.out.println("請輸入密碼:");
String password = sc.nextLine();
// 調用登錄功能
// UserDao ud = new UserDaomImpl();
boolean flag = ud.isLogin(username, password);
if (flag) {
System.out.println("登錄成功,可以開始玩游戲了");
System.out.println("你玩嗎?y/n");
while (true) {
String resultString = sc.nextLine();
if (resultString.equalsIgnoreCase("y")) {
// 玩游戲
GuessNumber.start();
System.out.println("你還玩嗎?y/n");
} else {
break;
}
}
System.out.println("謝謝使用,歡迎下次再來");
System.exit(0);
// break; //這里寫break,結束的是switch
} else {
System.out.println("用戶名或者密碼有誤,登錄失敗");
}
break;
case "2":
// 歡迎界面,請輸入用戶名和密碼
System.out.println("--------------注冊界面--------------");
System.out.println("請輸入用戶名:");
String newUsername = sc.nextLine();
System.out.println("請輸入密碼:");
String newPassword = sc.nextLine();
// 把用戶名和密碼封裝到一個對象中
User user = new User();
user.setUsername(newUsername);
user.setPassword(newPassword);
// 調用注冊功能
// 多態
// UserDao ud = new UserDaoImpl();
// 具體類使用
// UserDaoImpl udi = new UserDaoImpl();
ud.regist(user);
System.out.println("注冊成功");
break;
case "3":
default:
System.out.println("謝謝使用,歡迎下次再來");
System.exit(0);
break;
}
}
}
}
===================================================================================================