第九章 IO流
9 IO流
????IO流用來處理設備之間的數據傳輸,Java對數據的操作是通過流的方式。
流按操作數據分為:字節流和字符流
流按流向分為:輸入流和輸出流
9.1 IO流常用基類
字節流的抽象類
InputStream,OutputStream
字符流的抽象類
????? Reader,Writer
9.2 FileWriter和FileReader
9.2.1 FileWriter
????該對象一被初始化就必須要明確被操作的文件,而且該文件會被創建到指定目錄下。如果該目錄已有同名文件,將被覆蓋。若傳遞一個true參數,則不覆蓋已有文件,在末尾追加數據。
????FileWriter fw = new FilrWriter(“demo.txt”,true);
????調用write方法,將數據寫入到流中,在使用flush或者close方法刷新流中的數據。
9.2.2 FileReader
????創建一個文件讀取流對象,和指定的文件相關聯,該文件必須已經存在,如果不存在,則報FileNotFoundException異常。
????使用read方法讀取數據,讀到文件末尾返回-1。
9.3 帶緩沖區的字符流
BufferedWriter
BufferedReader
????緩沖區的出現是為了提高流的操作效率而出現的,所以在創建緩沖區之前,必須要先有流對象。只要將流對象作為參數傳遞給緩沖區的構造函數即可。
1、通過緩沖區,往文件中寫內容
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterDemo {
?? ?public static void main(String[] args) throws IOException {
?? ??? ?//創建字符寫入流對象
?? ??? ?FileWriter fw = new FileWriter("buf.txt");
?? ??? ?//加入緩沖
?? ???? BufferedWriter bw =new BufferedWriter(fw);
?? ???? bw.write("abcde");
?? ???? //只要用到緩沖區,就要刷新
?? ???? bw.flush();
?? ??? ?
?? ???? //關閉資源
?? ???? bw.close();
?? ?}
}
2、通過緩沖區,從文件中讀取內容
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderDemo {
?? ?public static void main(String[] args) throws IOException {
?? ??? ?FileReader fr = new FileReader("buf.txt");
?? ??? ?
?? ??? ?BufferedReader br = new BufferedReader(fr);
?? ??? ?String line = null;
?? ??? ?
?? ??? ?while (null!=(line=br.readLine())) {
?? ??? ??? ?System.out.println(line);
?? ??? ?}
?? ??? ?
?? ??? ?br.close();
?? ?}
}
3、通過緩沖區,復制文件
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedCopy {
?? ?public static void main(String[] args) {
?? ??? ?BufferedReader br = null;
?? ??? ?BufferedWriter bw = null;
?? ??? ?
?? ??? ?try {
?? ??? ??? ?br = new BufferedReader(new FileReader("in.txt"));
?? ??? ??? ?bw = new BufferedWriter(new FileWriter("out.txt"));
?? ??? ??? ?
?? ??? ??? ?String line = null;
?? ??? ??? ?
?? ??? ??? ?while (null!=(line=br.readLine())) {
?? ??? ??? ??? ?bw.write(line);
????????????????bw.newLine();
?? ??? ??? ???? bw.flush();
?? ??? ??? ?}
?? ??? ?} catch (IOException e) {
?? ??? ??? ?throw new RuntimeException("讀寫失敗");
?? ??? ?}finally{
?? ??? ??? ?try {
?? ??? ??? ??? ?if (null!=br) {
?? ??? ??? ??? ??? ?br.close();
?? ??? ??? ??? ?}
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?throw new RuntimeException("讀取關閉失敗");
?? ??? ??? ?}
?? ??? ??? ?try {
?? ??? ??? ??? ?if (null!=bw) {
?? ??? ??? ??? ??? ?bw.close();
?? ??? ??? ??? ?}
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?throw new RuntimeException("寫入關閉失敗");
?? ??? ??? ?}
?? ??? ?}
?? ?}
}
4、常用方法:
????newLine();換行
????readLine();讀一行文本,只返回回車符之前的內容,達到末尾返回null;
????BufferedReader的子類LineNumberReader提供了行號功能:setLineNumber和getLineNumber
???
9.4 字節流
FileOutputStream
FileInputStream
1、通過字節流,讀寫數據
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileStreamDemo {
?? ?public static void main(String[] args) throws IOException {
?? ??? ?readFile();
?? ??? ?writeFile();
?? ?}
?? ?
?? ?public static void readFile() throws IOException{
?? ??? ?FileOutputStream fos = new FileOutputStream("fos.txt");
?? ??? ?fos.write("abcde".getBytes());
?? ??? ?
?? ??? ?fos.close();
?? ?}
?? ?
?? ?public static void writeFile() throws IOException{
?? ??? ?FileInputStream fis = new FileInputStream("fos.txt");
?? ??? ?
?? ??? ?byte[] buf = new byte[1024];
?? ??? ?int len = 0;
?? ??? ?
?? ??? ?while ((len=fis.read(buf))!=-1) {
?? ??? ??? ?System.out.println(new String(buf, 0, len));
?? ??? ?}
?? ??? ?
?? ??? ?fis.close();
?? ?}
}
9.5 帶緩沖區的字節流
BufferedOutputStream
BufferedInputStream
1、通過帶緩沖區的字節流,復制文件
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferedStreamDemo {
?? ?public static void main(String[] args) throws IOException {
?? ??? ?copyFile();
?? ?}
?? ?
?? ?public static void copyFile() throws IOException{
?? ??? ?
?? ??? ?BufferedInputStream bis = new BufferedInputStream(new FileInputStream("1.mp3"));
?? ??? ?BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("2.mp3"));
?? ??? ?
?? ??? ?int b = 0;
?? ??? ?
?? ??? ?while ((b=bis.read())!=-1) {
?? ??? ??? ?bos.write(b);
?? ??? ?}
?? ??? ?
?? ??? ?bos.close();
?? ??? ?bis.close();
?? ?}
}
9.6 轉換流
InputStreamReader:字節流轉換為字符流
OutputStreamWriter:字符流轉換為字節流
????轉換流可以指定編碼表
1、通過轉換流,賦值文件內容
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class TransStream {
?? ?public static void main(String[] args) throws IOException {
?? ??? ?//獲取鍵盤錄入
?? ??? ?//InputStream is = System.in;
?? ??? ?FileInputStream fis =new FileInputStream("in.txt");
?? ??? ?//將讀取字節流轉為字符流
?? ??? ?InputStreamReader isr = new InputStreamReader(fis);
?? ??? ?
?? ??? ?BufferedReader br = new BufferedReader(isr);
?? ??? ?
?? ??? ?//OutputStream os = System.out;
?? ??? ?FileOutputStream fos =new FileOutputStream("out.txt");
?? ??? ?//將寫入字節流轉為字符流
?? ??? ?OutputStreamWriter osw =new OutputStreamWriter(fos);
?? ??? ?BufferedWriter bw =new BufferedWriter(osw);
?? ??? ?
?? ??? ?String line =null;
?? ??? ?while ((line=br.readLine())!=null) {
?? ??? ??? ?
?? ??? ??? ?if ("over".equals(line)) {
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?bw.write(line.toUpperCase());
?? ??? ??? ?bw.newLine();
?? ??? ??? ?bw.flush();
?? ??? ?}
?? ??? ?
?? ??? ?br.close();
?? ?}
}
9.7 流操作的基本規律
1、明確源和目的
????源:InputStream,Reader
????目的:OutputStream,Writer
2、操作的數據是否是純文本
????是:字符流
????不是:字節流
3、當體系確定后,再明確要使用哪個具體的對象
????源設備:內存、硬盤、鍵盤
????目的設備:內存、硬盤、控制臺
9.8 File類
????用來將文件或者文件夾封裝成對象,方便對文件或者文件夾的屬性信息進行操作。
1、File對象可以作為參數傳遞給流的構造函數
????File f1 = new File(“c:\\a\\a.txt”);
????File f2 = new File(“c:\\a”,b.txt);
????"\\"不利于跨平臺,要使用File.separator來代替
2、File類常見方法
創建
????boolean createNewFile();在指定位置創建文件,如果該文件已經存在,則不創建
????????File f1 = new File(“c:\\a\\a.txt”);
????????f1.createNewFile();
????boolean mkdir();創建文件夾
????boolean mkdirs();
刪除
????boolean delete();刪除指定目錄的文件
????void deleteOnExit();退出的時候,刪除文件
判斷
????boolean canRead();是否可讀
????boolean canWrite();是否可寫
????boolean exists();是否存在
????boolean isFile();是否是文件
????boolean isDirectory();是否是文件夾
獲取
????getName();獲取文件名
????getPath();獲取文件路徑
????getParent();獲取文件的父目錄,是絕對路徑中的父目錄;如果獲取的是相對路徑則返回null。
3、遍歷文件
import java.io.File;
public class ShowFile {
?? ?public static void main(String[] args) {
?? ??? ?File f =new File("c:\\");
?? ??? ?
?? ??? ?File[] ff = f.listFiles();
?? ??? ?
?? ??? ?for(File file : ff){
?? ??? ??? ?System.out.println(file);
?? ??? ?}
?? ?}
}
4、過濾文件
import java.io.File;
import java.io.FilenameFilter;
public class FileFilter {
?? ?public static void main(String[] args) {
?? ??? ?File f =new File("c:\\");
?? ??? ?
?? ??? ?File[] ff = f.listFiles(new FilenameFilter() {
?? ??? ??? ?
?? ??? ??? ?@Override
?? ??? ??? ?public boolean accept(File dir, String name) {
?? ??? ??? ??? ?return name.endsWith(".dll");
?? ??? ??? ?}
?? ??? ?});
?? ??? ?
?? ??? ?for(File file : ff){
?? ??? ??? ?System.out.println(file);
?? ??? ?}
?? ?}
}
9.9 Properties類
Properties是HashTable的子類,具備Map集合的特點,它里面存儲的鍵值對是字符串。
Properties是集合中和IO技術結合的容器。
特點:可以用于鍵值對形式的配置文件
1、往Properties中插入值并取出
import java.util.Properties;
import java.util.Set;
public class PropertiesDemo {
?? ?public static void main(String[] args) {
?? ??? ?Properties p =new Properties();
?? ??? ?p.setProperty("zhangsan", "20");
?? ??? ?p.setProperty("lisi", "30");
?? ??? ?
?? ??? ?Set<String> s = p.stringPropertyNames();
?? ??? ?
?? ??? ?for(String name:s){
?? ??? ??? ?
?? ??? ??? ?System.out.println(name+":"+p.getProperty(name));
?? ??? ?}
?? ?}
}
2、讀取配置文件、加載配置文件和修改配置文件中的值
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class PropertiesIO {
?? ?public static void main(String[] args) throws IOException {
????????method1();
????????method2();
?? ???? method3();
?? ?}
??? //讀取配置文件
??? public static void method1() throws IOException{
?? ??? ?
?? ??? ?BufferedReader br =new BufferedReader(new FileReader("properties.txt"));
?? ??? ?
?? ??? ?String line = null;
?? ??? ?Properties p? = new Properties();
?? ??? ?
?? ??? ?while ((line=br.readLine())!=null) {
?? ??? ??? ?String[] pro = line.split("=");
?? ??? ??? ?
?? ??? ??? ?p.setProperty(pro[0], pro[1]);
?? ??? ?}
?? ??? ?br.close();
?? ??? ?System.out.println(p);
?? ?}
??? //加載配置文件
????public static void method2() throws IOException{
?? ??? ?
?? ?FileReader fr =new FileReader("properties.txt");
?? ????
?? ??? ?Properties p? = new Properties();
???
?? ??? ?p.load(fr);
?? ??? ?p.list(System.out);
?? ?}
????//修改配置文件中的值
??? public static void method3() throws IOException{
?? ?
?? ?FileInputStream fr =new FileInputStream("properties.txt");
?? ?FileOutputStream fw = new FileOutputStream("properties.txt");
?? ?
?? ??? ?Properties p? = new Properties();
?? ??? ?
?? ??? ?p.load(fr);
?? ??? ?fr.close();
?? ??? ?p.setProperty("lis", "20");
?? ??? ?p.store(fw, null);
?? ??? ?p.list(System.out);
?? ??? ?fw.close();
?? ?}
}
轉載于:https://blog.51cto.com/8163413/1713162
總結
- 上一篇: java.lang.NoSuchMeth
- 下一篇: Python Web部署方式总结