最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java IO流相關(guān)知識代碼解析

 更新時間:2017年12月18日 10:03:59   作者:在成為巨擘的路上  
這篇文章主要介紹了Java IO流相關(guān)知識代碼解析,具有一定借鑒價值,需要的朋友可以參考下。

一、IO流的分類

字符流

Reader
InputStreamReader(節(jié)點流)
BufferedReader(處理流)
Writer
OutputStreamWriter(節(jié)點流)
BufferedWriter(處理流)
PrintWriter

字節(jié)流

InputStream
FileInputStream(節(jié)點流)
BufferedInputStream(處理流)
ObjectInputStream(處理流)
PrintStream
OutputStream
FileOutputStream(節(jié)點流)
BufferedOutputStream(處理流)
ObjectOutputStream(處理流)

斷點處理的流

RandomAccessfile

二、IO流的用法

1、轉(zhuǎn)換流的用法

     FileInputStream in = new FileInputStream(newFile(""));     
      Readerreader = new InputStreamReader(in);//字節(jié)轉(zhuǎn)字符
      FileOutputStreamout = new FileOutputStream(newFile(""));
      Writer writer = new OutputStreamWriter(out);//字符轉(zhuǎn)字節(jié)

2、對象序列化,對象需要實現(xiàn)Serializable接口

      FileOutputStreamfileOutputStream = new FileOutputStream("C:\\Users\\lx\\Desktop\\Record.txt");
      ObjectOutputStreamobjectOutputStream = new ObjectOutputStream(fileOutputStream);
      objectOutputStream.writeObject(object);//向指定文件寫入對象object
      objectOutputStream.close();

      FileInputStreamfileInputStream = new FileInputStream("C:\\Users\\lx\\Desktop\\Record.txt");
      ObjectInputStreamobjectInputStream = new ObjectInputStream(fileInputStream);
      object = objectInputStream.readObject();//讀取得到對象object
      fileInputStream . lose();

3、斷點的運(yùn)用

