java基礎第十五篇之IO流和遞歸演算法

来源:https://www.cnblogs.com/haizai/archive/2019/07/06/11144355.html
-Advertisement-
Play Games

FileInputStream : 輸入流 int available() : 一次讀取所有的位元組數 read() : 將文件上的位元組讀取到記憶體的數組中 FileOutputStream : 輸出流 write(byte[] b) : 將位元組數組中的位元組數據寫到文件上 緩衝區會在記憶體中創建一個819 ...


FileInputStream : 輸入流

int available() : 一次讀取所有的位元組數

read() : 將文件上的位元組讀取到記憶體的數組中

FileOutputStream : 輸出流

write(byte[] b) : 將位元組數組中的位元組數據寫到文件上

緩衝區會在記憶體中創建一個8192容量的位元組數組,記憶體的運算效率比硬碟要高的多所有隻要降低到硬碟的讀寫次數就會提高效率.

定義小數組的標準格式:進行讀寫位元組數據,要比緩衝流快一點點,因為定義小數組是操作一個的是一個數組,而緩衝流操作的是兩個數組.

public class Demo3_ArrayCopy {
public static void main(String[] args) throws IOException {
//method();
// method2();

FileInputStream fis = new FileInputStream("韓雪 - 想起.mp3");
FileOutputStream fos = new FileOutputStream("Copy.mp3");

byte[] arr = new byte[1024*8];
int len;
while((len = fis.read(arr)) != -1) {
fos.write(arr,0,len);
}
fis.close();
fis.close();

}


close方法:具備刷新的功能,在關閉流之前,就會先刷新一次緩衝區,將緩衝區的位元組全都刷新到文件上.
* flush : 具備刷新的功能,刷完之後還可以繼續寫.


* 位元組流讀取中文的問題:位元組流在讀中文的時候有可能會讀到半個中文,造成亂碼.
* 位元組流寫中文的問題:位元組流直接操作位元組,所有寫出中文比較將字元串轉換成位元組數組
* 寫出回車換行write("\r\n".getBytes());

//這是1.6版本以及以前的標準處理異常代碼
private static void method() throws FileNotFoundException, IOException {
//為什麼要加null?這裡是局部變數,所有必須要賦值.
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("xxx.txt");
fos = new FileOutputStream("yyy.txt");

int b;
while((b = fis.read()) != -1) {
fos.write(b);
}

}finally {
//try finally的嵌套目的是能關一個就儘量關一個
try {
if(fis != null) {
fis.close();
}
}finally {

if(fos != null) {
fos.close();
}

}



}
}
}

///這些抽象類具備自動關閉功能,只要實現了AutoCloseable就具備了,自動關閉釋放流的功能
//這是jdk1.7版本的標準異常處理代碼
原理:在try()中創建的流對象必須實現了AutoCloseable這個介面,如果實現了,在try後面的{}(讀寫代碼)執行後就會自動調用流對象的close方法將流關閉釋放.
try (
//這些抽象類具備自動關閉功能,只要實現了AutoCloseable就具備了,自動關閉釋放流的功能
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("yyy.txt");
MyClose mc = new MyClose();

){
int b ;
while((b = fis.read()) != -1) {
fos.write(b);
}

/*FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("yyy.txt");*/


}
/*fis.close();
fos.close();*/

}

/*
* 圖片加密
* 將寫出的位元組異或上一個數,這個數就是密鑰,解密的時候再次異或就可以了.
*/
public class Test1 {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("2017619-星期一-141543.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("Copy.jpg"));

int b;
while((b = bis.read()) != -1) {
//運算符 ^ 異或兩次就是他本身,所有這裡異或一次,進行加密
bos.write(b ^ 123);
}
bis.close();
bos.close();


}

//解密
/*BufferedInputStream bis = new BufferedInputStream(new FileInputStream("Copy.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("Copy2.jpg"));

int b;
while((b = bis.read()) != -1) {
//運算符 ^ 異或兩次就是他本身,所有這裡異或一次,進行加密
bos.write(b ^ 123);
}
bis.close();
bos.close();
*
*
*/

}

* 在控制台錄入文件的路徑,將文件拷貝到當前項目下
*
* 分析:
*
* 1.創建鍵盤錄入對象,定義方法對鍵盤錄入的路徑進行判斷,如果是文件就返回
* 2.在主方法中接收該文件
* 3.讀和寫該文件
*
*
*/
public class Test2 {
public static void main(String[] args) throws IOException {
//獲取文件
File file = getFile();

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName()));

int b;
while((b = bis.read()) != -1) {
bos.write(b);
}
bis.close();
bos.close();

}

/*
* 定義一個方法獲取鍵盤錄入的文件路徑,並封裝成File對象返回
* 1.返回值類型File
* 2.參數列表無
*
*/
public static File getFile() {
Scanner sc = new Scanner(System.in);
//接收鍵盤錄入路徑
//String line = sc.nextLine();
System.out.println("請輸入一個文件的路徑");
while(true) {
String line = sc.nextLine();
//封裝成File對象,並對其進行判斷
File file = new File(line);
if(!file.exists()) {
System.out.println("您錄入的文件路徑不存在,請重新錄入:");
}else if(file.isDirectory()) {
System.out.println("請錄入的是文件夾路徑,請重新錄入");
}else {
return file;
}


}


}
}


/*
* 鍵盤錄入的數據拷貝到當前項目下的text.txt文件中,鍵盤錄入數據當遇到quit時就退出
*
* 分析:
* 1.創建鍵盤錄入對象
* 2.創建輸出流對象,關聯text.txt文件
* 3.定義無限迴圈
* 4.遇到quit退出迴圈
* 5.如果不quit,就將內容寫出
* 6.關閉流
*
*/
public class Test3 {
public static void main(String[] args) throws IOException {
//1.創建鍵盤錄入對象
Scanner sc = new Scanner(System.in);
//2.創建輸出流對象,關聯text.txt文件
FileOutputStream fos = new FileOutputStream("text.txt");
System.out.println("請輸入數據");
//3.定義無限迴圈
while (true) {
String lin = sc.nextLine();
//4.遇到quit退出迴圈
if("quit".equals(lin)) {
break;
}
//5.如果不quit,就將內容寫出
//字元串寫出鼻血轉換成位元組數組
fos.write(lin.getBytes());
fos.write("\r\n".getBytes());
}
fos.close();


}
}
*/
/*
* 從鍵盤錄入一個文件夾路徑,統計該文件夾大小
*
* 從鍵盤錄入一個文件夾路徑
* 1.一個無限迴圈
* 2.定義一個無限迴圈
* 3.將獎品錄入的結果存儲並封裝成File對象
* 4.對File對象判斷
* 5.將文件夾路徑對象返回
*
* 統計該文件夾大小
* 1.定義一個求和變數
* 2.獲取該文件夾下所有的文件和文件夾ListFiles();
* 3.遍曆數組
* 4.判斷是文件就計算大小並累加
* 5.判斷是文件夾,遞歸調用
*
*/
public class Test1 {
public static void main(String[] args) {
/*File dir = getDir();
System.out.println(getFileLength(dir));*/
//直接獲取文件的大小是0
File dir = new File("F:\\day06");
System.out.println(dir.length());

}

/*
* 從鍵盤錄入接收一個文件夾路徑
* 1.返回值類型File
* 2.參數列表無
*
*/
public static File getDir() {
//1.創建鍵盤錄入對象
Scanner sc = new Scanner(System.in);
System.out.println("請輸入一個文件夾路徑:");
//2.迴圈
while(true) {
//將鍵盤錄入的結果存儲並封裝成File對象
String line = sc.nextLine();
File dir = new File(line);
//對File對象判斷
if(!dir.exists()) {
System.out.println("您錄入的文件夾路徑不存在,請輸入一個文件夾路徑:");
}else if(dir.isFile()) {
System.out.println("您錄入的是文件路徑,請輸入一個文件夾路徑:");
}else {
//將文件夾路徑對象返回
return dir;
}
}


}


/*
* 統計該文件夾大小
* 1.返回值類型 long
* 2.參數列表File
*
*/
public static long getFileLength(File dir) {
//1.定義一個求和變數
long len = 0;
// 2.獲取該文件夾下所有的文件和文件夾ListFiles();
File[] subFiles = dir.listFiles();
// 3.遍曆數組
for (File subFile : subFiles) {
//4.判斷是文件就計算大小並累加
if(subFile.isFile()) {
len = len + subFile.length();
// 5.判斷是文件夾,遞歸調用
}else {
len = len + getFileLength(subFile);
}

}




return len;
}

}
*/

