Java中IO流文件讀取、寫入和復(fù)制的示例分析

小編給大家分享一下Java中IO流文件讀取、寫入和復(fù)制的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)專注于襄城網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供襄城營銷型網(wǎng)站建設(shè),襄城網(wǎng)站制作、襄城網(wǎng)頁設(shè)計、襄城網(wǎng)站官網(wǎng)定制、小程序開發(fā)服務(wù),打造襄城網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供襄城網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

//構(gòu)造文件File類
File f=new File(fileName);

//判斷是否為目錄
f.isDirectory();

//獲取目錄下的文件名
String[] fileName=f.list();

//獲取目錄下的文件
File[] files=f.listFiles();

1、Java怎么讀取文件

package com.yyb.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/*
 * 讀取文件:
 * 1、找到指定的文件
 * 2、根據(jù)文件創(chuàng)建文件的輸入流
 * 3、創(chuàng)建字節(jié)數(shù)組
 * 4、讀取內(nèi)容,放到字節(jié)數(shù)組里面
 * 5、關(guān)閉輸入流
 */
public class FileRead {

	public static void main(String[] args) {
		// 構(gòu)建指定文件
		File file = new File("E:" + File.separator + "hello.txt");
		InputStream in = null;
		try {
			// 根據(jù)文件創(chuàng)建文件的輸入流
			in = new FileInputStream(file);
			// 創(chuàng)建字節(jié)數(shù)組
			byte[] data = new byte[1024];
			// 讀取內(nèi)容,放到字節(jié)數(shù)組里面
			in.read(data);
			System.out.println(new String(data));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				// 關(guān)閉輸入流
				in.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}

2、Java怎么寫入文件

package com.yyb.file;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

/*
 * 寫入文件:
 * 1、找到指定的文件
 * 2、根據(jù)文件創(chuàng)建文件的輸出流
 * 3、把內(nèi)容轉(zhuǎn)換成字節(jié)數(shù)組
 * 4、向文件寫入內(nèi)容
 * 5、關(guān)閉輸入流
 */
public class FileWriter {

	public static void main(String[] args) {
		// 構(gòu)建指定文件
		File file = new File("E:" + File.separator + "hello.txt");
		OutputStream out = null;
		try {
			// 根據(jù)文件創(chuàng)建文件的輸出流
			out = new FileOutputStream(file);
			String message = "我是好人。";
			// 把內(nèi)容轉(zhuǎn)換成字節(jié)數(shù)組
			byte[] data = message.getBytes();
			// 向文件寫入內(nèi)容
			out.write(data);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				// 關(guān)閉輸出流
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}

3、Java怎么復(fù)制文件

<span >package com.yyb.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
/*
 * 實(shí)現(xiàn)思路:
 * 1、構(gòu)建源文件與目標(biāo)文件
 * 2、源文件創(chuàng)建輸入流,目標(biāo)文件創(chuàng)建輸出流
 * 3、創(chuàng)建字節(jié)數(shù)組
 * 4、使用循環(huán),源文件讀取一部分內(nèi)容,目標(biāo)文件寫入一部分內(nèi)容,直到寫完所有內(nèi)容
 * 5、關(guān)閉源文件輸入流,目標(biāo)文件輸出流
 */
public class FileCopy {

	public static void main(String[] args) {
		// 構(gòu)建源文件
		File file = new File("E:" + File.separator + "HelloWorld.txt");
		// 構(gòu)建目標(biāo)文件
		File fileCopy = new File("D:" + File.separator + "HelloWorld");
		InputStream in = null;
		OutputStream out = null;
		try {
			// 目標(biāo)文件不存在就創(chuàng)建
			if (!(fileCopy.exists())) {
				fileCopy.createNewFile();
			}
			// 源文件創(chuàng)建輸入流
			in = new FileInputStream(file);
			// 目標(biāo)文件創(chuàng)建輸出流
			out = new FileOutputStream(fileCopy, true);
			// 創(chuàng)建字節(jié)數(shù)組
			byte[] temp = new byte[1024];
			int length = 0;
			// 源文件讀取一部分內(nèi)容
			while ((length = in.read(temp)) != -1) {
				// 目標(biāo)文件寫入一部分內(nèi)容
				out.write(temp, 0, length);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				// 關(guān)閉文件輸入輸出流
				in.close();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}</span><span >
</span>

以上是“Java中IO流文件讀取、寫入和復(fù)制的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

名稱欄目:Java中IO流文件讀取、寫入和復(fù)制的示例分析
瀏覽地址:http://muchs.cn/article0/ihesio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、云服務(wù)器網(wǎng)站設(shè)計、網(wǎng)站策劃靜態(tài)網(wǎng)站、定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司