Java細(xì)數(shù)IO流底層原理到方法使用
一、什么是IO流
輸入流和輸出流。
- 輸入流:數(shù)據(jù)從數(shù)據(jù)源(文件)到程序(內(nèi)存)的路徑
- 輸出流:數(shù)據(jù)從程序(內(nèi)存)到數(shù)據(jù)源(文件)的路徑
二、常用的文件操作
學(xué)習(xí)目標(biāo):創(chuàng)建文件對象相關(guān)構(gòu)造器和方法
new File(String pathname)//根據(jù)路徑構(gòu)建一個File對象
new File(File parent,String child)//根據(jù)父目錄文件+子路徑構(gòu)建
new File(String parent,String child)//根據(jù)父目錄+子路徑構(gòu)建
學(xué)習(xí)任務(wù):在e盤下,創(chuàng)建文件news1.txt、news2.txt、news3.txt用三種不同方式創(chuàng)建。
三種方式簡單看一下就行,后面會經(jīng)常遇到。
new File(String pathname)//根據(jù)路徑構(gòu)建一個File對象
package com.file;
import java.io.*;
public class FileCreate {
public static void main(String[] args) {
//方式 1 new File(String pathname)
String filePath = "e:\\news1.txt";
File file = new File(filePath);
try {
//創(chuàng)建新文件
file.createNewFile();
System.out.println("文件創(chuàng)建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
}new File(File parent,String child)//根據(jù)父目錄文件+子路徑構(gòu)建
package com.file;
import java.io.*;
public class FileCreate {
public static void main(String[] args) {
//方式 2 new File(File parent,String child) //根據(jù)父目錄文件+子路徑構(gòu)建
//e:\\news2.txt
File parentFile = new File("e:\\");
String fileName = "news2.txt";
//這里的 file 對象,在 java 程序中,只是一個對象
//只有執(zhí)行了 createNewFile 方法,才會真正的,在磁盤創(chuàng)建該文件
File file = new File(parentFile, fileName);
try {
file.createNewFile();
System.out.println("創(chuàng)建成功~");
} catch (IOException e) {
e.printStackTrace();
}
}
}new File(String parent,String child)//根據(jù)父目錄+子路徑構(gòu)建
package com.file;
import java.io.*;
public class FileCreate {
public static void main(String[] args) {
//方式 3 new File(String parent,String child) //根據(jù)父目錄+子路徑構(gòu)建
String parentPath = "e:\\";
String fileName = "news3.txt";
File file = new File(parentPath, fileName);
try {
file.createNewFile();
System.out.println("創(chuàng)建成功~");
} catch (IOException e) {
e.printStackTrace();
}
}
}三、獲取文件的相關(guān)信息
getName、getAbsolutePath、getParent、length、exists、isFile、isDirectory
學(xué)習(xí)任務(wù):獲取文件的大小、文件名、路徑、父File,是文件還是目錄(目錄本質(zhì)也是文件,一種特殊的文件),是否存在

四、目錄的操作和文件刪除
學(xué)習(xí)任務(wù):
- 判斷e:\news1.txt是否存在,如果存在就刪除
- 判斷e:\\demo02是否存在,存在就刪除,否則提示不存在
- 判斷e:\\demo\a\b\c目錄是否存在,如果存在就提示已經(jīng)存在,否則就創(chuàng)建
package com.collection;
import java.io.File;
public class Delete {
public static void main(String[] args) {
String filePath="e:\\news1.txt";
File file=new File(filePath);
if(file.exists()){
file.delete();
}else {
System.out.println("否則文件不存在~");
}
}
}package com.collection;
import java.io.File;
public class Delete {
public static void main(String[] args) {
String filePath="e:\\demo02";
File file=new File(filePath);
if(file.exists()){
if(file.delete()){
System.out.println(filePath+"刪除成功");
}
else{
System.out.println(filePath+"刪除失敗");
}
}else {
System.out.println("否則目錄不存在~");
}
}
}package com.collection;
import java.io.*;
public class Delete {
public static void main(String[] args) {
String directoryPath="e:\\demo\\a\\b\\c";
File file=new File(directoryPath);
if(file.exists()) {
System.out.println(directoryPath + "存在。。");
}
else {
if (file.mkdirs()){
System.out.println(directoryPath+"創(chuàng)建成功。。");
}else{
System.out.println(directoryPath+"創(chuàng)建失敗。。");
}
}
}
}按操作數(shù)據(jù)單位不同分為:字節(jié)流(8bit)二進(jìn)制文件,字符流(按字符)文本文件
按數(shù)據(jù)流的流向不同分為:輸入流,輸出流
按流的角色的不同分為:節(jié)點(diǎn)流,處理流/包裝流
字節(jié)流:InputStream,OutputStream
字符流:Reader,Writer
五、IO流體系圖-常用的類

六、FileInputStream常用方法
學(xué)習(xí)任務(wù):請使用 FileInputStream 讀取 hello.txt 文件,并將文件內(nèi)容顯示
先在e盤下創(chuàng)建hello.txt輸入內(nèi)容hello world
package com.FileInputStream;
import java.io.FileInputStream;
import java.io.IOException;
//字節(jié)流文件的輸入程序
public class FileInputStream_ {
public static void main(String[] args) {
String filePath="e:\\hello.txt";
int readData=0;
FileInputStream fileInputStream=null;
try {
//創(chuàng)建 FileInputStream 對象,用于讀取文件
fileInputStream=new FileInputStream(filePath);
//從該輸入流讀取一個字節(jié)的數(shù)據(jù)。 如果沒有輸入可用,此方法將阻止。
//如果返回-1 , 表示讀取完畢
while ((readData = fileInputStream.read()) != -1) {
System.out.print((char)readData);//轉(zhuǎn)成 char 顯示
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
//關(guān)閉文件流
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}七、FileOutputStream常用方法
學(xué)習(xí)任務(wù):請使用 FileOutputStream 在 abc.txt 文件,中寫入 “hello,world”. 如果文件不存在,會創(chuàng)建 文件(注意:前提是目錄已經(jīng)存在.)
package com.hspedu.outputstream_;
import java.io.*;
public class FileOutputStream01 {
public static void main(String[] args) {
String filePath = "D:\\abc.txt";
FileOutputStream fileOutputStream = null;
try {
//得到 FileOutputStream 對象 對象
//1. new FileOutputStream(filePath) 創(chuàng)建方式,當(dāng)寫入內(nèi)容是,會覆蓋原來的內(nèi)容
// 2. new FileOutputStream(filePath, true) 創(chuàng)建方式,當(dāng)寫入內(nèi)容是,是追加到文件后面
fileOutputStream = new FileOutputStream(filePath);
//寫入一個字節(jié)
//fileOutputStream.write('H');
//寫入字符串
String str = "hello,world!";
//str.getBytes() 可以把 字符串-> 字節(jié)數(shù)組
//fileOutputStream.write(str.getBytes());
/*
write(byte[] b, int off, int len) 將 len 字節(jié)從位于偏移量 off 的指定字節(jié)數(shù)組寫入此文件輸出流
*/
fileOutputStream.write(str.getBytes(), 0, 3);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}學(xué)習(xí)任務(wù):編程完成圖片/音樂 的拷貝
package com.hspedu.outputstream_;
import java.io.*;
public class FileCopy {
public static void main(String[] args) {
String srcFilePath="e:\\9030.jpg";
String destFilePath="e:\\9031.jpg";
FileInputStream fileInputStream=null;
FileOutputStream fileOutputStream=null;
try {
fileInputStream=new FileInputStream(srcFilePath);
fileOutputStream=new FileOutputStream(destFilePath);
//定義一個字節(jié)數(shù)組,提高讀取效果
byte[] buf=new byte[1024];
int readLen=0;
while ((readLen=fileInputStream.read(buf))!=-1){
fileOutputStream.write(buf,0,readLen);
}
System.out.println("拷貝成功!");
} catch (IOException e) {
e.printStackTrace();
}finally {
try{
if(fileInputStream!=null){
fileInputStream.close();
}
if(fileOutputStream!=null){
fileInputStream.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
}八、FileReader常用方法
- new FileReader(File/String)
- read:每次讀取單個字符,返回該字符,如果到文件末尾返回-1
- read(char[]):批量讀取多個字符到數(shù)組,返回讀取到的字符數(shù),如果到文件末尾返回-1
- 相關(guān)API:
- new String(char[]):將char[]轉(zhuǎn)換成String
- new String(char[],off,len):將char[]的指定部分轉(zhuǎn)換成String
九、FileWriter常用方法
- new FileWriter(File/String):覆蓋模式,相當(dāng)于流的指針在首端
- new FileWriter(File/String,true):追加模式,相當(dāng)于流的指針在尾端
- write(int):寫入單個字符
- write(char[]):寫入指定數(shù)組
- write(char[],off,len):寫入指定數(shù)組的指定部分
- write(string):寫入單個字符
- write(string[],off,len):寫入字符串的指定部分
- 相關(guān)API:String類:toCharArray:將String轉(zhuǎn)換成char[]
- FileWriter使用后,必須要關(guān)閉(close)或刷新(flush), 否則寫入不到指定的文件!
學(xué)習(xí)任務(wù):使用 FileReader 從 story.txt ,這一步先在story.txt存在數(shù)據(jù),然后在端口輸出數(shù)據(jù)顯示出來
package com.reader_;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile01 {
public static void main(String[] args) {
String filePath="e:\\story.txt";
FileReader fileReader=null;
int data=0;
//創(chuàng)建FileReader對象
try{
fileReader =new FileReader(filePath);
//循環(huán)讀取 使用 read, 單個字符讀取
while((data=fileReader.read())!=-1){
//data數(shù)值為整數(shù)型,強(qiáng)制轉(zhuǎn)換為字符
System.out.print((char)data);
}
}catch(
IOException e){
e.printStackTrace();
}finally {
try{
if(fileReader!=null){
//關(guān)閉文件流
fileReader.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}學(xué)習(xí)任務(wù):字符數(shù)組讀取文件
package com.reader_;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile02 {
public static void main(String[] args) {
String filePath="e:\\story.txt";
FileReader fileReader=null;
int readLen=0;
char[] buf=new char[8];
//創(chuàng)建FileReader對象
try{
fileReader =new FileReader(filePath);
//循環(huán)讀取 使用 read(buf), 返回的是實(shí)際讀取到的字符數(shù)
//如果返回-1, 說明到文件結(jié)束
while((readLen=fileReader.read(buf))!=-1){
System.out.print(new String(buf,0,readLen));
}
}catch(
IOException e){
e.printStackTrace();
}finally {
try{
if(fileReader!=null){
fileReader.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}學(xué)習(xí)任務(wù):使用 FileWriter 將 “風(fēng)雨之后,定見彩虹” 寫入到 note.txt 文件中
package com.hspedu.writer_;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriter_ {
public static void main(String[] args) {
String filePath="e:\\noth.txt";
FileWriter fileWriter=null;
char[] chars={'a','b','c'};
try {
fileWriter = new FileWriter(filePath);
//3) write(int):寫入單個字符
fileWriter.write('H');
// 4) write(char[]):寫入指定數(shù)組
fileWriter.write(chars);
// 5) write(char[],off,len):寫入指定數(shù)組的指定部分
fileWriter.write("程序員飛鳥".toCharArray(), 0, 3);
// 6) write(string):寫入整個字符串
fileWriter.write(" 你好廣州");
fileWriter.write("風(fēng)雨之后,定見彩虹");
// 7) write(string,off,len):寫入字符串的指定部分
fileWriter.write("上海天津", 0, 2);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
//對應(yīng) FileWriter , 一定要關(guān)閉流,或者 flush 才能真正的把數(shù)據(jù)寫入文件里面
fileWriter.flush();//關(guān)閉文件流
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("程序結(jié)束~");
}
}到此這篇關(guān)于Java細(xì)數(shù)IO流基礎(chǔ)到方法使用的文章就介紹到這了,更多相關(guān)Java IO流內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(20)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07
Java中的布隆過濾器原理實(shí)現(xiàn)和應(yīng)用
Java中的布隆過濾器是一種基于哈希函數(shù)的數(shù)據(jù)結(jié)構(gòu),能夠高效地判斷元素是否存在于一個集合中。它廣泛應(yīng)用于緩存、網(wǎng)絡(luò)協(xié)議、數(shù)據(jù)查詢等領(lǐng)域,在提高程序性能和減少資源消耗方面具有顯著優(yōu)勢2023-04-04
java阿拉伯?dāng)?shù)字轉(zhuǎn)中文數(shù)字
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)阿拉伯?dāng)?shù)字轉(zhuǎn)換為中文數(shù)字,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04
Java springboot 配置文件與多環(huán)境配置與運(yùn)行優(yōu)先級
這篇文章主要介紹了Java springboot如何配置文件,進(jìn)行多環(huán)境配置,以及運(yùn)行優(yōu)先級,感興趣的小伙伴可以借鑒一下2023-04-04
java 三種將list轉(zhuǎn)換為map的方法詳解
這篇文章主要介紹了java 三種將list轉(zhuǎn)換為map的方法詳解的相關(guān)資料,需要的朋友可以參考下2017-01-01
Springboot項目中單元測試時注入bean失敗的解決方案
這篇文章主要介紹了Springboot項目中單元測試時注入bean失敗的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
Springboot中使用Redisson+AOP+自定義注解實(shí)現(xiàn)訪問限流與黑名單攔截
本文主要介紹了Springboot中使用Redisson+AOP+自定義注解實(shí)現(xiàn)訪問限流與黑名單攔截,包含針對用戶IP限流,整個接口的訪問限流,以及對某個參數(shù)字段的限流,并且支持請求限流后處理回調(diào),感興趣的可以了解一下2024-02-02
使用profiles進(jìn)行多環(huán)境配置的代碼實(shí)現(xiàn)
在項目開發(fā)的過程中會用到多個環(huán)境,為了便于開發(fā)使用,通常需要使用profiles進(jìn)行多環(huán)境配置,所以本文給大家介紹了使用profiles進(jìn)行多環(huán)境配置的代碼實(shí)現(xiàn),需要的朋友可以參考下2024-02-02