/*

/*從鍵盤接收一個文件夾路徑,刪除該文件夾
*
* 刪除該文件夾
* 分析:
* 1.獲取該文件夾下的所有的文件和文件夾
* 2.遍曆數組
* 3.判斷是文件直接刪除
* 4.如果是文件夾,遞歸調用
* 5.迴圈結束後,把空文件夾刪掉
*
*/
public class Test2 {
public static void main(String[] args) {
//獲取文件夾路徑
File dir = Test1.getDir();

deleteFile(dir);

}
/*
* 刪除該文件夾
* 1.返回值類型void
* 2.參數列表:File dir
*
*/
public static void deleteFile(File dir) {
// 1.獲取該文件夾下的所有的文件和文件夾
File[] subFiles = dir.listFiles();
//2.遍曆數組
for (File subFile : subFiles) {
//3.判斷是文件直接刪除
if(subFile.exists()){
subFile.delete();
//4.如果是文件夾,遞歸調用
}else {
deleteFile(subFile);
}
}
//5.迴圈結束後,把空文件夾刪掉
dir.delete();


}
}

/*
* 從鍵盤接收兩個文件夾路徑,把其中一個文件夾中(包含內容)拷貝到另一個文件夾中
*
* 分析:
* 1.在目標文件夾中創建原文件夾
* 2.獲取原文件夾中所有的文件和文件夾,存儲在File數組中
* 3.遍曆數組
* 4.如果是文件就用io流讀寫
* 5.如果是文件夾就遞歸調用
*
*/
public class Test3 {
public static void main(String[] args) throws IOException {
File src = Test1.getDir();
File dest = Test1.getDir();
if(src.equals(dest)) {
System.out.println("目標文件夾是原文件夾的子文件夾");
}else {
copy(src,dest);

}

}

/*把其中一個文件夾中(包含內容)拷貝到另一個文件夾中
* 1.返回值類型void
* 2.參數列表:File src,File dest
*
*/
public static void copy(File src, File dest) throws IOException {
//1.在目標文件夾中創建原文件夾
File newDir = new File(dest, src.getName());
newDir.mkdir();
// 2.獲取原文件夾中所有的文件和文件夾,存儲在File數組中
File[] subFiles = src.listFiles();
//3.遍曆數組
for (File subFile : subFiles) {
if(subFile.isFile()) {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(subFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(newDir,subFile.getName())));
int b;
while((b = bis.read()) != -1) {
bos.write(b);
}
bos.close();
bis.close();

}else {
copy(subFile,newDir);
}
}


}
UTF-8:用UTF-8編碼的Java文件英文字元占一個位元組,中文字元占三個位元組.

GBK:用GBK編碼的中文占用兩個位元組,英文占用一個位元組.

在Windows裡面的.txt文件中編碼的一個字元是占用一個位元組,一個中文字占用兩個位元組(日文的一個日文字也占用兩個位元組)



}
/*
read() : 一次讀取一個位元組
//創建一個文件輸入流對象,並關聯aaa.txt
FileInputStream fis = new FileInputStream("aaa.txt");
//定義變數,記錄每次讀到的位元組
int b;
//將每次讀到的位元組賦值給b並判斷是否是-1
while((b = fis.read() != -1) {
syso(b);
}
fis.close();


read()方法返回值為什麼是int?
因為位元組輸入流可以操作任意類型的文件,比如圖片音頻等,這些文件底層都以二進位形式的存儲的,如果每次讀取都返回byte,有可能在讀到中間的時候遇到11111111
那麼這個11111111是byte類型的-1,我們的程式是遇到-1就會停止不讀了,後面的數據就讀不到了,所以在讀取的時候用int類型接收,如果11111111會在其前面補上
24個0湊足4個位元組,那麼byte類型的-1就編程int類型的255了這樣可以保證整個數據讀完,而結束標記的-1就是int類型

write() : 一次寫出一個位元組
//如果沒有bb.txt,會創建一個
FileOutputStream fos = new FileOutputStream("bb.txt");
//雖然寫出的是一個int數,但是在寫出的時候會將前面的24個0去掉,所有寫出的一個byte.
//fos.write(97);
fos.write(98);
fos.write(99);
fos.close();

緩衝思想:
位元組流一次讀寫一個數組的速度明顯比一次讀寫一個位元組的數據快很多,
這是加入了數組這樣的緩衝區效果,java本身在設計的時候.
也考慮到了這樣的設計思想(裝飾設計模式) , 所以提供了位元組緩衝區流

BufferedInputStream
BufferedInputStream內置了一個緩衝區(數組)
從BufferedInputStream中讀取一個位元組時
BufferedInputStream會一次性從文件中讀取8192個,存在緩衝區中,返回給程式一個
程式再次讀取時,就不用找文件了,直接從緩衝區中獲取.
直到緩衝區中所有的都被使用過,才重新從文本中讀取8192個.

BufferedOutputStream
BufferedOutputStream也內置了一個緩衝區(數組)
程式向流中寫位元組時,不會直接寫到文件,先寫到緩衝區中
直到緩衝區寫滿,BufferedOutputStream才會把緩衝區中的數據一次性寫到文件里

拷貝代碼:
//創建文件輸入流對象,關聯致青春.mp3
FileInputStream fis = new FileInputStream("知情權.mp3");
//創建緩衝區對fis修飾
BufferedInputStream bis = new BufferedInputStream(fis);
//創建輸出流對象,關聯copy.MP3
FileOutputStream fos = new FileOutputStream("copy.mp3");
//創建緩衝區對fos裝飾
BufferedOutputStream bos = new BufferedOutputStream(fos);

int b;
while((b = bis.read() != -1) {
bos.write;
}
//只關裝飾後的對象即可
bis.close();
bos.close();

小數組的讀寫和帶Buffered的讀取那個更快?
定義小數組如果是8192個位元組大小和Buffered比較的話
定義小數組會略勝一籌,因為讀和寫操作的是同一個數組
而Buffered操作的是兩個數組

flush和close的讀取那個更快?
flush()方法:
用來刷新緩衝流區的,刷新後可以再次寫出
close()方法:
用來關閉流程釋放資源的,如果是帶緩衝區的流對象的close()方法,不但會關閉流,還會再關閉之前刷新緩衝區,關閉後不能再寫出.

* File類:文件和目錄路徑名的抽象表示形式。
*
* 文件:可以保存數據的文件
* 目錄路徑名:指的文件夾,可以保存文件的
*
* 一個File對象 可以指一個文件 也可以指一個文件夾
* 構造方法:
* 1.public File(String filename);
* 2.public File(String parent,String child)
* 3.public File(File parent,String child)
*/
public class FileDemo01 {

public static void main(String[] args) {
// TODO Auto-generated method stub
//1.public File(String filename);
File f1 = new File("E:\\黑馬66期\\1.txt");
System.out.println(f1);
//2.public File(String parent, String child)
File f2 = new File("E:\\黑馬66期","1.txt");
System.out.println(f2);
//3.public File(File parent, String child)
File parent = new File("E:\\黑馬66期");
File f3 = new File(parent, "1.txt");
System.out.println(f3);
}

}


