一、字節輸出流
1.IO流介紹
input:輸入
output:輸出
2.字節輸出流
OutputStream:字節輸出流的頂層抽象父類
FileOutputStream:字節輸出流子類
3.FileOutputStream
構造方法:
FileOutputStream(File name); 根據File對象創建字節輸出流對象
FileOutputStream(File name,boolean append); 根據File對象創建字節輸出流對象,如果布爾類型參數爲true,可以實現追加續寫的功能
FileOutputStream(String name); 根據字符串路徑創建字節輸出流對象
FileOutputStream(String name,boolean append); 根據字符串路徑創建字節輸出流對象,如果布爾類型參數爲true,可以實現追加續寫的功能
(tips:字節輸出流的構造方法(不帶布爾類型參數)創建對象,如果文件不存在,將會自動創建出來。如果文件存在,則將內容清空)
成員方法:
write(int b); 一次寫一個字節
write(byte[] arr); 一次寫一個字節數組
write(byte[] arr,int index,int count); 一次寫出一個字節數組一部分
close();
釋放資源
4.使用字節輸出流寫出數據到文件中
public class Demo01OutputStream {
public static void main(String[] args) throws IOException {
//1.創建一個FileOutputStream對象,構造方法中傳遞寫入數據的目的地
FileOutputStream fos = new FileOutputStream("d:\\file.txt");
//2.調用FileOutputStream對象中的方法write,把數據寫入到文件中
//public void write(int b) :將指定的字節輸出流。
fos.write(97);
//3.釋放資源(流使用會佔用一定的內存,使用完畢要把內存清空,提供程序的效率)
fos.close();
}
}
5.使用字節流的其他寫出方法
public class Demo02OutputStream {
public static void main(String[] args) throws IOException {
//創建FileOutputStream對象,構造方法中綁定要寫入數據的目的地
FileOutputStream fos = new FileOutputStream(new File("d:\\b.txt"));
//調用FileOutputStream對象中的方法write,把數據寫入到文件中
//在文件中顯示100,寫個字節
/*fos.write(49);
fos.write(48);
fos.write(48);*/
/*
public void write(byte[] b):將 b.length字節從指定的字節數組寫入此輸出流。
一次寫多個字節:
如果寫的第一個字節是正數(0-127),那麼顯示的時候會查詢ASCII表
如果寫的第一個字節是負數,那第一個字節會和第二個字節,兩個字節組成一箇中文顯示,查詢系統默認碼錶(GBK)
*/
//byte[] bytes = {65,66,67,68,69};//ABCDE
//byte[] bytes = {-65,-66,-67,68,69};//烤紻E
//fos.write(bytes);
/*
public void write(byte[] b, int off, int len) :把字節數組的一部分寫入到文件中
int off:數組的開始索引
int len:寫幾個字節
*/
//fos.write(bytes,1,2);//BC
/*
寫入字符的方法:可以使用String類中的方法把字符串,轉換爲字節數組
byte[] getBytes() 把字符串轉換爲字節數組
*/
byte[] bytes2 = "你好".getBytes();
System.out.println(Arrays.toString(bytes2));//[-28, -67, -96, -27, -91, -67]
fos.write(bytes2);
//釋放資源
fos.close();
}
}
6.寫出換行和實現文件續寫的功能
public class Demo03OutputStream {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("09_IOAndProperties\\c.txt",true);
for (int i = 1; i <=10 ; i++) {
fos.write("你好".getBytes());
fos.write("\r\n".getBytes()); //換行
}
fos.close();
}
}
7.寫出一首溼到文件中
public class Demo01 {
public static void main(String[] args) throws IOException {
//創建字節輸出流對象
FileOutputStream fos = new FileOutputStream("d:\\溼.txt");
fos.write("故人西辭富士康".getBytes());
fos.write("\r\n".getBytes());
fos.write("爲學技術到鳥內".getBytes());
fos.write("\r\n".getBytes());
fos.write("鳥內畢業包分配".getBytes());
fos.write("\r\n".getBytes());
fos.write("尼瑪還是富士康".getBytes());
fos.close();
System.out.println("寫出完畢");
}
}
二、字節輸入流
1.涉及到的類
InputStream 字節輸入流頂層抽象父類
FileInputStream 文件字節輸入流
2.構造方法
FileInputStream(File name); 根據File對象創建一個字節輸入流對象
FileInputStream(String name); 根據字符串路徑創建一個字節輸入流對象
3.成員方法
int read(); 一次讀取一個字節數
int read(byte[] arr); 一次讀取一個字節數組大小的數據,將這些數據保存到了傳入的字節數組中
void close(); 釋放資源
4.使用字節輸入流讀取數據
public class Demo01InputStream {
public static void main(String[] args) throws IOException {
//1.創建FileInputStream對象,構造方法中綁定要讀取的數據源
FileInputStream fis = new FileInputStream("d:\\abc\\1.txt");
//2.使用FileInputStream對象中的方法read,讀取文件
//int read()讀取文件中的一個字節並返回,讀取到文件的末尾返回-1
/*
發現以上讀取文件是一個重複的過程,所以可以使用循環優化
不知道文件中有多少字節,使用while循環
while循環結束條件,讀取到-1的時候結束
布爾表達式(len = fis.read())!=-1
1.fis.read():讀取一個字節
2.len = fis.read():把讀取到的字節賦值給變量len
3.(len = fis.read())!=-1:判斷變量len是否不等於-1
*/
int len = 0; //記錄讀取到的字節
while( (len = fis.read()) != -1){
System.out.print((char)len);//abc
}
//3.釋放資源
fis.close();
}
}
5.使用字節輸入流一次讀取多個數據
public class Demo02InputStream {
public static void main(String[] args) throws IOException {
//創建FileInputStream對象,構造方法中綁定要讀取的數據源
/*
b.txt: A B C D E -1
*/
FileInputStream fis = new FileInputStream("09_IOAndProperties\\b.txt");
//使用FileInputStream對象中的方法read讀取文件
//int read(byte[] b) 從輸入流中讀取一定數量的字節,並將其存儲在緩衝區數組 b 中。
/*
發現以上讀取時一個重複的過程,可以使用循環優化
不知道文件中有多少字節,所以使用while循環
while循環結束的條件,讀取到-1結束
*/
byte[] bytes = new byte[1024];//存儲讀取到的多個字節
int len = 0; //記錄每次讀取的有效字節個數
while( (len = fis.read(bytes)) != -1){
//String(byte[] bytes, int offset, int length) 把字節數組的一部分轉換爲字符串 offset:數組的開始索引 length:轉換的字節個數
System.out.println(new String(bytes,0,len));
}
//釋放資源
fis.close();
}
}
6.文件複製
public class Demo01CopyFile {
public static void main(String[] args) throws IOException {
long s = System.currentTimeMillis();
//1.創建一個字節輸入流對象,構造方法中綁定要讀取的數據源
FileInputStream fis = new FileInputStream("c:\\視頻\\頸椎操.flv");
//2.創建一個字節輸出流對象,構造方法中綁定要寫入的目的地
FileOutputStream fos = new FileOutputStream("d:\\頸椎操.flv");
//一次讀取一個字節寫入一個字節的方式
//3.使用字節輸入流對象中的方法read讀取文件 62634毫秒
/*int len = 0;
while((len = fis.read())!=-1){
//4.使用字節輸出流中的方法write,把讀取到的字節寫入到目的地的文件中
fos.write(len);
}*/
//使用數組緩衝讀取多個字節,寫入多個字節
byte[] bytes = new byte[1024];
//3.使用字節輸入流對象中的方法read讀取文件 571毫秒
int len = 0;//每次讀取的有效字節個數
while((len = fis.read(bytes))!=-1){
//4.使用字節輸出流中的方法write,把讀取到的字節寫入到目的地的文件中
fos.write(bytes,0,len);
}
//5.釋放資源(先關寫的,後關閉讀的;如果寫完了,肯定讀取完畢了)
fos.close();
fis.close();
long e = System.currentTimeMillis();
System.out.println("複製文件共耗時:"+(e-s)+"毫秒");
}
}
二、字符流(只能操作文本文件)
1.字符輸入流
Reader 字符輸入流頂層抽象父類
FileReader 文件字符輸入流
2.FileReader
構造方法:
FileReader(File name); 根據File對象創建字符輸入流對象
FileReader(String name); 根據字符串路徑創建字符輸入流對象
成員方法:
int read(); 一次讀取一個字符
int read(char[] arr); 一次讀取一個字符數組
void close(); 釋放資源
3.使用字符輸入流讀取文本文件
public class Demo02Reader {
public static void main(String[] args) throws IOException {
//1.創建FileReader對象,構造方法中綁定要讀取的數據源
FileReader fr = new FileReader("09_IOAndProperties\\c.txt");
//2.使用FileReader對象中的方法read讀取文件
//int read() 讀取單個字符並返回。
/*int len = 0;
while((len = fr.read()) != -1){
System.out.print((char)len);
}*/
//int read(char[] cbuf)一次讀取多個字符,將字符讀入數組。
char[] cs = new char[1024];//存儲讀取到的多個字符
int len = 0;//記錄的是每次讀取的有效字符個數
while((len = fr.read(cs)) != -1){
/*
String類的構造方法
String(char[] value) 把字符數組轉換爲字符串
String(char[] value, int offset, int count) 把字符數組的一部分轉換爲字符串 offset數組的開始索引 count轉換的個數
*/
System.out.println(new String(cs,0,len));
}
//3.釋放資源
fr.close();
}
}
4.字符輸出流
Writer 字符輸出流頂層抽象父類
FileWriter 文件字符輸出流對象
5.FileWriter
構造方法:
FileWriter(String name); 根據字符串路徑創建字符輸出流對象
FileWriter(File name); 根據File對象創建字符輸出流對象
FileWriter(String name,boolean append); 根據字符串路徑創建字符輸出流對象,如果布爾類型參數爲true。可以實現續寫
FileWriter(File name,boolean append); 根據File對象創建字符輸出流對象,如果布爾類型參數爲true。可以實現續寫
成員方法:
write(int ch); 一次寫出一個字符
write(char[] arr); 一次寫出一個字符數組
write(char[] arr,int index,int count); 一次寫出字符數組的一部分
write(String s); 一次寫出一個字符串
write(String s,int index,int count); 一次寫出字符串的一部分
void close(); 釋放資源
void flush(); 刷新緩衝區
6.使用字符流複製文本文件
public class Test02 {
public static void main(String[] args) throws IOException{
FileReader fr = new FileReader("d:\\src\\詩.txt");
FileWriter fw = new FileWriter("d:\\dest\\copy.txt");
char[] arr = new char[1024];
int len;
while((len = fr.read(arr)) != -1) {
fw.write(arr,0,len);
}
fr.close();
fw.close();
}
}
7.flush方法和close方法的區別
flush():刷新緩衝區的內容,還可以繼續寫出
close():釋放資源,但是在正式關閉之前,也會刷新緩衝區。關閉流對象後,就不能在寫出了
注意事項:JVM的虛擬機目前只有512MB,所以操作的文件過大的時候,不能只通過最後一次colse()方法
去一次性刷新該流的緩衝,不然會導致內存溢出
8.如何實現字符流續寫和換行
public class Demo04Writer {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("09_IOAndProperties\\g.txt",true);
for (int i = 0; i <10 ; i++) {
fw.write("HelloWorld"+i+"\r\n");
}
fw.close();
}
}
9.JDK1.7版本之前的IO流標準異常處理代碼【重點練習】
public class Demo03 {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("d:\\src\\桌面.jpg");
fos = new FileOutputStream("d:\\dest\\copy.jpg");
//讀和寫
byte[] arr = new byte[1024];
int len;
while((len = fis.read(arr)) != -1) {
fos.write(arr,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException o) {
o.printStackTrace();
}finally {
//釋放資源
try {
if(fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
10.JDK7版本的IO流異常處理代碼【重點練習】
public class Demo02JDK7 {
public static void main(String[] args) {
try(
//1.創建一個字節輸入流對象,構造方法中綁定要讀取的數據源
FileInputStream fis = new FileInputStream("c:\\1.jpg");
//2.創建一個字節輸出流對象,構造方法中綁定要寫入的目的地
FileOutputStream fos = new FileOutputStream("d:\\1.jpg");
)
{
//可能會產出異常的代碼
//一次讀取一個字節寫入一個字節的方式
//3.使用字節輸入流對象中的方法read讀取文件
int len = 0;
while((len = fis.read())!=-1){
//4.使用字節輸出流中的方法write,把讀取到的字節寫入到目的地的文件中
fos.write(len);
}
}catch (IOException e){
//異常的處理邏輯
System.out.println(e);
}
}
}
11.JDK9版本的IO流異常處理代碼
public class Demo03JDK9 {
public static void main(String[] args) throws FileNotFoundException{
//1.創建一個字節輸入流對象,構造方法中綁定要讀取的數據源
FileInputStream fis = new FileInputStream("c:\\1.jpg");
//2.創建一個字節輸出流對象,構造方法中綁定要寫入的目的地
FileOutputStream fos = new FileOutputStream("d:\\1.jpg");
try(fis; fos){
//一次讀取一個字節寫入一個字節的方式
//3.使用字節輸入流對象中的方法read讀取文件
int len = 0;
while((len = fis.read())!=-1){
//4.使用字節輸出流中的方法write,把讀取到的字節寫入到目的地的文件中
fos.write(len);
}
}catch (IOException e){
System.out.println(e);
}
//fos.write(1);//Stream Closed
}
}
三、Properties集合
1.Properties集合是一個雙列集合。它的父類是Hashtable
/*
使用Properties集合存儲數據,遍歷取出Properties集合中的數據
Properties集合是一個雙列集合,key和value默認都是字符串
Properties集合有一些操作字符串的特有方法
Object setProperty(String key, String value) 調用 Hashtable 的方法 put。
String getProperty(String key) 通過key找到value值,此方法相當於Map集合中的get(key)方法
Set<String> stringPropertyNames() 返回此屬性列表中的鍵集,其中該鍵及其對應值是字符串,此方法相當於Map集合中的keySet方法
*/
private static void show01() {
//創建Properties集合對象
Properties prop = new Properties();
//使用setProperty往集合中添加數據
prop.setProperty("趙麗穎","168");
prop.setProperty("迪麗熱巴","165");
prop.setProperty("古力娜扎","160");
//prop.put(1,true);
//使用stringPropertyNames把Properties集合中的鍵取出,存儲到一個Set集合中
Set<String> set = prop.stringPropertyNames();
//遍歷Set集合,取出Properties集合的每一個鍵
for (String key : set) {
//使用getProperty方法通過key獲取value
String value = prop.getProperty(key);
System.out.println(key+"="+value);
}
}
2.Properties集合的store方法和load方法的使用
public class Demo04 {
public static void main(String[] args) throws Exception{
//demo01();
Properties prop = new Properties();
//load()方法的使用
//prop.load(new FileReader("my01-code\\config.properties"));
prop.load(new FileInputStream("my01-code\\\\config.properties"));
//獲取數據
String value = prop.getProperty("username");
String value2 = prop.getProperty("password");
System.out.println(value);
System.out.println(value2);
}
//store方法的使用
private static void demo01() throws IOException {
Properties prop = new Properties();
prop.setProperty("username","admin");
prop.setProperty("password","123456");
//將集合中的數據寫出到配置文件中
prop.store(new FileOutputStream("my01-code\\config.properties"),"this is my config");
}
}