日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java核心类库篇6——IO

發布時間:2025/3/12 java 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java核心类库篇6——IO 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Java核心類庫篇6——IO

1、File

1.1、構造方法

方法聲明功能介紹
public File(File parent, String child)從父抽象路徑名和子路徑名字符串創建新的 File實例
public File(String pathname)通過將給定的路徑名字符串轉換為抽象路徑名來創建新的 File實例
public File(String parent, String child)從父路徑名字符串和子路徑名字符串創建新的File實例
public File(URI uri)通過將給定的file:URI轉換為抽象路徑名來創建新的File實例

1.2、方法

方法聲明功能介紹
public boolean exists()測試此抽象路徑名表示的文件或目錄是否存在
public boolean exists()測試此抽象路徑名表示的文件或目錄是否存在
public long length()返回由此抽象路徑名表示的文件的長度
public long lastModified()用于獲取文件的最后一次修改時間
public String getAbsolutePath()用于獲取絕對路徑信息
public boolean delete()用于刪除文件,當刪除目錄時要求是空目錄
public boolean createNewFile()用于創建新的空文件
public boolean mkdir()用于創建目錄
public boolean mkdirs()用于創建多級目錄
public File[] listFiles()獲取該目錄下的所有內容
public boolean isFile()判斷是否為文件
public boolean isDirectory()判斷是否為目錄
public File[] listFiles(FileFilter filter)獲取目錄下滿足篩選器的所有內容

2、IO

IO就是Input和Output的簡寫,也就是輸入和輸出的含義

2.1、IO分類

  • 按讀寫數據的基本單位
    • 字節流:以字節為單位進行數據讀寫的流,可以讀寫任意類型的文件
    • 字符流:以字符(2個字節)為單位進行數據讀寫的流,只能讀寫文本文件
  • 按讀寫數據的方向不同
    • 輸入流:從文件中讀取數據內容輸入到程序中,也就是讀文件
    • 輸出流:將程序中的數據內容輸出到文件中,也就是寫文件
  • 按流的角色
    • 節點流:直接和輸入輸出源對接的流
    • 處理流:需要建立在節點流的基礎之上的流

2.2、IO流的體系結構

紅色為抽象類

藍色為節點流,必須直接與指定的物理節點關聯

分類字節輸入流字節輸出流字符輸入流字符輸出流
抽象基類InputStreamOutputStreamReaderWriter
訪問文件FileInputStreamFileOutputStreamFileReaderFileWriter
訪問數組ByteArrayInputStreamByteArrayOutputStreamCharArrayReaderCharArrayWriter
訪問管道PipedInputStreamPipedOutputStreamPipedReaderPipedWriter
訪問字符串————StringReaderStringWriter
緩沖流BufferedInputStreamBufferedOutputStreamBufferedReaderBufferedWriter
轉換流————InputStreamReaderOutputStreamWriter
對象流ObjectInputStreamObjectOutputStream————
打印流——PrintStream——PrintWriter
FilterInputStreamFilterOutputStreamFilterReaderFilterWriter
推回輸入流PushbackInputStream——PushbackReader——
特殊流DataInputStreamDataOutputStream————

2.3、FileInputStream

逐個字節讀

public class Test {public static void main(String[] args) throws IOException {String path="D:\\a.txt";File file = new File(path);FileInputStream fileInputStream = new FileInputStream(file);int l = 0;while ((l=fileInputStream.read())!=-1){System.out.println((char)l);}fileInputStream.close();} }

建立緩沖區讀

public class Test {public static void main(String[] args) throws IOException {String path="D:\\a.txt";File file = new File(path);FileInputStream fileInputStream = new FileInputStream(file);byte[] b=new byte[1024];int l = 0;while ((l=fileInputStream.read(b))!=-1){System.out.println(new String(b,0,l));}fileInputStream.close();} }

2.4、FileOutputStream

寫入文件

public class Test {public static void main(String[] args) throws IOException {String path="D:\\a.txt";File file = new File(path);FileOutputStream fileOutputStream = new FileOutputStream(file);String str="hello world, i am ruoye!";byte[] bytes = str.getBytes();fileOutputStream.write(bytes);fileOutputStream.close();} }

追加內容

public class Test {public static void main(String[] args) throws IOException {String path="D:\\a.txt";File file = new File(path);FileOutputStream fileOutputStream = new FileOutputStream(file,true);String str="hello world, i am ruoye!";byte[] bytes = str.getBytes();fileOutputStream.write(bytes);fileOutputStream.close();} }

2.5、FileReader