* 文件:可以保存數據的文件
* 目錄路徑名:指的文件夾,可以保存文件的
* File類的成員方法:
* 第一組 獲取方法
*
* public String getAbsolutePath();//獲取絕對路徑(以盤符開頭的路徑);
*
* public String getPath();//獲取路徑,你用構造方法創建File對象時寫那個路徑
*
* public String getName();//獲取不帶路徑的文件名
*
* public long length();//獲取只能文件位元組大小
*
*
*
*/
public class FileDemo02 {

public static void main(String[] args) {
// TODO Auto-generated method stub
//獲取絕對路徑
File f1 = new File("E:\\黑馬66期\\1.txt");
// File f1 = new File("a.txt");
// String absolutePath = f1.getAbsolutePath();
// System.out.println(absolutePath);
//獲取路徑
// String path = f1.getPath();
// System.out.println(path);
//獲取名字
// String name = f1.getName();
// System.out.println(name);
//獲取文件的長度
// long len = f1.length();
// System.out.println(len);
//能不能獲取文件夾的長度,不能獲取,獲取到的是一個不確定的值
// File f2 = new File("E:\\黑馬66期\\test");
// long len = f2.length();
// System.out.println(len);
}

}

* 文件和文件夾的創建刪除等
*
* 1.public boolean createNewFile();//只能創建文件類型File對象,如果此文件存在 那麼創建失敗
*
* 2.public boolean mkdir();//makeDirectory,創建文件夾類型的File對象
*
* 3.public boolean exists();//判斷File 對象是否存儲在
*
* 4.public boolean isDirectory();//判斷是否是文件夾
*
* 5.public boolean isFile();//判斷是否是一個文件
*
* 6.public boolean delete();//刪除文件和文件夾對象
*
7.public boolean endsWith() : 判斷
*/
public class FileDemo03 {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//1.創建文件
File f1 = new File("E:\\黑馬66期\\test\\a.txt");
// boolean b = f1.createNewFile();
// System.out.println(b);
//2.創建文件夾
// File f2 = new File("E:\\黑馬66期\\test\\a.txt");
// boolean b = f2.mkdir();
// System.out.println(b);
//3.判斷File對象是否存在
// File f3 = new File("E:\\黑馬66期\\test\\abcd.txt");
// System.out.println(f3.exists());
//4.判斷到底是文件還是文件夾
// System.out.println(f1.isDirectory());
// System.out.println(f1.isFile());
//5.刪除File對象(可以是文件也可以是文件夾)
File fd = new File("E:\\黑馬66期\\test\\abc.txt");
System.out.println(fd.delete());

}

}


* File類中另外兩個比較重要的方法
*
* 1. public String[] list();//列出代表文件夾的File對象中的文件和目錄,
* 如果調用方法的File對象表示的是一個文件,會返回null
*
*
* 2. public File[] listFiles();//列出代表文件夾的File對象中的文件和目錄,
* 如果調用方法的File對象表示的是一個文件,會返回null
*
*/
public class FileDemo01 {

public static void main(String[] args) {
// TODO Auto-generated method stub
//1.public String[] list()
// File fileDir = new File("F:\\demo");
// String[] files = fileDir.list();
// //遍歷這個files數組
// for (String file : files) {
// System.out.println(file);
// }
//2.File[] listFiles()
File fileDir = new File("F:\\demo\\1.txt");
File[] files = fileDir.listFiles();
for (File file : files) {
System.out.println(file.getAbsolutePath());
}

}

}

listFiles():過濾器

案例:
Java.io.File.listFiles(FileFilter filter) 返回抽象路徑名數組,表示在目錄中此抽象路徑名錶示,滿足指定過濾器的文件和目錄。

聲明
以下是java.io.File.listFiles(FileFilter filter) 方法的聲明:

public File[] listFiles(FileFilter filter) 參數
filter - File filter

返回值
該方法返回抽象路徑名數組,表示在目錄中此抽象路徑名錶示,滿足指定過濾器的文件和目錄。

異常
SecurityException -- 如果安全管理器存在並且其SecurityManager.checkRead(java.lang.String) 方法拒絕對文件的讀訪問

例子
下麵的例子顯示 java.io.File.listFiles(FileFilter filter)方法的用法。

package com.yiibai;

import java.io.File;
import java.io.FileFilter;

public class FileDemo {
public static void main(String[] args) {

File f = null;
File[] paths;

try{
// create new file
f = new File("c:/test");

FileFilter filter = new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isFile();
}
};

// returns pathnames for files and directory
paths = f.listFiles(filter);

// for each pathname in pathname array
for(File path:paths)
{
// prints file and directory paths
System.out.println(path);
}
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
}
}
讓我們編譯和運行上面的程式,這將產生以下結果:

c:\test\child_test.txt
c:\test\child_test.xlsx


* 遞歸:是一個演算法,只要含有方法這種概念的編程語言都可以使用
* 就是在一個方法內部,調用了自己
* StackOverflowError:堆棧溢出錯誤(記憶體溢出)
* 遞歸:1.不能無限的調用自己,必須有出口
* 2.必須保證遞歸調用方法的次數有一定限制
* 遞歸在JavaEE開中用的還是比較少的
* 而且遞歸有一個致命的缺點:急劇消耗記憶體(建議:實際開發能不用就不用)
* 遞歸的分類:
* 直接遞歸: A方法內部調用A方法
* 間接遞歸: A方法內部調用B B方法內部調用C .... Z方法內部調用A
* 遞歸的練習:
* 遞歸的代碼演示,計算1-n之間的和,使用遞歸完成
* 1.使用遞歸: a.確定規律(分解式子) b.確定出口
* 遞歸的代碼演示: 計算第100個斐波那契數列的元素的值
* a.確定規律(分解式子) b.確定出口
* 第一百個斐波那契數列的元素的值 f(100) = f(99)+f(98)
* ...
* f(1) = 1 f(2) = 1



