* ...
* ...
* ...
* 1,1,2,3,5,8...
// ...
===========================================================================package cn.itcast_01;
===========================================================================package cn.itcast_05;
==========================================================================
二.
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
用來讀取字符文件的便捷類。此類的構造方法假定默認字符編碼和默認字節緩衝區大小都是適當的。要本身指定這些值,能夠先在 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;
}
}
}
}
===================================================================================================