逐個字符讀

public class Test {public static void main(String[] args) throws IOException {String path="D:\\a.txt";File file = new File(path);FileReader fileReader = new FileReader(file);int l = 0;while ((l=fileReader.read())!=-1){System.out.println((char)l);}fileReader.close();} }

建立緩沖區讀

public class Test {public static void main(String[] args) throws IOException {String path="D:\\a.txt";File file = new File(path);FileReader fileReader = new FileReader(file);char[] b=new char[1024];int l = 0;while ((l=fileReader.read(b))!=-1){System.out.println(new String(b,0,l));}fileReader.close();} }

2.6、FileWriter

寫入文件

public class Test {public static void main(String[] args) throws IOException {String path="D:\\a.txt";File file = new File(path);FileWriter fileWriter = new FileWriter(file);String str="你好,世界!";fileWriter.write(str);fileWriter.close();} }

追加內容

public class Test {public static void main(String[] args) throws IOException {String path="D:\\a.txt";File file = new File(path);FileWriter fileWriter = new FileWriter(file,true);String str="你好,世界!";fileWriter.append(str);fileWriter.close();} }

2.7、轉換流

public class Test {public static void main(String[] args) throws IOException {InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(new File("D:\\a.txt")));int l = 0;while ((l=inputStreamReader.read())!=-1){System.out.println((char)l);}inputStreamReader.close();} } public class Test {public static void main(String[] args) throws IOException {OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(new File("D:\\a.txt")));String str="hello wordld";outputStreamWriter.write(str);outputStreamWriter.close();} }

2.8、緩沖流

public class Test {public static void main(String[] args) throws IOException {String path="D:\\a.txt";File file = new File(path);FileInputStream fileInputStream = new FileInputStream(file);BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);byte[] bytes=new byte[1024];int l = 0;while ((l=fileInputStream.read(bytes))!=-1){System.out.println(new String(bytes,0,l));}fileInputStream.close();} } public class Test {public static void main(String[] args) throws IOException {String path="D:\\a.txt";File file = new File(path);FileOutputStream fileOutputStream = new FileOutputStream(file);BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);String str="hello world, i am ruoye!";byte[] bytes = str.getBytes();bufferedOutputStream.write(bytes);bufferedOutputStream.close();fileOutputStream.close();} }

writer和reader同上

2.9、Data流

2.9.1、DataInputStream

  • read(byte b[])—從數據輸入流讀取數據存儲到字節數組b中
  • read(byte b[],int off,in len)—從數據輸入流中讀取數據存儲到數組b里面,位置從off開始,長度為len個字節
  • readFully(byte b[])—從數據輸入流中循環讀取b.length個字節到數組b中
  • readFully(byte b[],int off,in len )—從數據輸入流中循環讀取len個字節到字節數組b中.從b的off位置開始
  • skipBytes(int b)—跳過n個字節
  • readBoolean()—從數據輸入流讀取布爾類型的值
  • readByte()—從數據輸入流中讀取一個字節
  • readUnsignedByte()—從數據輸入流中讀取一個無符號的字節,返回值轉換成int類型
  • readShort()—從數據輸入流讀取一個short類型數據
  • readUnsignedShort()—從數據輸入流讀取一個無符號的short類型數據
  • readChar()—從數據輸入流中讀取一個字符數據
  • readInt()—從數據輸入流中讀取一個int類型數據
  • readLong()—從數據輸入流中讀取一個long類型的數據
  • readFloat()—從數據輸入流中讀取一個float類型的數據
  • readDouble()—從數據輸入流中讀取一個double類型的數據
  • readUTF()—從數據輸入流中讀取用UTF-8格式編碼的UniCode字符格式的字符串
public class Test {public static void main(String[] args) throws IOException {DataInputStream dataInputStream = new DataInputStream(new FileInputStream("D:\\a.txt"));System.out.println(dataInputStream.readUTF());System.out.println(dataInputStream.readInt());System.out.println(dataInputStream.readBoolean());System.out.println(dataInputStream.readShort());System.out.println(dataInputStream.readLong());System.out.println(dataInputStream.readDouble());dataInputStream.close();} }