* 世界一個著名的演算法 漢諾塔演算法
*
* 求斐波那契數列的前20個數和
*/
1+1+2+3+5+8+12+20

public class DiGuiDemo01 {

public static int getSum1(int a,int b){ //a = 1,b = 1
if(b > ((b-1)+(b-2)) {
return sum;
}
int temp = b;
b = a + b ;



a = temp;

sum = b + getsum1(a,b);

return sum ;b + getsum1(a , b);//b = 2,a = 1;b = 3,a = 2;b = 5,
}

public static void main(String[] args) {
// TODO Auto-generated method stub
// int num = getSum(1000);
// System.out.println(num);
long num = getFBNum(30);//1 1 2 3 5 8 13 21 34 55
System.out.println(num);
}
/*
* 求第100個斐波那契數列的元素的值
*/
public static long getFBNum(int n){
if(n==1 || n == 2){
return 1;
}
return getFBNum(n-1)+getFBNum(n-2);
}


/*
* 寫一個方法求1-n的和
* 1+2+...+n=== f(n)
* 1+2+...n-1 === f(n-1)+n
* f(n) = f(n-1)+n;
* f(n-1) = f(n-2)+(n-1)
* f(n-2) = f(n-3)+n-2..
* ....
* f(2) = f(1)+2
* f(1) = 1
*
*/
public static int getSum(int n){
if(n == 1){
return 1;
}
return getSum(n-1)+n;
// int sum = 0;
// for (int i = 1; i < n+1; i++) {
// sum+=i;
// }
// return sum;
}
public static void demo01(){
System.out.println("demo01");
demo01();
}



}

import java.io.File;

/*
* 需求: 給你一個文件夾的File對象
* 要求列出該文件夾下所有文件(如果包括有文件夾,那麼要求也要列出子文件夾下的文件)
*
*/
public class FileDiGuiDemo {

public static void main(String[] args) {
// TODO Auto-generated method stub
showFiles(new File("D:\\"));
}
/*
* 定義方法:列出一個文件夾的File對象的中的文件
*/
public static void showFiles(File fileDir){//參數代表一個文件夾的File對象
if(fileDir==null || fileDir.isFile()){
System.out.println("我要的是一個文件夾的File,文件的File不行!");
return;
}
File[] files = fileDir.listFiles();
if(files==null){
return;
}
for (File file : files) {
if(file.isFile()){
System.out.println(file);
}else if(file.isDirectory()){
//是一個文件夾 那麼不管他
showFiles(file);//調用自己 ,傳入一個文件夾
}
}
// File[] files = fileDir.listFiles();
// System.out.println(files);
// System.out.println(files.length);

}

}


/*
* File中的另外一個方法:
* File[] listFiles(FileFilter filter);//列出當前文件夾下 我們要求的File對象
*
*
*/
public class FilterDemo01 {

public static void main(String[] args) {
// TODO Auto-generated method stub
//1.創建文件夾對象
File fileDir = new File("F:\\demo");
//2.調用方法
//2.1先創建一個過濾器的實現類對象
FileFilter ff = new FileFilter(){

@Override
public boolean accept(File pathname) {
// TODO Auto-generated method stub
if(pathname.getName().length() <= 6){
return true;
}
return false;
}

};

File[] files = fileDir.listFiles(ff);

System.out.println(files.length);
for (File file : files) {
System.out.println(file);
}
}


}

/*
* 策略設計模式:
* 核心思想:把邏輯和演算法分離,提高程式的擴展和靈活性.
*
* 核心技術:把方法作為參數傳遞(傳遞的一個介面的實現類)
*
* 自定義一個工具類 MyCollections 提供一個靜態方法sort用來排序數組
* 自定義一個工具類 MyCollections 提供一個靜態方法sort用來字元串數組
*
*/
public class CeLveDemo2 {
public static void main(String[] args) {
demo01();
System.out.println();
demo02();
}

public static void demo02() {
String[] strs = {"java","hello","world","heimama","baiducn","man","idaidaodo"};
MyCollections.sortString(strs, new CompareStringInterface() {

@Override
public boolean compare(String str1, String str2) {
// TODO Auto-generated method stub
if(str1.charAt(0)>str2.charAt(0)) {
return true;
}
return false;
}
});
for(String string : strs) {
System.out.print(string + " ");
}
System.out.println();

}

/*
* 排序int 數組
*
*/
public static void demo01() {
int[] num3 = {2,56,3,5674,67,43,35,67890};
MyCollections.sort(num3, new CompareInterface() {

@Override
public boolean compare(int num1, int num2) {
// TODO Auto-generated method stub
if(num1 < num2) {
return true;
}
return false;
}

});
for(int num : num3) {
System.out.print(num + " ");
}
}

}


public class MyCollections {
private MyCollections() {}
public static void sort(int[] nums,CompareInterface ci) {
//冒泡
for(int i = 0; i < nums.length -1; i++) {
for(int j = 0 ; j< nums.length - 1- i; j++) {
//交換
int temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
}
}



}
/*
* 字元串排序
*
*/
public static void sortString(String[] arr,CompareStringInterface sci) {
for(int i = 0; i < arr.length-1 ; i++) {
for(int j = 0; j < arr.length-1-i; j++) {
//比較 arr[j] arr[j+1]
if(sci.compare(arr[j], arr[j+1])) {
String str = arr[j];
arr[j] = arr[j+1];
arr[j+1] = str;
}
}
}
}




}

interface CompareInterface {
public abstract boolean compare(int num1,int num2);
}

interface CompareStringInterface {
public abstract boolean compare(String str1,String str2);
}

File file = new File("demo02.txt");

//文件的大小
System.out.println(file.getTotalSpace());

//File對象的路徑名
System.out.println(file.getPath());
//文件的絕對路徑
System.out.println(file.getAbsolutePath());

/*
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;

public class Test5 {
public static void main(String[] args) {
File file = new File("D:\\新建文件夾\\新建文件夾");
System.out.println(file);
/*
FileFilter ff = new FileFilter() {

@Override
public boolean accept(File pathname) {


//File file = new File(pathname);



return pathname.isFile() && pathname.getName().endsWith(".txt");


//return false;
}

};


File[] list = file.listFiles(ff);

for (File file2 : list) {
System.out.println(file2);
}
*/

/* File[] files = file.listFiles(new FileFilter() {
//把文件夾下麵的所有文件包括文件夾進行判斷,如果返回true則添加到File[] files數組中
@Override
public boolean accept(File pathname) {//這個File pathname : 表示的是這個是路徑的File對象
// TODO Auto-generated method stub
//File file = new File(pathname);
System.out.println(pathname);
return true;
}
});*/
/*File[] files = file.listFiles(new FilenameFilter() {

@Override
public boolean accept(File dir, String name) {
//File dir :這個是盤符的意思(也就是文件名稱以前的東西),name : 這個是文件名稱
// TODO Auto-generated method stub
File file = new File(dir,name);
return file.isFile() && file.getName().endsWith(".txt");
}
});*/

File[] files = file.listFiles(new FilenameFilter() {

@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub

System.out.println(dir);
System.out.println(name);
return true;
}
});

