淺談Java中File文件的創(chuàng)建以及讀寫(xiě)
1.創(chuàng)建一個(gè)文件
@Test
public void test6() throws IOException {
File file1 = new File("C:\\IDEA\\h1.txt");
if(!file1.exists()){//文件不存在
file1.createNewFile();
System.out.println("創(chuàng)建成功");
}else{//文件存在
file1.delete();
System.out.println("刪除成功");
}
}
輸出


2.創(chuàng)建一個(gè)文件夾
file.mkdir,不會(huì)幫你創(chuàng)建上層目錄 file.mkdirs,會(huì)幫你創(chuàng)建上層目錄
@Test
public void test7(){
//創(chuàng)建文件夾,mkdir,不會(huì)幫你創(chuàng)建上層目錄
File file1 = new File("c:\\IDEA\\io2");
boolean mkdir =file1.mkdir();
if(mkdir){
System.out.println("創(chuàng)建成功1");
}
//創(chuàng)建文件夾,mkdirs,會(huì)幫你創(chuàng)建上層目錄
File file2 = new File("c:\\IDEA\\io1\\io3");
boolean mkdirs =file2.mkdirs();
if(mkdirs){
System.out.println("創(chuàng)建成功2");
}
}
輸出


3.創(chuàng)建同目錄下文件

4.刪除文件或空文件夾
@Test
public void test8(){
//刪除文件或空文件夾
File file1 = new File("c:\\IDEA\\h1.txt");
file1.delete();
}
5.遞歸刪除所有文件(包括子文件)
//遞歸函數(shù)刪除所有文件
private boolean deletedir(File dir){
if (dir.isDirectory()) {
File[] files = dir.listFiles();
//遞歸刪除目錄中的子目錄下
for (File f:files) {
boolean success = deletedir(f);
if (!success) {
return false;
}
}
}
// 目錄此時(shí)為空,可以刪除
return dir.delete();
}
@Test
public void test8() {
File dir = new File("c:\\IDEA");
System.out.println(deletedir(dir));
}