public class Copy extends Thread{
	//可以利用多線程實現(xiàn)拷貝  
	longstart;
	longend;
	Filesorce;
	Filetargetdir;
	publicCopy() {
	}
	publicCopy(longstart,long end, File sorce, File targetdir) {
		//利用構(gòu)造方法傳遞需要拷貝的長度,拷貝開始位置,以及目標(biāo)文件和源文件
		super();
		this.start= start;
		this.end= end;
		this.sorce= sorce;
		this.targetdir= targetdir;
	}
	@Override
	   publicvoid run(){
		try{
			RandomAccessFilesouceRaf = new RandomAccessFile(sorce,"r");
			RandomAccessFiletargetRaf = new RandomAccessFile(newFile(targetdir,sorce.getName()),"rw");
			souceRaf.seek(start);
			targetRaf.seek(start);
			intlen= 0;
			byte[]bs = new byte[1024];
			longseek;
			System.out.println(start+"---->"+end+this.getName());
			while((len= souceRaf.read(bs))!=-1){
				targetRaf.write(bs, 0, len);
				seek= souceRaf.getFilePointer();
				//獲取斷點位置
				if(seek== end){
					break;
				}
			}
			targetRaf.close();
			souceRaf.close();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
}

4、字節(jié)流的用法

public class Test_InputStream {
	//利用字節(jié)流獲取文本文件內(nèi)容,但是容易出現(xiàn)問題
	/*  
  //可能出現(xiàn)int長度越界
  public static void main(String[] args) throws IOException {
    InputStream inputStream = new FileInputStream(new File("C:\\Users\\lx\\Desktop\\test\\33.txt"));
    byte[] b = new byte[inputStream.available()];  
    inputStream.read(b);
    String str = new String(b);
    System.out.println(str);
  }
*/
	//可能出現(xiàn)亂碼    
	public static void main(String[] args) throws IOException {
		File file = new File("C:\\Users\\lx\\Desktop\\test\\33.txt");
		InputStream inputStream = new FileInputStream(file);
		//統(tǒng)計每次讀取的實際長度
		int len = 0;
		//聲明每次讀取1024個字節(jié)
		byte[] b = new byte[2];
		StringBuffer sBuffer = new StringBuffer();
		while((len=inputStream.read(b))!=-1){
			sBuffer.append(new String(b,0,len));
		}
		System.out.println(sBuffer.toString());
	}
}
//利用字節(jié)流拷貝文件
public void copy(File sourceFile, File targetDir) {
	//
	FileInputStreamfileInputStream = null;
	FileOutputStreamfileOutputStream = null;
	fileInputStream= new FileInputStream(sourceFile);
	FiletargetFile = new File(targetDir,sourceFile.getName());
	fileOutputStream= new FileOutputStream(targetFile);
	byte[]b = new byte[1024];
	intlen = 0;
	while((len= fileInputStream.read(b)) != -1) {
		fileOutputStream.write(b, 0, len);
	}
}

5、緩存字符流的用法

publicstatic void main(String[] args) throws IOException {
	//緩存字符流實現(xiàn)寫入文件
	InputStreamin = System.in;
	Readerreader = new InputStreamReader(in);
	BufferedReaderbr = new BufferedReader(reader);
	BufferedWriterbw = new BufferedWriter(new FileWriter(new File("src/1.txt")));
	Strings="";
	while((s=br.readLine())!=null) {
		bw.write(s);
		bw.newLine();
		bw.flush();
		//字符流千萬不要忘了flush!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	}
}

總結(jié)

以上就是本文關(guān)于Java IO流相關(guān)知識代碼解析的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

相關(guān)文章

  • Spring AOP實現(xiàn)原理解析

    Spring AOP實現(xiàn)原理解析

    這篇文章主要為大家詳細(xì)介紹了Spring AOP的實現(xiàn)原理,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Java 高并發(fā)七:并發(fā)設(shè)計模型詳解

    Java 高并發(fā)七:并發(fā)設(shè)計模型詳解

    本文主要介紹Java高并發(fā) 并發(fā)設(shè)計模型的知識,這里主要講解 1. 什么是設(shè)計模式 2. 單例模式 3. 不變模式 4. Future模式 5. 生產(chǎn)者消費(fèi)者,有需要的小伙伴可以參考下
    2016-09-09
  • 微信公眾號開發(fā)之回復(fù)圖文消息java代碼

    微信公眾號開發(fā)之回復(fù)圖文消息java代碼

    這篇文章主要為大家詳細(xì)介紹了微信公眾號開發(fā)之回復(fù)圖文消息java代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • 解決JDBC Connection Reset的問題分析

    解決JDBC Connection Reset的問題分析

    這篇文章主要介紹了解決JDBC Connection Reset的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Lombok的@Accessors使用說明

    Lombok的@Accessors使用說明

    這篇文章主要介紹了Lombok的@Accessors使用說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • 詳解如何解決SSM框架前臺傳參數(shù)到后臺亂碼的問題

    詳解如何解決SSM框架前臺傳參數(shù)到后臺亂碼的問題

    這篇文章主要介紹了詳解如何解決SSM框架前臺傳參數(shù)到后臺亂碼的問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • SpringBoot條件注解之@ConditionalOnClass等注解的使用場景分析

    SpringBoot條件注解之@ConditionalOnClass等注解的使用場景分析

    文章詳細(xì)介紹了SpringBoot中條件注解的體系,包括基本概念、@ConditionalOnClass等常用注解的工作原理和使用場景,文章還探討了條件注解的組合使用、實戰(zhàn)應(yīng)用以及最佳實踐,幫助開發(fā)者更好地理解和應(yīng)用條件注解,實現(xiàn)更靈活和智能的應(yīng)用配置,感興趣的朋友一起看看吧
    2025-03-03
  • java 使用Graphics2D在圖片上寫字

    java 使用Graphics2D在圖片上寫字

    這篇文章主要介紹了java 使用Graphics2D在圖片上寫字,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java不可變類機(jī)制淺析

    Java不可變類機(jī)制淺析

    所謂的不可變類是指這個類的實例一旦創(chuàng)建完成后,就不能改變其成員變量值。如JDK內(nèi)部自帶的很多不可變類:Interger、Long和String等。接下來通過本文給大家介紹Java不可變類機(jī)制,需要的朋友參考下
    2017-02-02
  • 淺談Mybatis SqlSession執(zhí)行流程

    淺談Mybatis SqlSession執(zhí)行流程

    本文主要介紹了淺談Mybatis SqlSession執(zhí)行流程,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07

最新評論

原平市| 凤台县| 禹城市| 汶上县| 怀柔区| 奇台县| 唐河县| 化州市| 湘潭县| 合山市| 永兴县| 济源市| 佛冈县| 通海县| 贞丰县| 交口县| 张北县| 通道| 延寿县| 丰宁| 邮箱| 灵台县| 兴仁县| 鹤岗市| 南澳县| 密云县| 安徽省| 日喀则市| 双江| 湖北省| 湘潭县| 富民县| 陵川县| 五指山市| 奉贤区| 翁源县| 全南县| 封开县| 光泽县| 巩留县| 乌鲁木齐市|