/*for (File file2 : files) {
System.out.println(file2);
}*/

}
}
*/

/*
File : renameTo(File file) : 重命名的方法(將原先的File對象的名字修改為一個新的File對象)

* newLine() 與 \r\n的區別
* newLine()是跨平臺的方法
* \r\n只支持的是windows系統

標準讀寫字元流操作

*將一個文本文檔上的文本反轉,第一行和倒數第一行交換,第二行和倒數第二行交換
*
* 分析:
* 1.創建輸入輸出流對象
* 2.創建集合對象
* 3.將讀到的數據存儲在集合中
* 4.倒著遍歷集合將數據寫到文件上
* 5.關流
*
* 註意事項:
* 流對象儘量晚開早關
*
*/
public class Tet1 {
public static void main(String[] args) throws IOException {
//改寫後儘量晚開早關
//1.創建輸入輸出流對象
BufferedReader br = new BufferedReader(new FileReader("xxx.txt"));

//2.創建集合對象
ArrayList<String> list = new ArrayList<>();
//3.將讀到的數據存儲在集合中
String line;
while((line = br.readLine()) != null) {
list.add(line);
}
br.close();
BufferedWriter bw = new BufferedWriter(new FileWriter("yyy.txt"));
//4.倒著遍歷集合將數據寫到文件上
for(int i = list.size() -1; i >= 0;i--) {
bw.write(list.get(i));
bw.newLine();
}
//關流

bw.close();

}
}

public class Demo5_LineNumberReader {
public static void main(String[] args) throws IOException {
//多了一個行號:可以添加行號
LineNumberReader lnr = new LineNumberReader(new FileReader("xxx.txt"));

String line;
//添加的行號以101開始的
lnr.setLineNumber(100);
while((line = lnr.readLine()) != null) {
System.out.println(lnr.getLineNumber() + ":" + line);
}

lnr.close();
}
}

/*
* 裝飾設計模式:耦合性不強,被裝飾的類的變化與裝飾類的變化無關
*
*/
public class Demo_Wrap {
public static void main(String[] args) {
HeiMaStudent hms = new HeiMaStudent(new Student());

hms.code();

}
}

interface Coder {
public void code();
}

class Student implements Coder {

@Override
public void code() {
// TODO Auto-generated method stub
System.out.println("javase");
System.out.println("javaweb");
}

}

class HeiMaStudent implements Coder {
//獲取學生引用(獲取學生對象)
//獲取被裝飾類的引用
private Student s;
//2.在構造方法中傳入被裝飾類的對象
public HeiMaStudent(Student s) {
this.s = s;
}
//3.對原有的的功能進行升級
@Override
public void code() {
// TODO Auto-generated method stub
s.code();
System.out.println("ssh");
System.out.println("資料庫");
System.out.println("大數據");
System.out.println("...");


}

}

public class Demo7_TransTo {
public static void main(String[] args) throws IOException {
//method();
//method1();
/*
* BufferedReader -- InputStreamReader(位元組流,編碼表)位元組通向字元的橋梁,通過指定的編碼表將位元組轉換為字元.---FileInputStream -- ut-8txt
* BufferedWriter -- OutputStreamWriter(位元組流,編碼表)字元通向位元組的橋梁,通過指定的編碼表將字元轉換為位元組 -- FileOutputStream -- gbk.txt
*
*
*/

//更高效的讀
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("utf-8.txt"), "utf-8"));
//更高效的寫
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("gbk.txt"), "gbk"));
int c;
while((c = br.read()) != -1) {
bw.write(c);
}
br.close();
bw.close();



}

private static void method1() throws UnsupportedEncodingException, FileNotFoundException, IOException {
//指定碼表讀字元
InputStreamReader isr = new InputStreamReader(new FileInputStream("utf-8.txt"),"uTf-8");
//指定碼表寫字元
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk.txt"), "gbk");

int c;
while((c = isr.read()) != -1) {
osw.write(c);
}
isr.close();
osw.close();
}

private static void method() throws FileNotFoundException, IOException {
//用預設編碼表讀寫,出現亂碼
FileReader fr = new FileReader("utf-8.txt");
FileWriter fw = new FileWriter("gbk.txt");
int c;
while(( c = fr.read()) != -1) {
fw.write(c);
}
fr.close();
fw.close();
}
}

/*
* 當我們下載一個試用軟體,沒有購買正版的時候,每執行一次就會體系我們換有多少次使用機會用學過的IO流只是,模擬試用版軟體
* 使用10次機會,執行一次就提示一次您換有幾次機會,如果次數到了
*
* 分析:
* 1.創建帶緩衝的輸入流對象,因為要使用readLine方法,可以保證數據的原樣性
* 2.將讀到的字元串轉換為int數
* 3.對int數進行判斷,如果大於零,就將其--寫回去,如果不大於零,就提示請購買正版
* 4.在if判斷中要將--的結果列印,並將結果通過輸出流寫到文件上
* 5.
*/
public class Test2 {
public static void main(String[] args) throws IOException {
//1.創建帶緩衝的輸入流對象,因為要使用readLine方法,可以保證數據的原樣性
BufferedReader br = new BufferedReader(new FileReader("config.txt"));
//2.將讀到的字元串轉換為int數
String line = br.readLine();
//System.out.println(line);
//將數字字元串轉換為數字
int times = Integer.parseInt(line);
//3.對int數進行判斷,如果大於零,就將其--寫回去,如果不大於零,就提示請購買正版
if(times > 0) {
//4.在if判斷中要將--的結果列印,並將結果通過輸出流寫到文件上
System.out.println("您還有" + times-- + "次機會");
FileWriter fw = new FileWriter("config.txt");
fw.write(times + "");
fw.close();


}else {
System.out.println("您的試用次數已到,請購買正版");
}
br.close();

}
}

* 遞歸:方法位元組調用自己
*
* 遞歸的弊端:不能調用次數過多,容易導致棧記憶體溢出
* 遞歸的好處:不用知道迴圈次數
*
* 構造方法用不能使用遞歸調用
*
* 遞歸調用不一定必須有返回值


* IO流對象: 數據進行讀取和寫入
* Input:數據讀取
* Output:數據寫出
*

Java中IO流分類:
按照操作數據分類
位元組流:能讀所有任意的文件
字元流:只能讀寫文本文件,((只要能用文本工具打開而且打開後還能看懂的)(記事本,notepad++等等)這些都是文本文件)
文本文件有:.txt,.java.html,.css,.其他編程語言的源碼
註意:world,excel,ppt都不是文本文件

按照數據的流向分類
輸入流:從外界設備 讀取數據 到記憶體;
輸出流:從記憶體寫數據 到外界設備.

總結:總共有四種流

1.字元輸入流:reader是根類(基類)(抽象類) 方法:read(讀一個字元,讀一個字元數組)
FileReader,BufferedReader

