老熟女激烈的高潮_日韩一级黄色录像_亚洲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
主站蜘蛛池模板: 国产精品99久久久久久人红楼 | 91久久久爱一区二区三区 | 香蕉久热 | 熟妇人妻中文a∨无码 | 一区在线视频观看 | 国产精品国产三级国产专播一 | 国产一区二区久久久 | 久久精品国产亚州Av果冻传媒 | 女人高潮av国产伦理剧 | 日本高清VA在线播放 | 日韩高清片| 天堂在线成人 | 国产成年无码a∨片在线韩国 | 国产欧美日韩视频免费 | 麻豆AV无码精品一区二区 | 麻豆视频在线观看免费网站 | 中文字幕一线产区和二线 | 熟妇高潮一区二区三区 | 久久婷婷五月综合色国产香蕉 | 国产情侣一区二区三区 | 亚洲熟妇无码AV在线观看 | 国产精品三p一区二区 | xxx麻豆 | 男人操女人视频在线观看 | 国产hsck在线亚洲 | 男人自慰泄欲飞机GAY2022 | 伊人天堂av无码av日韩av | 亚洲欧美精品在线 | a级黄色一级片 | 欧美群妇大交群的观看方式 | 亚洲国产精品激情综合图片 | 午夜福利无码不卡在线观看 | 天天操天天操天天操天天 | 国产真实乱子伦精品视频 | 3344成人免费高清免费视频 | 精品福利一区二区三区免费视频 | 亚洲国产成人久久精品软件 | 无码专区狠狠躁躁天天躁 | 免费1级a做爰片观看 | 中出在线视频 | 亚洲视频一 |