1.對(duì)于文本文件(.txt,.java,.c,.cpp),使用字符流處理
2.對(duì)于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt)使用字節(jié)流處理
6.讀取txt文件內(nèi)容,流操作要用try-catch(字符流)
@Test
public void test9() {
FileReader fr = null;//自動(dòng)補(bǔ)的
try {
//1.實(shí)例化File類(lèi)的對(duì)象,指明要操作的文件
File file1 = new File("c:\\IDEA\\hello.txt");
file1.createNewFile();//要拋出異常
//2.提供具體的流
fr = new FileReader(file1);
//3.數(shù)據(jù)的讀入
//read():返回讀入的一個(gè)字符,如果達(dá)到文件末尾,返回-1
int data = fr.read();
while(data!=-1){
System.out.print((char)data);
data = fr.read();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
//4.流的關(guān)閉操作
if(fr!=null)//防止沒(méi)有實(shí)例化成功,避免空指針異常
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}

要記得關(guān)閉,因?yàn)槲锢磉B接JVM垃圾回收機(jī)制不會(huì)自動(dòng)回收,要手動(dòng)關(guān)閉。
7.讀取文件內(nèi)容升級(jí)方法(字符流)
@Test
public void test1() {
FileReader fr = null;
try {
//1.File類(lèi)的實(shí)例化
File file = new File("hello.txt");
//2.FileReader流的實(shí)例化
fr = new FileReader(file);
//3.讀入的操作
//read(char[] cbuf):返回每次讀入cbuf數(shù)組中的字符的個(gè)數(shù)。如果達(dá)到文件末尾,返回-1
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf)) != -1) {
//錯(cuò)誤的寫(xiě)法
//for(int i=0;i<cbuf.length;i++{
// System.out.println(cbuf[i]);
//}
//正確的寫(xiě)法
for (int i = 0; i < len; i++) {
System.out.println(cbuf[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null)
try {
//4.資源的關(guān)閉
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
8.文件的寫(xiě)入(字符流)
@Test
public void test2() throws IOException{
//File對(duì)應(yīng)的硬盤(pán)中的文件
// 如果不存在,在輸出的過(guò)程中,會(huì)自動(dòng)創(chuàng)建此文件
//1.提供File類(lèi)的對(duì)象,指明寫(xiě)出到的文件
FileWriter fw = null;
try {
File file = new File("hello.txt");
//2.提供FileWriter的對(duì)象,用于數(shù)據(jù)的寫(xiě)出
//FileWriter(file,append)第二個(gè)參數(shù),append是true則在后面添加,是false就覆蓋
fw = new FileWriter(file,true);
//3.寫(xiě)出的操作
fw.write("I have a dream!");
fw.write("you need have a dream");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fw!=null)
//4.流資源的關(guān)閉
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

9.從一個(gè)文件讀取然后寫(xiě)入到另一個(gè)文件(字符流)
@Test
public void test3(){
FileReader fr = null;
FileWriter fw = null;
try {
// 1.創(chuàng)建File類(lèi)的對(duì)象,指明讀入和寫(xiě)出的文件
File src = new File("hello.txt");
File des = new File("hello1.txt");
// 2.創(chuàng)建輸入輸出流的對(duì)象
fr = new FileReader(src);
fw = new FileWriter(des,true);//不覆蓋
// 3.數(shù)據(jù)的讀入和寫(xiě)出操作
char[] cbuf = new char[5];
int len;
while((len = fr.read(cbuf))!=-1){
//每次寫(xiě)出len個(gè)字符
fw.write(cbuf,0,len);//從cbuf的0號(hào)位開(kāi)始寫(xiě)入len個(gè)字符
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 4.關(guān)閉流資源1
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
// 4.關(guān)閉流資源2
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
10.從一個(gè)文件讀取然后寫(xiě)入到另一個(gè)文件(字節(jié)流圖片)
@Test
public void test4(){
FileInputStream fis=null;
FileOutputStream fos=null;
try {
//1.造文件
File src = new File("b.jpg");
File des = new File("c.jpg");
//2.造流
fis = new FileInputStream(src);
fos = new FileOutputStream(des);
//3.讀數(shù)據(jù),存數(shù)據(jù)
byte[] buffer = new byte[5];
int len;//記錄每次讀取的字節(jié)的個(gè)數(shù)
while((len = fis.read(buffer))!=-1){
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos!=null) {
try {
//4.關(guān)閉資源
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis!=null) {
try {
//4.關(guān)閉資源
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
11.緩沖流(開(kāi)發(fā)時(shí)使用速度更快,效率更高)
@Test
public void test5(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.造文件
File src = new File("b.jpg");
File des = new File("d.jpg");
//2.造流
//2.1造節(jié)點(diǎn)流
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(des);
//2.2造緩沖流
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//3.復(fù)制的細(xì)節(jié):讀取,寫(xiě)入
byte[] buffer =new byte[10];
int len;
while((len=bis.read(buffer))!=-1){
bos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.資源關(guān)閉
//要求,先關(guān)閉外層的流,再關(guān)閉內(nèi)層的流
if(bos!=null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//說(shuō)明:關(guān)閉外層流的同時(shí),內(nèi)層自動(dòng)關(guān)閉,所以外層關(guān)閉可以省略
//fos.close();
//fis.close();
}
}
用緩沖流快了很多
經(jīng)典步驟:
1.創(chuàng)建File類(lèi)的對(duì)象,指明讀入和寫(xiě)出的文件
2.創(chuàng)建輸入輸出流的對(duì)象
3.數(shù)據(jù)的讀入和寫(xiě)出操作
4.關(guān)閉流資源
到此這篇關(guān)于淺談Java中File文件的創(chuàng)建以及讀寫(xiě)的文章就介紹到這了,更多相關(guān)Java中File文件的創(chuàng)建及讀寫(xiě)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺析 ArrayList 和 LinkedList 有什么區(qū)別
ArrayList 和 LinkedList 有什么區(qū)別,是面試官非常喜歡問(wèn)的一個(gè)問(wèn)題。今天通過(guò)本文給大家詳細(xì)介紹下,感興趣的朋友跟隨小編一起看看吧2020-10-10
Java在高并發(fā)場(chǎng)景下實(shí)現(xiàn)點(diǎn)贊計(jì)數(shù)器
點(diǎn)贊計(jì)數(shù)器的本質(zhì)就是對(duì)某個(gè)變量在高并發(fā)情況下的修改,這篇文章主要為大家介紹了Java實(shí)現(xiàn)點(diǎn)贊計(jì)數(shù)器的示例代碼,感興趣的小伙伴可以了解一下2023-06-06
List集合中對(duì)數(shù)據(jù)實(shí)現(xiàn)多重規(guī)則進(jìn)行排序的案例
今天小編就為大家分享一篇關(guān)于List集合中對(duì)數(shù)據(jù)實(shí)現(xiàn)多重規(guī)則進(jìn)行排序的案例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
SpringBoot整合mybatis簡(jiǎn)單案例過(guò)程解析
這篇文章主要介紹了SpringBoot整合mybatis簡(jiǎn)單案例過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
maven-surefire-plugin總結(jié)示例詳解
這篇文章主要介紹了maven-surefire-plugin總結(jié),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
Java 實(shí)現(xiàn)攔截器Interceptor的攔截功能方式
這篇文章主要介紹了Java 實(shí)現(xiàn)攔截器Interceptor的攔截功能方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Java中數(shù)組和List的互相轉(zhuǎn)換問(wèn)題小結(jié)
這篇文章主要介紹了Java中數(shù)組和List的互相轉(zhuǎn)換問(wèn)題小結(jié),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-03-03
Java并發(fā)Futures和Callables類(lèi)實(shí)例詳解
Callable對(duì)象返回Future對(duì)象,該對(duì)象提供監(jiān)視線程執(zhí)行的任務(wù)進(jìn)度的方法, Future對(duì)象可用于檢查Callable的狀態(tài),然后線程完成后從Callable中檢索結(jié)果,這篇文章給大家介紹Java并發(fā)Futures和Callables類(lèi)的相關(guān)知識(shí),感興趣的朋友一起看看吧2024-05-05
如何使用?Spring?Boot?搭建?WebSocket?服務(wù)器實(shí)現(xiàn)多客戶(hù)端連接
本文介紹如何使用SpringBoot快速搭建WebSocket服務(wù)器,實(shí)現(xiàn)多客戶(hù)端連接和消息廣播,WebSocket協(xié)議提供全雙工通信,SpringBoot通過(guò)@ServerEndpoint簡(jiǎn)化配置,支持實(shí)時(shí)消息推送,適用于聊天室或通知系統(tǒng)等應(yīng)用場(chǎng)景2024-11-11