2.字元輸出流 :writer是根類(基類)(抽象類) 方法:writer(寫一個字元,寫一個字元數組(一部分),寫一個字元串(一部分)
FileWriter,BufferedWriter

3.位元組輸入流: InputStream(基類)(抽象類) 方法:read(一個位元組,讀一個位元組數組)
FileInputStream

4.位元組輸出流:OutputStream(基類)(抽象類) 方法:writer(寫一個位元組,寫一個位元組數組(一部分)
FileOutputStream

java中的流命名十分講究:
功能+流的基類(FileReader,OutputStream)

OutputStream:位元組輸出流根類
1.public voidwrite(int b) ; 寫一個位元組

2.

* InputStream:位元組輸入流(讀數據)
* 子類:FileInputStream
* 1.public int read();//讀取一個位元組
*
* 2.public int read(byte[] bs);//讀取一個位元組數組
*
*/
public class InputStreamDemo01 {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//1.創建位元組輸入流對象(多態)
InputStream is = new FileInputStream("a.txt");
/*
* 1.創建了一個位元組輸入流對象
* 2.如果文件不存在 那麼報錯
* 3.把流指向文件
*/
//2.一次讀取一個位元組
//int b = is.read();//0000 0000 0000 0000 0000 0000 0101 0100
//System.out.println((char)b);//0000 0000 0101 0100
//b = is.read();//0000 0000 0000 0000 0000 0000 0101 0100
//System.out.println((char)b);//0000 0000 0101 0100
//3.一次讀取一個位元組數組
byte[] bs = new byte[4];
int len = is.read(bs);
System.out.println(len);
System.out.println(new String(bs,0,len));

//再讀一次
len = is.read(bs);
System.out.println(len);
System.out.println(new String(bs,0,len));


}

}

* OutputStream:位元組輸出流根類
* 子類:FileOutputStream
* 1.public void write(int b);//寫一個位元組
* 2.public void wirte(byte[] bs);//寫一個位元組數組
*
* 3.public void wirte(byte[] bs,int start,int len);//寫一個位元組數組的一部分
*
*
* 追加問題:
* 只要創建流的時候;
* new FileOutputStream("a.txt",true);
* 換行問題:
* windows: \r\n
* Linux: \n
* Mac Os: \r
*/
public class OutputStreamDemo01 {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//1.創建一個文件的位元組輸出流(多態的方式)
OutputStream os = new FileOutputStream("a.txt");
/*
* 1.創建了一個位元組輸出流對象
* 2.如果文件不存在,創建這個文件對象
* 3.讓這個流指向這個文件
*/
//2.調用寫數據方法 write
//寫一個位元組
// os.write(106);
// os.write(97);
// os.write(118);
// os.write(97);
//寫一個位元組數組 ILOVEYOUTIGER
// byte[] bs = {106,97,118,97,97,97};
//byte[] bs = "ILoveJavaEE".getBytes();
//寫"中國我愛你"
// byte[] bs1 = "中國我愛你".getBytes();
// System.out.println( bs1.length);
// System.out.println(bs1[0]);
// System.out.println(bs1[1]);
// os.write(bs1);
//寫一個位元組數組的一部分
// byte[] bs = "中國我愛你".getBytes();
// os.write(bs, 0, 6);
for(int i = 0;i< 10;i++){
os.write("我愛你中國".getBytes());
os.write("\r\n".getBytes());
}

//3.關閉流
os.close();

}

}

/*
* 位元組流複製文件(可以複製任意文件) C:\Users\yingpeng\Desktop\pictures\3.png
*
* 1.源文件 : 3.png---->FileInputStream
* 2.目標文件: copy.png---->FileOutputStream
*
*
*
*/
public class CopyFileDemo01 {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//1.創建流對象(2個,一個讀 一個寫)
FileInputStream fis = new FileInputStream("C:\\Users\\yingpeng\\Desktop\\pictures\\3.png");
FileOutputStream fos = new FileOutputStream("copy.png");
//2.一邊讀取數據 一邊寫入數據
//a.一次讀取一個位元組 寫入一個位元組
// int b = fis.read();
// fos.write(b);
//標準代碼==================
// int b = 0;
// while( (b = fis.read())!=-1){
// fos.write(b);
// }
//b.一次讀取一個位元組數組
long s = System.currentTimeMillis();
//標準代碼
byte[] bs = new byte[1024];//保存讀取到的數據
int len = 0;//保存實際讀取到的位元組數
while( (len = fis.read(bs))!=-1 ){
fos.write(bs,0,len);
}
//3.釋放資源
fos.close();
fis.close();

long e = System.currentTimeMillis();
System.out.println(e-s);
}

}


* FileReader---->BufferedReader
* FileWriter---->BufferedWriter
*
* FileInputStream---->BufferedInputStream
* FileOutputStream----->BufferedOutputStream
*
* 1.BufferedOutputStream:位元組緩衝輸出流(寫數據)
* public BufferedOutputStream(OutputStream out)
* a.public void write(int b);
* b.public void write(byte[] bs);
* c.public void wirte(byte[] bs,int start,int len);
*
*
*/
public class BufferedDemo01 {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//1.創建位元組緩衝輸出流對象
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("b.txt"));
//2.調用bos的寫數據方法
//寫一個位元組
// bos.write(49);
// bos.write(48);
// bos.write(48);
//寫一個位元組數組
// byte[] bs = {49,48,48,48};
// bos.write(bs);
//寫 "helloworld"
// byte[] bs = "helloworld".getBytes();
// bos.write(bs);
byte[] bs = "鳳a姐".getBytes();
System.out.println(bs.length);
bos.write(bs,2,3);
//3.關閉流
bos.close();

}

}

/*
* BufferedInputStream-->位元組緩衝輸入流(讀數據)
*
* 構造:
* public BufferedInputStream(InputStream in)
*
* 練習:使用BufferedInputStream讀取一個txt文件列印到控制台
*/
public class BufferedInputStreamDemo {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//1.創建位元組緩衝輸入流對象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("ArrayList.java"));
//a.一次讀取一個位元組
// int b = 0;//保存讀取到位元組
// while((b = bis.read())!=-1){
// System.out.print((char)b);
// }
//b.一次讀取一個位元組數組
int len = 0;//實際讀取的個數
byte[] bs = new byte[1024];
while((len=bis.read(bs))!=-1){
System.out.print(new String(bs,0,len));
}

//3.關閉
bis.close();

}
public static void demo01() throws IOException{
//1.創建位元組緩衝輸入流對象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("1.txt"));
//調用bis的讀數據方法
//a.一次讀取一個位元組
// int b = bis.read();
// System.out.println((char)b);
//b.一次讀取一個位元組數組
// byte[] bs = new byte[2];
// int len = bis.read(bs);
// System.out.println(len);
// System.out.println(new String(bs,0,len));
//
// len = bis.read(bs);
// System.out.println(len);
// System.out.println(new String(bs,0,len));
//3.關閉
bis.close();

}
}



*/