2.9.2、DataOutputStream

  • intCount(int value)—數據輸出流增加的字節數
  • write(int b)—將int類型的b寫到數據輸出流中
  • write(byte b[],int off, int len)—將字節數組b中off位置開始,len個長度寫到數據輸出流中
  • flush()—刷新數據輸出流
  • writeBoolean()—將布爾類型的數據寫到數據輸出流中,底層是轉化成一個字節寫到基礎輸出流中
  • writeByte(int v)—將一個字節寫到數據輸出流中(實際是基礎輸出流)
  • writeShort(int v)—將一個short類型的數據寫到數據輸出流中,底層將v轉換2個字節寫到基礎輸出流中
  • writeChar(int v)—將一個charl類型的數據寫到數據輸出流中,底層是將v轉換成2個字節寫到基礎輸出流中
  • writeInt(int v)—將一個int類型的數據寫到數據輸出流中,底層將4個字節寫到基礎輸出流中
  • writeLong(long v)—將一個long類型的數據寫到數據輸出流中,底層將8個字節寫到基礎輸出流中
  • writeFloat(flloat v)—將一個float類型的數據寫到數據輸出流中,底層會將float轉換成int類型,寫到基礎輸出流中
  • writeDouble(double v)—將一個double類型的數據寫到數據輸出流中,底層會將double轉換成long類型,寫到基礎輸出流中
  • writeBytes(String s)—將字符串按照字節順序寫到基礎輸出流中
  • writeChars(String s)—將字符串按照字符順序寫到基礎輸出流中
  • writeUTF(String str)—以機器無關的方式使用utf-8編碼方式將字符串寫到基礎輸出流中
  • size()—寫到數據輸出流中的字節數
public class Test {public static void main(String[] args) throws IOException {DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream("D:\\a.txt"));dataOutputStream.writeUTF("α");dataOutputStream.writeInt(1234567);dataOutputStream.writeBoolean(true);dataOutputStream.writeShort((short)123);dataOutputStream.writeLong((long)456);dataOutputStream.writeDouble(99.98);dataOutputStream.close();} }

2.10、Zip流

壓縮文件

public class Test {public static void main(String[] args) throws IOException {File file = new File("D:\\a.txt");FileInputStream fileInputStream = new FileInputStream(file);File zip = new File("D:\\a.zip");FileOutputStream fileOutputStream = new FileOutputStream(zip);ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);int l = 0;zipOutputStream.putNextEntry(new ZipEntry(file.getName()));while ((l=fileInputStream.read())!=-1){//壓縮算法zipOutputStream.write(l);}fileInputStream.close();zipOutputStream.close();fileOutputStream.close();} }

解壓文件

public class Test {public static void main(String[] args) throws IOException {File file = new File("D:\\a.txt");File zip = new File("D:\\a.zip");ZipFile zipFile= new ZipFile(zip);ZipEntry entry = zipFile.getEntry("a.txt");InputStream input = zipFile.getInputStream(entry);FileOutputStream fileOutputStream = new FileOutputStream(file);int l;while ((l=input.read())!=-1){//解壓算法fileOutputStream.write(l);}fileOutputStream.close();input.close();} }

壓縮多個文件

待補

解壓多個文件

待補

2.11、Object流

想要使用ObjectOutputStream和ObjectInputStream,對象一定要序列化

import java.io.Serializable;public class Person implements Serializable {private String name;private int age;public Person() {}public Person(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +'}';} }

當我們使用Serializable接口實現序列化操作的時候,如果一個對象的某一個屬性不想被序列化保存下來,那么我們可以使用transient關鍵字進行說明

private transient String name;

2.11.1、ObjectOutputStream

public class Test {public static void main(String[] args) throws IOException {File file = new File("D:\\a.txt");ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));objectOutputStream.writeObject(new Person("zhangsan",20));objectOutputStream.close();} }

2.11.2、ObjectInputStream

public class Test {public static void main(String[] args) throws IOException, ClassNotFoundException {File file = new File("D:\\a.txt");ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(file));Person person = (Person) objectInputStream.readObject();System.out.println(person);} }

2.12、RandomAccessFile

方法聲明功能介紹
public RandomAccessFile(String name, String mode)根據參數指定的名稱和模式構造對象 r: 以只讀方式打開 rw:打開以便讀取和寫入 rwd:打開以便讀取和寫入,同步文件內容的更新 rws:打開以便讀取和寫入,同步文件內容和元數據 的更新
int read()讀取單個字節的數據
void seek(long pos)用于設置從此文件的開頭開始測量的文件指針偏移量
void write(int b)將參數指定的單個字節寫入
void close()用于關閉流并釋放有關的資源
public class Test {public static void main(String[] args) throws IOException {RandomAccessFile r = new RandomAccessFile("D:\\a.txt", "rw");r.seek(1);System.out.println((char) r.read());r.write('b');r.close();} }

總結

以上是生活随笔為你收集整理的Java核心类库篇6——IO的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。