Java核心类库篇6——IO
生活随笔
收集整理的這篇文章主要介紹了
Java核心类库篇6——IO
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Java核心類庫篇6——IO
1、File
1.1、構(gòu)造方法
| public File(File parent, String child) | 從父抽象路徑名和子路徑名字符串創(chuàng)建新的 File實例 |
| public File(String pathname) | 通過將給定的路徑名字符串轉(zhuǎn)換為抽象路徑名來創(chuàng)建新的 File實例 |
| public File(String parent, String child) | 從父路徑名字符串和子路徑名字符串創(chuàng)建新的File實例 |
| public File(URI uri) | 通過將給定的file:URI轉(zhuǎn)換為抽象路徑名來創(chuàng)建新的File實例 |
1.2、方法
| public boolean exists() | 測試此抽象路徑名表示的文件或目錄是否存在 |
| public boolean exists() | 測試此抽象路徑名表示的文件或目錄是否存在 |
| public long length() | 返回由此抽象路徑名表示的文件的長度 |
| public long lastModified() | 用于獲取文件的最后一次修改時間 |
| public String getAbsolutePath() | 用于獲取絕對路徑信息 |
| public boolean delete() | 用于刪除文件,當(dāng)刪除目錄時要求是空目錄 |
| public boolean createNewFile() | 用于創(chuàng)建新的空文件 |
| public boolean mkdir() | 用于創(chuàng)建目錄 |
| public boolean mkdirs() | 用于創(chuàng)建多級目錄 |
| public File[] listFiles() | 獲取該目錄下的所有內(nèi)容 |
| public boolean isFile() | 判斷是否為文件 |
| public boolean isDirectory() | 判斷是否為目錄 |
| public File[] listFiles(FileFilter filter) | 獲取目錄下滿足篩選器的所有內(nèi)容 |
2、IO
IO就是Input和Output的簡寫,也就是輸入和輸出的含義
2.1、IO分類
- 按讀寫數(shù)據(jù)的基本單位
- 字節(jié)流:以字節(jié)為單位進行數(shù)據(jù)讀寫的流,可以讀寫任意類型的文件
- 字符流:以字符(2個字節(jié))為單位進行數(shù)據(jù)讀寫的流,只能讀寫文本文件
- 按讀寫數(shù)據(jù)的方向不同
- 輸入流:從文件中讀取數(shù)據(jù)內(nèi)容輸入到程序中,也就是讀文件
- 輸出流:將程序中的數(shù)據(jù)內(nèi)容輸出到文件中,也就是寫文件
- 按流的角色
- 節(jié)點流:直接和輸入輸出源對接的流
- 處理流:需要建立在節(jié)點流的基礎(chǔ)之上的流
2.2、IO流的體系結(jié)構(gòu)
紅色為抽象類
藍色為節(jié)點流,必須直接與指定的物理節(jié)點關(guān)聯(lián)
| 抽象基類 | InputStream | OutputStream | Reader | Writer |
| 訪問文件 | FileInputStream | FileOutputStream | FileReader | FileWriter |
| 訪問數(shù)組 | ByteArrayInputStream | ByteArrayOutputStream | CharArrayReader | CharArrayWriter |
| 訪問管道 | PipedInputStream | PipedOutputStream | PipedReader | PipedWriter |
| 訪問字符串 | —— | —— | StringReader | StringWriter |
| 緩沖流 | BufferedInputStream | BufferedOutputStream | BufferedReader | BufferedWriter |
| 轉(zhuǎn)換流 | —— | —— | InputStreamReader | OutputStreamWriter |
| 對象流 | ObjectInputStream | ObjectOutputStream | —— | —— |
| 打印流 | —— | PrintStream | —— | PrintWriter |
| FilterInputStream | FilterOutputStream | FilterReader | FilterWriter | |
| 推回輸入流 | PushbackInputStream | —— | PushbackReader | —— |
| 特殊流 | DataInputStream | DataOutputStream | —— | —— |
2.3、FileInputStream
逐個字節(jié)讀
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();} }建立緩沖區(qū)讀
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();} }追加內(nèi)容
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();} }建立緩沖區(qū)讀
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();} }追加內(nèi)容
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、轉(zhuǎn)換流
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[])—從數(shù)據(jù)輸入流讀取數(shù)據(jù)存儲到字節(jié)數(shù)組b中
- read(byte b[],int off,in len)—從數(shù)據(jù)輸入流中讀取數(shù)據(jù)存儲到數(shù)組b里面,位置從off開始,長度為len個字節(jié)
- readFully(byte b[])—從數(shù)據(jù)輸入流中循環(huán)讀取b.length個字節(jié)到數(shù)組b中
- readFully(byte b[],int off,in len )—從數(shù)據(jù)輸入流中循環(huán)讀取len個字節(jié)到字節(jié)數(shù)組b中.從b的off位置開始
- skipBytes(int b)—跳過n個字節(jié)
- readBoolean()—從數(shù)據(jù)輸入流讀取布爾類型的值
- readByte()—從數(shù)據(jù)輸入流中讀取一個字節(jié)
- readUnsignedByte()—從數(shù)據(jù)輸入流中讀取一個無符號的字節(jié),返回值轉(zhuǎn)換成int類型
- readShort()—從數(shù)據(jù)輸入流讀取一個short類型數(shù)據(jù)
- readUnsignedShort()—從數(shù)據(jù)輸入流讀取一個無符號的short類型數(shù)據(jù)
- readChar()—從數(shù)據(jù)輸入流中讀取一個字符數(shù)據(jù)
- readInt()—從數(shù)據(jù)輸入流中讀取一個int類型數(shù)據(jù)
- readLong()—從數(shù)據(jù)輸入流中讀取一個long類型的數(shù)據(jù)
- readFloat()—從數(shù)據(jù)輸入流中讀取一個float類型的數(shù)據(jù)
- readDouble()—從數(shù)據(jù)輸入流中讀取一個double類型的數(shù)據(jù)
- readUTF()—從數(shù)據(jù)輸入流中讀取用UTF-8格式編碼的UniCode字符格式的字符串
2.9.2、DataOutputStream
- intCount(int value)—數(shù)據(jù)輸出流增加的字節(jié)數(shù)
- write(int b)—將int類型的b寫到數(shù)據(jù)輸出流中
- write(byte b[],int off, int len)—將字節(jié)數(shù)組b中off位置開始,len個長度寫到數(shù)據(jù)輸出流中
- flush()—刷新數(shù)據(jù)輸出流
- writeBoolean()—將布爾類型的數(shù)據(jù)寫到數(shù)據(jù)輸出流中,底層是轉(zhuǎn)化成一個字節(jié)寫到基礎(chǔ)輸出流中
- writeByte(int v)—將一個字節(jié)寫到數(shù)據(jù)輸出流中(實際是基礎(chǔ)輸出流)
- writeShort(int v)—將一個short類型的數(shù)據(jù)寫到數(shù)據(jù)輸出流中,底層將v轉(zhuǎn)換2個字節(jié)寫到基礎(chǔ)輸出流中
- writeChar(int v)—將一個charl類型的數(shù)據(jù)寫到數(shù)據(jù)輸出流中,底層是將v轉(zhuǎn)換成2個字節(jié)寫到基礎(chǔ)輸出流中
- writeInt(int v)—將一個int類型的數(shù)據(jù)寫到數(shù)據(jù)輸出流中,底層將4個字節(jié)寫到基礎(chǔ)輸出流中
- writeLong(long v)—將一個long類型的數(shù)據(jù)寫到數(shù)據(jù)輸出流中,底層將8個字節(jié)寫到基礎(chǔ)輸出流中
- writeFloat(flloat v)—將一個float類型的數(shù)據(jù)寫到數(shù)據(jù)輸出流中,底層會將float轉(zhuǎn)換成int類型,寫到基礎(chǔ)輸出流中
- writeDouble(double v)—將一個double類型的數(shù)據(jù)寫到數(shù)據(jù)輸出流中,底層會將double轉(zhuǎn)換成long類型,寫到基礎(chǔ)輸出流中
- writeBytes(String s)—將字符串按照字節(jié)順序?qū)懙交A(chǔ)輸出流中
- writeChars(String s)—將字符串按照字符順序?qū)懙交A(chǔ)輸出流中
- writeUTF(String str)—以機器無關(guān)的方式使用utf-8編碼方式將字符串寫到基礎(chǔ)輸出流中
- size()—寫到數(shù)據(jù)輸出流中的字節(jié)數(shù)
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 +'}';} }當(dāng)我們使用Serializable接口實現(xiàn)序列化操作的時候,如果一個對象的某一個屬性不想被序列化保存下來,那么我們可以使用transient關(guān)鍵字進行說明
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) | 根據(jù)參數(shù)指定的名稱和模式構(gòu)造對象 r: 以只讀方式打開 rw:打開以便讀取和寫入 rwd:打開以便讀取和寫入,同步文件內(nèi)容的更新 rws:打開以便讀取和寫入,同步文件內(nèi)容和元數(shù)據(jù) 的更新 |
| int read() | 讀取單個字節(jié)的數(shù)據(jù) |
| void seek(long pos) | 用于設(shè)置從此文件的開頭開始測量的文件指針偏移量 |
| void write(int b) | 將參數(shù)指定的單個字節(jié)寫入 |
| void close() | 用于關(guān)閉流并釋放有關(guān)的資源 |
總結(jié)
以上是生活随笔為你收集整理的Java核心类库篇6——IO的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: es dsl java api_求帮助将
- 下一篇: java joda 获取utc时间_ja