/*
/*
* 記憶體輸出流:該輸出流可以向記憶體中寫數據,把記憶體當作一個緩衝區,寫出之後
*
* FileInputStream讀取中文的時候會出現亂碼
*
* 解決方案:
* 1.使用字元流
*
* InputStream:位元組輸入流(讀數據)
* 子類:FileInputStream
* 1.public int read();//讀取一個位元組
*
* 2.public int read(byte[] bs);//讀取一個位元組數組
*
*/
public class InputStreamDemo01 {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//1.創建位元組輸入流對象(多態)
InputStream is = new FileInputStream("a.txt");
/*
* 1.創建了一個位元組輸入流對象
* 2.如果文件不存在 那麼報錯
* 3.把流指向文件
*/
//2.一次讀取一個位元組
//int b = is.read();//0000 0000 0000 0000 0000 0000 0101 0100
//System.out.println((char)b);//0000 0000 0101 0100
//b = is.read();//0000 0000 0000 0000 0000 0000 0101 0100
//System.out.println((char)b);//0000 0000 0101 0100
//3.一次讀取一個位元組數組
byte[] bs = new byte[4];
int len = is.read(bs);
System.out.println(len);
System.out.println(new String(bs,0,len));

//再讀一次
len = is.read(bs);
System.out.println(len);
System.out.println(new String(bs,0,len));


}

}



*/
public class DmeoByteArrayOutputStream {
public static void main(String[] args) throws IOException {
//emthod();
//在記憶體中創建了可以增長的記憶體數組
FileInputStream fis = new FileInputStream("e.txt");
//ByteArrayOutputStream不用關流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int b;
//將讀取到的數據逐個寫到記憶體中
while ((b = fis.read()) != -1) {
baos.write(b);
}
//
//將緩衝區的數據全部獲取出來,並賦值給arr數組
//他可以使用指定的編碼表轉換
byte[] arr = baos.toByteArray();
System.out.println(new String(arr));
//使用的是預設的編碼表進行轉換輸出
//將緩衝區的內容轉換為了字元串,在輸出語句中可以省略調用toString()方法
System.out.println(baos.toString());
fis.close();


}

//使用字元流解決問題中文輸入問題
private static void emthod() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("e.txt");
byte[] arr = new byte[3];
int len;
while((len = fis.read(arr)) != -1) {
System.out.println(new String(arr,0,len));
}

fis.close();
}
}

/*

public class Test {
public static void main(String[] args) throws IOException {
//創建流對象(2個,一個讀,一個寫)
//一次讀取一個位元組數組
FileInputStream fis = new FileInputStream("韓雪 - 想起.mp3");
FileOutputStream fos = new FileOutputStream("Copy.mp3");
//標準代碼
//保存讀到到的數據
byte[] bs = new byte[1024];
//保存實際讀取到的位元組數
int len;
while((len = fis.read(bs)) != -1) {
fos.write(bs,0,len);
}
//3.釋放資源
fos.close();
fis.close();



}
}

* 複製單級文件夾:
*
* 0.創建目標文件夾(如果不存在) F:\\test
*
* 1.創建一個File對象(代表要複製的源文件夾) E:\\demo
*
* 2.調用File對象的listFiles方法 獲取文件夾中所有的文件對象
*
* 3.挨個複製文件對象 到 目標文件夾中
* 源文件 E:\\demo\\1.txt---> 目標文件 F:\\test\\1.txt
*/
public class CopyDirectoryDemo {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//0.創建目標文件夾(如果不存在) F:\\test
File fileDest = new File("F:\\test");
if(!fileDest.exists()){
fileDest.mkdir();
}
//1.創建源文件夾的File對象
File fileSrc = new File("E:\\demo");
//調用fileSrc的listFiles
File[] files = fileSrc.listFiles();
for (File file : files) {
System.out.println(file);//源文件 E:\demo\1.txt
File newFile = new File(fileDest,file.getName());//目標文件 F:\\test\\1.txt
System.out.println(newFile);
//複製文件 從file 複製到newFile
copyFile(file,newFile);
}
}

public static void copyFile(File file, File newFile) throws IOException {
// TODO Auto-generated method stub
//複製文件 源文件 file 目標文件 newFile
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));

//迴圈讀取文件 寫入文件
int len = 0;
byte[] bs = new byte[1024];
while((len= bis.read(bs))!=-1){

bos.write(bs, 0, len);
}
//關閉流
bis.close();
bos.close();
System.out.println("複製文件"+file.getAbsolutePath()+"成功!");
}

}


*
* 求前n個斐波那契數列的元素之和
* 1.求第n個斐波那契數列的元素
* f(n) = f(n-1)+f(n-2)
* f(2) = 1
* f(1) = 1
* 2.求前n個斐波那契數列的元素之和
* sum(n) = sum(n-1)+ getFBNum(n)
* sum(1) = 1
*
* 留一個題:
* 求前n個數的和
* 1 1+2 1+2+3 1+2+3+4 1+2+3+4+5

第n個數為
1 3 6 10 15
*
*/
public class DiGuiDemo {

public static int sum(int n) {
if(n == 1) {
return 1;
}
return n + sum(n-1); //n = 3,(3+2+1)
}

public static int getSum(int n) {
if(n == 1) {
return 1;
}
return sum(n) + getSum(n-1);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
//測試
// int num = getFBNum(5);
// System.out.println(num);
int sum = getSum(6);//1 1 2 3 5 8
System.out.println(sum);
}
/*
* 求第n個斐波那契數列的元素
*/
public static int getFBNum(int n){
if(n==1){
return 1;
}
if(n==2){
return 1;
}
return getFBNum(n-1)+getFBNum(n-2);
}

/*
*前n個元素之和
*/
public static int getSum(int n){
if(n==1){
return 1;
}
//sum(n) = sum(n-1)+ getFBNum(n)
return getSum(n-1)+getFBNum(n);
}
}


* Properties類:是Map介面的一個實現,具有map介面下的所有功能
* 沒有泛型,Key和Value都是String類型
* 1.put(String key,String value)---->setProperty(String key,String value)
* 2.get(String key)---->getProperty(String key);
* 3.刪除方法 還是使用 map介面中的remove方法
*
* 4.keySet();//以鍵找值 ----->Set<String> stringPropertyNames();
* 5.entrySet();//鍵值對 只要map中有
*
* 持久屬性集的用法:
* public void store(OutputStream out, String comments);
* 把屬性集Properties對象中數據保存到 一個輸入流中
* public void load(InputStream inStream);
* 從輸入流中載入數據
*/
public class PropertiesDemo {
public static void main(String[] args) throws IOException {
// //1.創建一個Properties對象
// Properties ps = new Properties();
// //2.添加數據
// ps.setProperty("zhangsan", "18");
// ps.setProperty("lisi", "28");
// ps.setProperty("rose", "38");
// ps.setProperty("jack", "48");
// //3.保存數據
// ps.store(new FileOutputStream("abc.properties"), "");

//4.從輸入流中載入數據
Properties ps = new Properties();
System.out.println(ps);
ps.load(new FileInputStream("abc.properties"));
System.out.println(ps);

}
/*
* 基本用法
*/
public static void demo01(){
//1.創建一個Properties對象
Properties ps = new Properties();
//2.添加數據
ps.setProperty("zhangsan", "18");
ps.setProperty("lisi", "28");
ps.setProperty("rose", "38");
ps.setProperty("jack", "48");
//System.out.println(ps);
// String value = ps.getProperty("jack");
// System.out.println(value);
//遍歷ps集合
Set<String> namesSet = ps.stringPropertyNames();
//foreach或者迭代器
for (String name : namesSet) {
String value = ps.getProperty(name);
System.out.println(name+"="+value);
}
}
}

