老熟女激烈的高潮_日韩一级黄色录像_亚洲1区2区3区视频_精品少妇一区二区三区在线播放_国产欧美日产久久_午夜福利精品导航凹凸

重慶分公司,新征程啟航

為企業提供網站建設、域名注冊、服務器等服務

Java文件讀寫IO/NIO及性能的比較

本篇內容介紹了“Java文件讀寫IO/NIO及性能的比較”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

雙陽ssl適用于網站、小程序/APP、API接口等需要進行數據傳輸應用場景,ssl證書未來市場廣闊!成為創新互聯的ssl證書銷售渠道,可以享受市場價格4-6折優惠!如果有意向歡迎電話聯系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!

文件讀寫有以下幾種常用的方法

1、字節讀寫(InputStream/OutputStream)

2、字符讀?。‵ileReader/FileWriter)

3、行讀?。˙ufferedReader/BufferedWriter)

代碼(以讀取為例):

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
/** 
 * 文件讀取類 
 * 1、按字節讀取文件內容 
 * 2、按字符讀取文件內容 
 * 3、按行讀取文件內容 
 * @author qin_xijuan 
 * 
 */
public class FileOperate {
	private static final String FILE_PATH = "d:/work/the List of Beautiful Music.txt";
	/** 
   * 以字節為單位讀取文件內容 
   * @param filePath:需要讀取的文件路徑 
   */
	public static void readFileBybyte(String filePath) {
		File file = new File(filePath);
		// InputStream:此抽象類是表示字節輸入流的所有類的超類。 
		InputStream ins = null ;
		try{
			// FileInputStream:從文件系統中的某個文件中獲得輸入字節。 
			ins = new FileInputStream(file);
			int temp ;
			// read():從輸入流中讀取數據的下一個字節。 
			while((temp = ins.read())!=-1){
				System.out.write(temp);
			}
		}
		catch(Exception e){
			e.getStackTrace();
		}
		finally{
			if (ins != null){
				try{
					ins.close();
				}
				catch(IOException e){
					e.getStackTrace();
				}
			}
		}
	}
	/** 
   * 以字符為單位讀取文件內容 
   * @param filePath 
   */
	public static void readFileByCharacter(String filePath){
		File file = new File(filePath);
		// FileReader:用來讀取字符文件的便捷類。 
		FileReader reader = null;
		try{
			reader = new FileReader(file);
			int temp ;
			while((temp = reader.read()) != -1){
				if (((char) temp) != '\r') {
					System.out.print((char) temp);
				}
			}
		}
		catch(IOException e){
			e.getStackTrace();
		}
		finally{
			if (reader != null){
				try {
					reader.close();
				}
				catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/** 
   * 以行為單位讀取文件內容 
   * @param filePath 
   */
	public static void readFileByLine(String filePath){
		File file = new File(filePath);
		// BufferedReader:從字符輸入流中讀取文本,緩沖各個字符,從而實現字符、數組和行的高效讀取。 
		BufferedReader buf = null;
		try{
			// FileReader:用來讀取字符文件的便捷類。 
			buf = new BufferedReader(new FileReader(file));
			// buf = new BufferedReader(new InputStreamReader(new FileInputStream(file))); 
			String temp = null ;
			while ((temp = buf.readLine()) != null ){
				System.out.println(temp);
			}
		}
		catch(Exception e){
			e.getStackTrace();
		}
		finally{
			if(buf != null){
				try{
					buf.close();
				}
				catch (IOException e) {
					e.getStackTrace();
				}
			}
		}
	}
	public static void main(String args[]) {
		readFileBybyte(FILE_PATH);
		readFileByCharacter(FILE_PATH);
		readFileByLine(FILE_PATH);
	}
}

//-----------------------------------------------------------------分割線-----------------------------------------------------------------------------

再經過兩位同行的提點下,我對之前寫的文件做了點修改,并通過讀寫一個1.2M的文本文件來測試各方法的性能。從多次測試結果來看,行讀寫卻是是Java.nio更有效率。

經過修改之后的代碼如下:

package com.waddell.basic;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/** 
 * 文件讀取類 
 * 1、按字節讀取文件內容 
 * 2、按字符讀取文件內容 
 * 3、按行讀取文件內容 
 * 
 * @author qin_xijuan 
 * 
 */
public class FileOperate {
	private static final String FILE_PATH = "d:/work/jipinwodi.txt";
	/** 
   * 以字節為單位讀寫文件內容 
   * 
   * @param filePath 
   *      :需要讀取的文件路徑 
   */
	public static void readFileBybyte(String filePath) {
		File file = new File(filePath);
		// InputStream:此抽象類是表示字節輸入流的所有類的超類。 
		InputStream ins = null;
		OutputStream outs = null;
		try {
			// FileInputStream:從文件系統中的某個文件中獲得輸入字節。 
			ins = new FileInputStream(file);
			outs = new FileOutputStream("d:/work/readFileByByte.txt");
			int temp;
			// read():從輸入流中讀取數據的下一個字節。 
			while ((temp = ins.read()) != -1) {
				outs.write(temp);
			}
		}
		catch (Exception e) {
			e.getStackTrace();
		}
		finally {
			if (ins != null && outs != null) {
				try {
					outs.close();
					ins.close();
				}
				catch (IOException e) {
					e.getStackTrace();
				}
			}
		}
	}
	/** 
   * 以字符為單位讀寫文件內容 
   * 
   * @param filePath 
   */
	public static void readFileByCharacter(String filePath) {
		File file = new File(filePath);
		// FileReader:用來讀取字符文件的便捷類。 
		FileReader reader = null;
		FileWriter writer = null;
		try {
			reader = new FileReader(file);
			writer = new FileWriter("d:/work/readFileByCharacter.txt");
			int temp;
			while ((temp = reader.read()) != -1) {
				writer.write((char)temp);
			}
		}
		catch (IOException e) {
			e.getStackTrace();
		}
		finally {
			if (reader != null && writer != null) {
				try {
					reader.close();
					writer.close();
				}
				catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/** 
   * 以行為單位讀寫文件內容 
   * 
   * @param filePath 
   */
	public static void readFileByLine(String filePath) {
		File file = new File(filePath);
		// BufferedReader:從字符輸入流中讀取文本,緩沖各個字符,從而實現字符、數組和行的高效讀取。 
		BufferedReader bufReader = null;
		BufferedWriter bufWriter = null;
		try {
			// FileReader:用來讀取字符文件的便捷類。 
			bufReader = new BufferedReader(new FileReader(file));
			bufWriter = new BufferedWriter(new FileWriter("d:/work/readFileByLine.txt"));
			// buf = new BufferedReader(new InputStreamReader(new 
			// FileInputStream(file))); 
			String temp = null;
			while ((temp = bufReader.readLine()) != null) {
				bufWriter.write(temp+"\n");
			}
		}
		catch (Exception e) {
			e.getStackTrace();
		}
		finally {
			if (bufReader != null && bufWriter != null) {
				try {
					bufReader.close();
					bufWriter.close();
				}
				catch (IOException e) {
					e.getStackTrace();
				}
			}
		}
	}
	/** 
   * 使用Java.nio ByteBuffer字節將一個文件輸出至另一文件 
   * 
   * @param filePath 
   */
	public static void readFileByBybeBuffer(String filePath) {
		FileInputStream in = null;
		FileOutputStream out = null;
		try {
			// 獲取源文件和目標文件的輸入輸出流  
			in = new FileInputStream(filePath);
			out = new FileOutputStream("d:/work/readFileByBybeBuffer.txt");
			// 獲取輸入輸出通道 
			FileChannel fcIn = in.getChannel();
			FileChannel fcOut = out.getChannel();
			ByteBuffer buffer = ByteBuffer.allocate(1024);
			while (true) {
				// clear方法重設緩沖區,使它可以接受讀入的數據 
				buffer.clear();
				// 從輸入通道中將數據讀到緩沖區 
				int r = fcIn.read(buffer);
				if (r == -1) {
					break;
				}
				// flip方法讓緩沖區可以將新讀入的數據寫入另一個通道  
				buffer.flip();
				fcOut.write(buffer);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if (in != null && out != null) {
				try {
					in.close();
					out.close();
				}
				catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	public static long getTime(){
		return System.currentTimeMillis();
	}
	public static void main(String args[]) {
		long time1 = getTime() ;
		// readFileByByte(FILE_PATH);// 8734,8281,8000,7781,8047 
		// readFileByCharacter(FILE_PATH);// 734, 437, 437, 438, 422 
		// readFileByLine(FILE_PATH);// 110, 94, 94, 110, 93 
		readFileByBybeBuffer(FILE_PATH);
		// 125, 78, 62, 78, 62 
		long time2 = getTime() ;
		System.out.println(time2-time1);
	}
}

在main方法中,調用各方法之后,有五組數據,分辨是我5次讀寫文件測試出來的時間(毫秒)。

關于Java.nio請參考:https://www.jb51.net/article/131338.htm

個人測試:

public static void main(String args[]) {
	long time1 = getTime() ;
	//     readFileByByte(FILE_PATH);   //2338,2286 
	//     readFileByCharacter(FILE_PATH);//160,162,158 
	//     readFileByLine(FILE_PATH);   //46,51,57 
	//    readFileByBybeBuffer(FILE_PATH);//19,18,17 
	//    readFileByBybeBuffer(FILE_PATH);//2048: 11,13 
	//    readFileByBybeBuffer(FILE_PATH);//1024*100 100k,711k: 6,6 
	//    readFileByBybeBuffer(FILE_PATH);//1024*100 100k,1422k: 7 
	//    readFileByBybeBuffer(FILE_PATH);//1024*100 100k,9951k: 49,48 
	//    readFileByBybeBuffer(FILE_PATH);//1024*1000 1M,711k: 7,7 
	//    readFileByBybeBuffer(FILE_PATH);//1024*1000 1M,1422k: 7,8 
	//    readFileByBybeBuffer(FILE_PATH);//1024*1000 1M,9951k: 48,49 
	//    readFileByBybeBuffer(FILE_PATH);//1024*10000 10M,711k: 21,13,17 
	//    readFileByBybeBuffer(FILE_PATH);//1024*10000 10M,1422k: 16,17,14,15 
	//    readFileByBybeBuffer(FILE_PATH);//1024*10000 10M,9951k:64,60 
	long time2 = getTime() ;
	System.out.println(time2-time1);
}

“Java文件讀寫IO/NIO及性能的比較”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注創新互聯網站,小編將為大家輸出更多高質量的實用文章!


網站名稱:Java文件讀寫IO/NIO及性能的比較
路徑分享:http://www.xueling.net.cn/article/pdgiod.html

其他資訊

在線咨詢
服務熱線
服務熱線:028-86922220
TOP
主站蜘蛛池模板: 少妇人妻14页_麻花色 | 91性视频 | www.日韩在线视频 | 中文字幕一区二区三区精华液 | 天天做天天爱天天综合网 | 妖精森林的救世主动漫在线观看 | 国产精品美女久久久久av爽金牛 | 久久99亚洲精品久久99 | 亚洲精品欧美二区三区中文字幕 | 一级做a爰片久久高潮 | 教师学生毛片一区二区三区 | 欧美成人精品三级网站 | 91麻豆国产福利在线观看宅福利 | 欧美精品网站在线观看 | 综合自拍 | 国产特黄一级 | 亚洲欧美日韩国产综合 | 亚洲日韩国产一区二区三区 | A片在线观看免费视频网站 亚洲精品久久久久国产 | 免费观看性生交大片3区 | 大JI巴好深好爽又大又粗视频 | 91九色porny老版 | 韩国免费A级作爱片无码 | 亚洲国产福利一区二区三区 | 久久精品亚洲94久久精品 | 一级网站在线观看 | 蜜桃视频在线播放 | 成人国产第一区在线观看 | a级片免费看 | 国产亚洲精品久久飘花 | 最新激情网站 | 粉色视频在线免费观看 | 一级黄色香蕉视频 | 2023国产一二三区日本精品2022 | 在线日本欧美 | 国产区在线观看成人精品 | 凤隐天下60集全免费播放在线观看 | 亚洲性夜夜时 | 久久ww| av在线免费播放网址 | 狠狠色综合激起情丁香色五月 |