01 復習
文件夾刪除
public class Demo01
{
public static void main(String[] args) {
deleteFile(new File("D:/abc"));
}
public static void deleteFile(File file){
if (file.isFile()){
file.delete();//刪除文件
? ? ? ? }
if (file.isDirectory()){
File[] files = file.listFiles();
for (File f : files) {
deleteFile(f);
}
file.delete();//刪除文件夾
? ? ? ? }
}
}
02 IO流常見流父類復習
頂級父類們
03輸出流的使用步驟
輸出流:頂層父類是OutputStream【抽象類】,借助其子類FileOutputStream學習
輸出流的使用步驟:
1.創(chuàng)【創(chuàng)建流對象】
FileOutputStream(File file):創(chuàng)建文件輸出流以寫入由指定的File對象表示的文件裹芝。
public FileoutputStream(String name):創(chuàng)建文件輸出流以指定的名稱寫入文件衙猪。
【如果原來文件存在數(shù)據(jù),會清空,如果沒有文件會創(chuàng)建一個】
2 寫【調用寫數(shù)據(jù)的方法】
write方法:
public void write(int b):寫出一個字節(jié)
public void write(byte[] bytes):寫出一個字節(jié)數(shù)組
public void write(byte[] bytes,int off,int len)
3:關【強調關流的方法】
public void close()
public class Demo2
{
public static void main(String[] args)throws IOException {
FileOutputStream fos=new FileOutputStream(new File("file01.txt"));
fos.write(98);
byte[] bytes ="abc我愛java".getBytes();
fos.write(bytes);
fos.write(bytes,0,2);
fos.close();
}
}
04 寫出換行及文件數(shù)據(jù)追加功能
如果要完成文件數(shù)據(jù)追加續(xù)寫
創(chuàng)建流時需要使用一下兩個構造方法:
public FileOutputStream(File file,boolean append):創(chuàng)建文件輸出流以寫入由指定的File對象表示的文件。
public FileOutputStream(File file,boolean append):創(chuàng)建文件輸出流以寫入由指定的File對象表示的文件。
public FileOutputStream(String name,boolean append):創(chuàng)建文件輸出流以指定的名稱寫入文件榛了。
如果要完成文件數(shù)據(jù)的追加,append參數(shù)要設置為true
寫出換行
\r\n
\r
\n
String ls=System.lineSeparator
06字節(jié)輸入流的使用
輸入流頂層父類是:InputStream【抽象類】可以使用FileInputStream
使用步驟:
1)創(chuàng)【創(chuàng)建輸入流對象】
FileInputStream的構造方法
public FileInputStream(File file):關聯(lián)要讀取的文件
public FileInputStream(String filePath):關聯(lián)要讀取的文件路徑
要求指定的文件煞抬,一定要存在霜大,否則會報錯
【FileNotFoundException】
2)讀【讀取數(shù)據(jù)】
read:
public int read():讀取一個字節(jié)并返回,如果讀取到末位革答,會返回-1
public int read(byte[] bytes):讀取多個數(shù)據(jù)到字節(jié)數(shù)組战坤,返回讀取有效的字節(jié)個數(shù),如果沒有讀到數(shù)據(jù)返回-1
3)關【關閉資源】
public void close()
07 文件的復制
低效復制:int read()void write()
/*
把dir1中文件beauty.jpg復制到dir2中去
思路:先把dir1中的beauty.jpg讀取到內存残拐,然后寫入到dir2中的beauty.jpg
步驟:
1)創(chuàng):【創(chuàng)建輸入輸出流】
2)讀途茫,寫【邊讀邊寫】
3)關【關輸入和輸出流的資源】
public class Demo01{
publicstaticvoidmain(String[]args)throwsIOException{
longt1=System.currentTimeMillis();
//1)創(chuàng):【創(chuàng)建輸入輸出流】
FileInputStreamfis=newFileInputStream("dir1/beauty.jpg");
FileOutputStreamfos=newFileOutputStream("dir2/beauty.jpg");
//2)讀,寫【邊讀邊寫】
intb;
while((b=fis.read())!=-1) {
fos.write(b);
? ? ?? }
?
//3)關【關輸入和輸出流的資源】? 先開后關
fos.close();
fis.close();
?
longtimeCost=System.currentTimeMillis()-t1;
System.out.println("timeCost = "+timeCost);
?? }
}
高效的復制(int read(byte[] bytes) void write(byte[] bytes,int off,int len))
/*
? ? 把dir1中文件beauty.jpg復制到dir2中去
? ? 思路:先把dir1中的beauty.jpg讀取到內存溪食,然后寫入到dir2中的beauty.jpg
步驟:
1)創(chuàng):【創(chuàng)建輸入輸出流】
2)讀囊卜,寫【邊讀邊寫】
3)關【關輸入和輸出流的資源】
*/
public class Demo02 {
? ? public static void main(String[] args) throws IOException {
? ? ? ? long t1 = System.currentTimeMillis();
? ? ? ? //1)創(chuàng):【創(chuàng)建輸入輸出流】
? ? ? ? FileInputStream fis = new FileInputStream("dir1/beauty.jpg");
? ? ? ? FileOutputStream fos = new FileOutputStream("dir2/beauty5.jpg");
? ? ? ? //2)讀,寫【邊讀邊寫】
? ? ? ? int len;
? ? ? ? byte[] bytes = new byte[1024*8];
? ? ? ? while ((len = fis.read(bytes)) != -1) {
? ? ? ? ? ? fos.write(bytes,0,len); //寫出有效數(shù)據(jù)
? ? ? ? }
? ? ? ? //3)關【關輸入和輸出流的資源】? 先開后關
? ? ? ? fos.close();
? ? ? ? fis.close();
? ? ? ? long timeCost = System.currentTimeMillis() - t1;
? ? ? ? System.out.println("timeCost = " + timeCost);
? ? }
}
08字符輸入流的使用
字符的輸入流:Reader【抽象類】错沃,得其子類 FileReader
使用步驟:
1)創(chuàng)
? ? FileReader的構造方法
? ? - FileReader(File file): 創(chuàng)建一個新的 FileReader 栅组,給定要讀取的File對象。
? ? - FileReader(String fileName): 創(chuàng)建一個新的 FileReader 枢析,給定要讀取的文件的名稱玉掸。
? ? 如果文件不存在,會報錯FileNotFoundException
2)讀
? ? read:
? ? public int read():讀取一個字符醒叁,如果讀取到末位司浪,返回-1
? ? public int read(char[] chars) :讀取有效的字符到字符數(shù)組,返回讀取字符的有效個數(shù)辐益,如果沒有字符返回-1
3)關
? ? public void close()
*/
public class Demo01{
public static void main(String[]args)throwsIOException{
//1:創(chuàng)
FileReaderfr=newFileReader("file05.txt");
//2:讀
?
/* // public int read():讀取一個字符断傲,如果讀取到末位,返回-1
int c;//保存讀取的字符
while ((c = fr.read()) != -1) {
System.out.println((char)c);
}*/
?
// public int read(char[] chars) :讀取有效的字符到字符數(shù)組智政,返回讀取字符的有效個數(shù)认罩,如果沒有字符返回-1
intlen;
char[]chars=newchar[2];
while((len=fr.read(chars))!=-1) {
System.out.println(newString(chars,0,len));
? ? ?? }
//3:關
fr.close();
?
?? }
}
10字符的輸出流使用
/*
字符輸出流:Writer【抽象類】,得用其子類FileWriter
?
使用步驟:
1)創(chuàng)
FileWriter的構造方法
public FileWriter(File file): 關聯(lián)一個文件用來保存寫出的數(shù)據(jù)
public FileWriter(String filePath): 關聯(lián)一個文件用來保存寫出的數(shù)據(jù)
?
//如果append設置為true续捂,具有數(shù)據(jù)追加拼接的功能
public FileWriter(File file ,boolean append): 關聯(lián)一個文件用來保存寫出的數(shù)據(jù)
public FileWriter(String filePath, boolean append): 關聯(lián)一個文件用來保存寫出的數(shù)據(jù)
?
2)寫
write:
public void write(int c):寫出一個字符
public void write(char[] chars):寫出一個字符數(shù)組
public void write(char[] chars,int off,int len):寫出一個字符數(shù)組中指定字符
public void write(String str):直接寫出字符串
?
3)關
public void close()
?
*/
publicclassDemo01{
publicstaticvoidmain(String[]args)throwsIOException{
//1:創(chuàng)
FileWriterfw=newFileWriter("file06.txt");
//2:寫
fw.write('A');//寫出一個字符
char[]chars={'a','b','c'};
fw.write(chars);//寫出一個字符數(shù)組
fw.write(chars,0,2);//寫出字符數(shù)組中指定的字符
fw.write("HelloWorld");//寫出字符串
fw.write("我愛Java",0,2);//寫出字符串中指定的字符
//3:關
fw.close();
?? }
}
11 Properties屬性類的使用
/*
Properties屬性類垦垂,最終會實現(xiàn)Map,本質上就是Map特點就是保存鍵值對信息
用來存儲:屬性配置信息【鍵值對】
downloadPath=D:/download
?
1)創(chuàng)建對象
2)使用方法
- public Object setProperty(String key, String value) : 保存一對屬性牙瓢。 ? put
- public String getProperty(String key) :使用此屬性列表中指定的鍵搜索屬性值劫拗。 get
- public Set<String> stringPropertyNames() :所有鍵的名稱的集合。 keySet
?
從流中讀取
- public void load(InputStream in): 從字節(jié)輸入流中讀取屬性文件矾克。
- public void load(Reader reader): 從字符流中讀取屬性文件
存入到流中
- public void store(OutputStream out,String comment): comment是注釋
- public void store(Writer writer, String comment):
?
?
*/
publicclassDemo01{
publicstaticvoidmain(String[]args)throwsIOException{
?
Propertiespro=newProperties();
pro.setProperty("downloadPath1","D:/download");
pro.setProperty("downloadPath2","D:/music");
pro.setProperty("downloadPath3","D:/movie");
System.out.println("pro = "+pro);
?
StringaaaValue=pro.getProperty("aaa","沒有配置信息");
System.out.println("aaaValue = "+aaaValue);
StringdownloadPath=pro.getProperty("downloadPath");
System.out.println("downloadPath = "+downloadPath);
?
Set<String>keySet=pro.stringPropertyNames();
System.out.println("keySet = "+keySet);
?
// 存入到流中:存儲到指定的路徑
//- public void store(OutputStream out,String comment): comment是注釋
//- public void store(Writer writer, String comment):
pro.store(newFileWriter("downloadInfo.properties"),"this is comment");
?? }
}
/*
從流中讀取
- public void load(InputStream in): 從字節(jié)輸入流中讀取屬性文件页慷。
- public void load(Reader reader): 從字符流中讀取屬性文件
?
*/
publicclassDemo02{
publicstaticvoidmain(String[]args)throwsIOException{
Propertiespro=newProperties();
System.out.println("pro = "+pro);
?
pro.load(newFileReader("downloadInfo.properties"));
?
System.out.println("pro = "+pro);
?
?
?? }
}