/*
* 一個文件夾複製到另外一個盤符下(這個被覆制的文件夾裡面有還有文件夾,文件夾裡面還有文件.)
* 複製多個文件夾以及內容進行複製
*
* 分析:
* 1.創建新的文件路徑(這個是需要複製到的位置)對象,再創建一個路徑對象
* 2.定義方法,這個方法裡面判斷當前文件夾下麵所有的文件,
* 3.如果是文件用方法進行複製,
* 4.如果是文件夾就用新的file對象去封裝成一個新的盤符路徑,再把新的盤符路徑和這個迴圈出來的文件進行遞歸
*
*/
public class Test5 {
public static void main(String[] args) throws IOException {
File file = new File("F:\\demo");
File newFile = new File("E:\\dmeo2");
method(file,newFile);
}
//用方法進行遞歸判斷尋找文件並創建文件夾
public static void method(File file,File newFile) throws IOException {
File[] files = file.listFiles();
for (File file2 : files) {
if(file2.isDirectory()) {
File file3 = new File(newFile,file2.getName());
file3.mkdirs();
method(file2,file3);
}else {
File file4 = new File(newFile,file2.getName());
copy(file2,file4);
}
}

}
//用方法進行文件複製
private static void copy(File file2, File file4) throws IOException {
// TODO Auto-generated method stub
FileInputStream fis = new FileInputStream(file2);
FileOutputStream fos = new FileOutputStream(file4);
byte[] b = new byte[1024];
int i;
while((i = fis.read(b)) != -1) {
fos.write(b, 0, i);
}
fis.close();
fos.close();


}
}

UTF-8:字母代表一個位元組,漢子代表三個位元組

GBK : 字母是一個位元組,漢子是兩個位元組

public static void main(String[] args) throws IOException {
//1.封裝源目錄:
File srcDir = new File("D:\\單級目錄");
//2.封裝目標目錄:
File destDir = new File("E:\\");
//3.判斷目標目錄下是否包含要複製的目錄:
destDir = new File(destDir,srcDir.getName());//new File("E:\\單級目錄");
if(!destDir.exists()){
destDir.mkdir();//創建目錄
}
//4.獲取源目錄下所有子文件的File數組;
File[] fileArray = srcDir.listFiles();
if(fileArray!= null){
//5.遍曆數組,獲取每個File對象;
for(File f : fileArray){
//針對每個File對象創建獨立的輸入流(位元組流)
FileInputStream in = new FileInputStream(f);
//並向目標位置創建獨立的輸出流(位元組流)
FileOutputStream out = new FileOutputStream(new File(destDir,f.getName()));
//開始複製-->一次複製一個位元組數組
byte[] byteArray = new byte[1024 * 8];
int len = 0;
while((len = in.read(byteArray)) != -1){
out.write(byteArray,0,len);
}
out.close();
in.close();
}

}
System.out.println("複製完畢!");
}
<上一題

public static void main(String[] args) throws Exception {
//創建源目錄
File file = new File("D:/java");
//獲取文件
File[] files = file.listFiles();
//創建輸入流對象
InputStream in = null;
//創建輸出流對象
OutputStream out = null;
//記錄源文件夾中文件的名稱
String fileName = null;
// 記錄 目標文件夾中文件的名稱
String destName = null;
//指定目標文件夾
File f = new File("D:/jad");
//遍歷
for (File file2 : files) {
in = new FileInputStream(file2);
//查看 目標文件夾 是否存在
if(!f.exists()){
f.mkdir();
}
//獲取源文件夾中文件的名稱
fileName = file2.getName();
destName = fileName;
//創建複製的文件輸出流
out = new FileOutputStream(new File("D:/jad/"+destName));
int len = 0;
byte[] b = new byte[1024];
//完成複製
while((len = in.read(b))!= -1){
out.write(b, 0, len);
//System.out.println(new String(b,0,len));
}
}
}
<上一題

1.刪除目錄主要用到的知識:File類和遞歸。
2.本題的完整代碼為:
public class Test {
public static void main(String[] args) {
delete(new File("D:/a"));
}

public static void delete(File f) {
// 判斷是否是文件夾
if (f.isDirect

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • >>,>>>,<<的用法: * <<:左移 左邊最高位丟棄,右邊補齊0 * >>:右移 最高位是0,左邊補齊0;最高為是1,左邊補齊1 * >>>:無符號右移 無論最高位是0還是1,左邊補齊0 代碼: 結果: ...
  • 源文件的字元編碼 預設情況下,Python 源碼文件以 UTF 8 編碼方式處理。如果不使用預設編碼,要聲明文件所使用的編碼,源碼文件的 第一行要寫成特殊的註釋。語法如下所示: 其中 encoding 可以是 Python 支持的任意一種 codecs。比如,要聲明使用 Windows gbk 編碼 ...
  • 記錄 2019-07-06: Python是一門解釋型語言,擁有許多強大的標準庫,是完全面向對象語言 如果需要一段關鍵代碼運行得更快或者希望某些演算法不公開,可以把部分程式用c或c++編寫,然後在python程式中使用它們 缺點: 運行速度慢 國內市場較小 中文資料匱乏 中文資料匱乏 可以使用任意文本 ...
  • 一、 偏函數 二、 1.定義:參數固定的函數,相當於一個有特定參數的函數體。 2.格式:functools.partial(函數,固定參數) 3.返回值:把一個函數某些參數固定,返回一個新的函數。 二、zip函數 1.定義:把兩個可迭代的內容生成一個可以迭代的tuple元素組成的內容 2.格式:zi ...
  • 9-7 管理員: 管理員是一種特殊的用戶。編寫一個名為Admin的類,並讓它繼承練習9-3或者9-5的User類。添加一個名為privileges的屬性,用於存儲一個由字元串(如"can add post"、"can delete post"、"can ban user"等)組成的列表。編寫一個名為 ...
  • 開發環境: Windows操作系統開發工具:MyEclipse/Eclipse + JDK+ Tomcat + MySQL 資料庫項目截圖: 獲取源碼請聯繫博主-Q:782827013 ...
  • 世界是物質的,物質是運動的,運動是有規律的,規律是可以被認識的 二項式反演 $$ g_n=\sum_{i=0}^n \binom{n}if_i\Rightarrow f_n=\sum_{i=0}^n( 1)^{n i}\binom{n}ig_i $$ 證明如下 $$ \begin{aligned} ...
  • 博主最近在學習加法器、乘法器、IEEE的浮點數標準,作為數字IC的基礎。當看到booth編碼的乘法器時,對booth編碼不是很理解,然後在網上找各種理解,終於豁然開朗。現將一個很好的解釋分享給大家,希望能對大家有所幫助。 首先,看看這幾個公式: 可以證明的是,這三個公式是相等的,一個有符號的二進位數 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...