Java第十章IO
十、IO
?
File類?
?
java.io.File類 1.凡是與輸入、輸出相關的類、接口等都定義在java.io包下 2.File是一個類,可以有構造器創建其對象。此對象對應著一個文件或文件目錄 3.File類對象是與平臺無關的 4.File中的方法,僅涉及到如何創建、刪除、重命名等等,只要涉及文件內容的,File無能為力。必須有IO流來完成 5.File類的對象常作為IO流的具體類的構造器的形參。路徑: 絕對路徑:包括盤符在內的完整的文件路徑 相對路徑:在當前文件目錄下的文件的路徑?
?
?
File方法
?
getName()//獲取文件名 getPath()//獲取文件路徑 getAbsoluteFile()//獲取絕對文件名 getParent()//獲取上一層的文件路徑//相對路徑時null getAbsolutePath()//獲取絕對路徑 renameTo(File newName)//重命名 file1.renameTo(file2)//file1重命名為file2.要求file文件一定存在,file2一定不存在exists()//文件存在 canWrite()//文件可寫 canRead()//文件可讀 isFile()//是文件嗎 isDirectory()//是文件目錄 lastModfied()//最后修改時間 length()//內容長度sysout(new Date(file1.lastModified()));createNewFile() delete() mkDir():創建一個文件目錄,只有在上層文件目錄存在的情況下,才能創建 mkDirs():創建一個文件目錄,若上層文件目錄不存在,一并創建。 list() :得到文件目錄的內容,用字符串數組接收 listFiles():得到文件目錄的內容,用文件數組接收。
?
?
?
IO的體系
?
流
?
FileInputStream
?
//read() @Testpublic void test1(){FileInputStream fis = null;try {File file1 = new File("g:/IO/hello2.txt");fis = new FileInputStream(file1);int b = fis.read();while(b!=-1){System.out.print((char)b);b = fis.read();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(fis != null){try {fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}//read(byte[] byte)
?? ?@Test
?? ?public void test2(){
?? ??? ?FileInputStream fis = null;
?? ??? ?try {
?? ??? ??? ?File file2 = new File("g:/IO/hello2.txt");
?? ??? ??? ? fis = new FileInputStream(file2);
?? ??? ??? ?byte[] b = new byte[5];
?? ??? ??? ?int len ;
?? ??? ??? ?while((len = fis.read(b))!= -1){
?? ??? ??? ??? ?for(int i= 0 ;i<len;i++){
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ?}
?? ??? ??? ??? ?String str = new String(b,0,len);
?? ??? ??? ??? ?System.out.print(str);
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ?}? catch (IOException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}finally{
?? ??? ??? ?if(fis!=null){
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?fis.close();
?? ??? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ?}
?? ??? ?
?? ?}
?
? FileOutputStream
? 輸出的物理文件可以不存在,當執行過程中,若不存在,會自動的創建,若存在,會將原有的文件覆蓋
?
@Testpublic void test3(){File file3 = new File("g:/IO/hello3.txt");FileOutputStream fos = null;try{fos = new FileOutputStream(file3);fos.write(new String("I love myself!").getBytes());}catch(Exception e){e.printStackTrace();}finally{if(fos!=null){try {fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}?
?
FileReaderFileWriter
?
@Test public void test5(){FileReader fr = null;FileWriter fw = null;try {File file1 = new File("C:\\Users\\dell\\Desktop\\新建文件夾\\java基礎.txt");File file2 = new File("C:\\Users\\dell\\Desktop\\新建文件夾\\111.txt");fr = new FileReader(file1);fw = new FileWriter(file2);int len;char[] c = new char[1000];while((len=fr.read(c))!=-1){fw.write(c, 0, len);}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(fr!=null){try {fr.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(fw!=null){try {fw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}?
? BufferedInputStream/BufferedOutputStream
@Testpublic void test6(){FileInputStream fis = null;FileOutputStream fos = null;BufferedInputStream bis = null;BufferedOutputStream bos = null;try {File file1 = new File("D:\\百度網盤下載\\java\\第10章:IO(day15-day16)\\day15\\day15_04IO流概述.wmv");File file2 = new File("D:\\百度網盤下載\\java\\第10章:IO(day15-day16)\\day15\\1.wmv");fis = new FileInputStream(file1);fos = new FileOutputStream(file2);bis = new BufferedInputStream(fis);bos = new BufferedOutputStream(fos);int len;byte[] b = new byte[1024];while((len = bis.read(b))!= -1){bos.write(b, 0, len);bos.flush();}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(bis!= null){try {bis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(bos!=null){try {bos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}?
?
BufferedWriter/BufferedReader
?
@Testpublic void test7(){BufferedWriter bw = null;BufferedReader br = null;try {File file1 = new File("");File file2 = new File("");FileReader fr = new FileReader(file1);FileWriter fw = new FileWriter(file2);br = new BufferedReader(fr);bw = new BufferedWriter(fw);String str;while((str = br.readLine())!= null){bw.write(str);bw.newLine();//換行bw.flush();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(bw!=null){try {bw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(br!=null){try {br.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}?
?
?
?
轉換流:
InputStreamReader / OutputStreamWriter(前提:數據是文本文件)
/*如何實現字節流與字符流之間的轉換:* 轉換流:InputStreamReader OutputStreamWriter* 編碼:字符串 ---->字符數組* 解碼:字符數組--->字符串*/@Testpublic void test8(){BufferedReader br = null;BufferedWriter bw = null;try {File file1 = new File("");File file2 = new File("");FileInputStream fis = new FileInputStream(file1);FileOutputStream fos = new FileOutputStream(file2);InputStreamReader isr = new InputStreamReader(fis, "UTF-8");OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");br = new BufferedReader(isr);bw = new BufferedWriter(osw);String str;while((str = br.readLine())!=null){bw.write(str);bw.newLine();bw.flush();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(bw!=null){try {bw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(br!=null){try {br.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
?
?
標準的輸入輸出流:
標準的輸出流:System.out
標準的輸入流:System.in
str.toUpperCase();小寫變大寫
注:使用FileReader、FileWriter 可以實現文本文件的復制
對于非文本文件(視頻文件、音頻文件、圖片),只能使用字節流
?
打印流:
打印流:字節流:PrintStream 字符流:PrintWriter //創建打印輸出流,設置為自動刷新模式(寫入換行符或字節'\n'時都會刷新輸出緩存區)if(ps != null){//把標準輸出流(控制臺輸出)改成文件System.setOut(ps);}?
?
?數據流:
數據流:用來處理基本數據類型、String、字節數組的數據:DataInputStream DataOutputStream
?
對象流:
?
要實現序列化的類:1.要求此類是可序列化的,實現Serializable接口2.要求類的屬性同樣的要實現Serializable接口3.提供一個版本號,private static fianl long serialVersionUID4.使用static 或 transient 修飾的屬性,不可實現序列化對象的序列化過程,將內存中的對象通過ObjectOutputStream轉換為二進制流,存儲到硬盤文件中對象的反序列化過程,將二進制流通過ObjectInputStream轉換為內存中的對象。?
?
?
?
RandomAccessFile:
?
/** RandomAccessFile:支持隨機訪問* 1.即可以充當一個輸入流,又可以充當一個輸出流* 2.支持從文件的開頭讀取、寫入* 3.支持從任意位置的讀入、寫入(插入)*/@Testpublic void testRandomAccess(){RandomAccessFile raf1 = null;RandomAccessFile raf2 = null;try {raf1 = new RandomAccessFile(new File("hello.txt"),"r");raf2 = new RandomAccessFile(new File("hello1.txt"),"rw");byte[] b = new byte[5];int len;while((len = raf1.read(b)) != -1){raf2.write(b,0, len);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(raf1 != null){try {raf1.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(raf2!=null){try {raf2.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}/*
?? ? * 實現文件指定位置插入數據
?? ? */
?? ?@Test
?? ?public void test2(){
?? ??? ?
?? ??? ?RandomAccessFile raf = null;
?? ??? ?try {
?? ??? ??? ?raf = new RandomAccessFile(new File("hello1.txt"), "rw");
?? ??? ??? ?raf.seek(4);//把指針位置移到第4個位置
?? ??? ??? ?byte[] b = new byte[10];
?? ??? ??? ?int len;
?? ??? ??? ?StringBuffer sb = new StringBuffer();
?? ??? ??? ?while((len = raf.read(b))!=-1){
?? ??? ??? ??? ?sb.append(new String(b,0,len));//把文件中當前指針位置后面的數據存到StringBuffer
?? ??? ??? ?}
?? ??? ??? ?raf.seek(4);
?? ??? ??? ?raf.write("YHS".getBytes());
?? ??? ??? ?raf.write(sb.toString().getBytes());
?? ??? ?} catch (IOException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}finally{
?? ??? ??? ?if(raf!=null){
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?raf.close();
?? ??? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?}
?? ??? ??? ??? ?
?? ??? ??? ?}
?? ??? ?}
?
?
?
轉載于:https://www.cnblogs.com/yangHS/p/10699729.html
總結
- 上一篇: 程序退出
- 下一篇: 【STM32H7教程】第4章 STM3