欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

新手了解java IO基礎(chǔ)知識

 更新時間:2021年07月06日 09:04:28   作者:王星偉QAQ  
這篇文章主要介紹了java 基礎(chǔ)知識之IO總結(jié)的相關(guān)資料,Java中的I/O分為兩種類型,一種是順序讀取,一種是隨機讀取,需要的朋友可以參考下,希望對你有幫助

一、File類

1、簡介

java.io.File類:文件和文件目錄路徑的抽象表示形式,與平臺無關(guān) File 能新建、刪除、重命名文件和目錄,但 File 不能訪問文件內(nèi)容本 身。如果需要訪問文件內(nèi)容本身,則需要使用輸入/輸出流。 想要在Java程序中表示一個真實存在的文件或目錄,那么必須有一個 File對象。

2、創(chuàng)建方式

public File(String pathname);//以pathname為路徑創(chuàng)建File對象,可以是絕對路徑或者相對路徑。
  • 絕對路徑:是一個固定的路徑,從盤符開始
  • 相對路徑:是相對于某個位置開始
public File(String parent,String child);//以parent為父路徑,child為子路徑創(chuàng)建File對象。
public File(File parent,String child);//根據(jù)一個父File對象和子文件路徑創(chuàng)建File對象。

3、常用方法

public String getAbsolutePath()//獲取絕對路徑
public String getPath() //獲取路徑
public String getName() //獲取名稱
public String getParent()//獲取上層文件目錄路徑。若無,返回null
public long length() //獲取文件長度(即:字節(jié)數(shù))
public long lastModified() //獲取最后一次的修改時間,毫秒值
public String[] list() //獲取指定目錄下的所有文件或者文件目錄的名稱數(shù)組
public File[] listFiles()//獲取指定目錄下的所有文件或者文件目錄的File數(shù)組
public boolean createNewFile()//當(dāng)且僅當(dāng)不存在具有此抽象路徑名指定名稱的文件時,不可分地創(chuàng)建一個新的空文件。 
public boolean delete() //刪除此抽象路徑名表示的文件或目錄。 
public boolean exists()//測試此抽象路徑名表示的文件或目錄是否存在。 
public String[] list()//返回一個字符串?dāng)?shù)組,這些字符串指定此抽象路徑名表示的目錄中的文件和目錄。
public boolean mkdirs()//創(chuàng)建此抽象路徑名指定的目錄,包括所有必需但不存在的父目錄。 
public boolean isDirectory()//判斷是否是文件目錄
public boolean isFile()//判斷是否是文件

示例:

public class FileTest {
    public static void main(String[] args) {
        //File(String pathname);//以pathname為路徑創(chuàng)建File對象
        File file = new File("E:\\aaa");
        //File(File parent,String child);//根據(jù)一個父File對象和子文件路徑創(chuàng)建File對象。
        File file1 = new File(file,"test.txt");
        //boolean exists()判斷文件或目錄是否存在。 
        if (!(file.exists())){
            // boolean mkdirs()創(chuàng)建此路徑名指定的目錄,包括所有必需但不存在的父目錄。
            file.mkdirs();
        }else {
            try {
                //boolean createNewFile()當(dāng)且僅當(dāng)不存在具有此路徑名指定名稱的文件時,創(chuàng)建一個新的空文件。
                file1.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //String getPath()獲取路徑
        System.out.println(file.getPath());
        //long length()獲取文件長度(即:字節(jié)數(shù))
        System.out.println(file.length());
        //String getName()獲取文件名稱
        System.out.println(file.getName());
        //long lastModified()獲取最后一次的修改時間,毫秒值
        System.out.println(file.getName());
        // public boolean isFile() :判斷是否是文件
		System.out.println(file.isFile());
      //  delete(file);
    }
//遞歸的方式刪除文件或者文件夾
    public static void delete(File file){
 //File[] listFiles() 獲取指定目錄下的所有文件或者文件目錄的名稱數(shù)組
        File[] files = file.listFiles();
        for (File f : files) {
            //boolean isDirectory()判斷是否是文件目錄
            if (f.isDirectory()){
                delete(f);
            }
            //boolean delete()刪除此路徑名表示的文件或目錄。
            f.delete();
        }
        file.delete();
    }

說明:Java中的刪除不到回收站,要刪除一個文件目錄,注意該文件目錄內(nèi)不能包含文件或者文件目錄。

二、IO概念

  • I/O 即輸入Input/ 輸出Output的縮寫,其實就是計算機調(diào)度把各個存儲中(包括內(nèi)存和外部存儲)的數(shù)據(jù)寫入寫出
  • java中用“流(stream)”來抽象表示這么一個寫入寫出的功能,封裝成一個“類”,都放在java.io這個包里面。
  • java.io包下提供了各種“流”類和接口,用以獲取不同種類的數(shù)據(jù),并 通過標(biāo)準(zhǔn)的方法輸入或輸出數(shù)據(jù)

1.什么是輸入

​ 程序從內(nèi)存中讀取數(shù)據(jù)叫輸入Input。

2.什么輸出(Output)

​ 程序把數(shù)據(jù)寫入到內(nèi)存中叫輸出Output。

三、流的分類

  • 按操作數(shù)據(jù)單位不同分為:字節(jié)流(8 bit),字符流(16 bit)
  • 按數(shù)據(jù)流的流向不同分為:輸入流,輸出流
  • 按流的角色的不同分為:節(jié)點流,處理流

IO流體系

img

1、InputStream(字節(jié)流)

示例:

 public static void main(String[] args) {
       iprt();
    }
    public static void ipst(){
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("C:\\1.txt");
            int i;
            while ( (i = inputStream.read()) != -1){
                System.out.print((char) i);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null){
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

說明:使用InputStream向內(nèi)存中讀如文件數(shù)據(jù)。

2、OutputStream(字節(jié)流)

示例:

public class ImageCopy {
    public static void main(String[] args) {
        try(
                InputStream inputStream = new FileInputStream("D:\\KDA.jpg");
                OutputStream outputStream = new FileOutputStream("E:\\aaa\\KDA.jpg")
         ){
            byte[] bytes = new byte[1024];
            int i;
            while ((i = inputStream.read(bytes)) != -1){
                outputStream.write(bytes,0,i);
            }
        }  catch (IOException e) {
            e.printStackTrace();
        }
    }
}

說明:使用輸入流與輸出流結(jié)合實現(xiàn)圖片復(fù)制的功能。

3、Reader(字符流)

示例:

public static  void iprt(){
        Reader reader = null;
        try {
            reader = new FileReader("C:\\1.txt");
            int i ;
            while ((i =  reader.read()) != -1){
                System.out.print((char) i);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

說明:使用Reader(字符流)從文件中讀入數(shù)據(jù)。

4、Writer(字符流)

public static  void iprt(){
        Reader reader = null;
        Writer writer = null;
        try {
            reader = new FileReader("C:\\Users\\52425\\Desktop\\1.txt");
            writer = new FileWriter("C:\\2.txt");
            int i ;
            while ((i =  reader.read()) != -1){
                writer.write(i);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
                try {
                        writer.close();
                        reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

說明:使用字符流實現(xiàn)文件復(fù)制功能。

四、總結(jié)(1+2)

1. File類及方法的使用

File是操作文件/目錄的類,可以對文件/目錄進行創(chuàng)建,重命名, 刪除等操作。

2.IO流的分類

  • 根據(jù)數(shù)據(jù)大小可分為:字節(jié)流和字符流
  • 根據(jù)流向可分為:輸入流和輸出流
  • 根據(jù)功能可分為:節(jié)點流和處理流

3.IO流的四個基本類

  • 字節(jié)輸入流:InputStream,它的常用子類是FileInputStream
  • 字節(jié)輸出流:OutputStream,它的常用子類是OutputStream
  • 字符輸入流:Reader,它的常用子類是FileReader
  • 字符輸出流:Writer,它的常用子類是FileWriter

總結(jié)

本篇關(guān)于java IO的文章就到這里了,希望能幫到你,也希望你能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • java 如何計算同比增長工具類

    java 如何計算同比增長工具類

    這篇文章主要介紹了java 如何計算同比增長工具類的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Lucene實現(xiàn)索引和查詢的實例講解

    Lucene實現(xiàn)索引和查詢的實例講解

    下面小編就為大家分享一篇Lucene實現(xiàn)索引和查詢的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • Java高效實現(xiàn)excel轉(zhuǎn)pdf(支持帶圖片的轉(zhuǎn)換)

    Java高效實現(xiàn)excel轉(zhuǎn)pdf(支持帶圖片的轉(zhuǎn)換)

    這篇文章主要為大家詳細(xì)介紹了如何用java實現(xiàn)excel轉(zhuǎn)pdf文件,并且支持excel單元格中帶有圖片的轉(zhuǎn)換,文中的示例代碼講解詳細(xì),需要的可以參考下
    2024-01-01
  • Springboot Mybatis Plus自動生成工具類詳解代碼

    Springboot Mybatis Plus自動生成工具類詳解代碼

    mybatis-plus 是一個 Mybatis 的增強工具,在 Mybatis 的基礎(chǔ)上只做增強不做改變,為簡化開發(fā)、提高效率而生,這篇文章帶你使用Springboot Mybatis Plus自動生成工具類
    2021-11-11
  • 最新評論