日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java基础10(IO流)-字节流

發(fā)布時間:2025/3/20 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java基础10(IO流)-字节流 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

IO流

輸入與輸出【參照物是程序】

如果從鍵盤、文件、網(wǎng)絡甚至是另一個進程(程序或系統(tǒng))將數(shù)據(jù)讀入到程序或系統(tǒng)中,稱為輸入
如果是將程序或系統(tǒng)中的數(shù)據(jù)寫到屏幕、硬件上的文件、網(wǎng)絡上的另一端或者是一個進程(程序或系統(tǒng)),稱為輸出

IO流的分類

  • 根據(jù)數(shù)據(jù)流向不同分為:輸入流和輸出流
    輸入流: 程序可以從中讀取數(shù)據(jù)的流
    輸出流: 程序能向其中寫入數(shù)據(jù)的流
  • 根據(jù)數(shù)據(jù)處理類不同分為:字節(jié)流和字符流
    字節(jié)流:以字節(jié)為單位傳輸數(shù)據(jù)的流
    字符流:以字符為單位傳輸數(shù)據(jù)的流

注:數(shù)據(jù)所在文件若能用win下記事本打開則用字符流

IO流類結構圖


讀數(shù)據(jù)

InputStream讀取數(shù)據(jù)

int read() : 從此輸入流中讀取一個數(shù)據(jù)字節(jié) int read(byte[] b): 從此輸入流中將最多 b.length 個字節(jié)的數(shù)據(jù)讀入一個 byte 數(shù)組中。返回值是一次讀取字節(jié)數(shù)組的個數(shù) int read(byte[] b, int off, int len): 從此輸入流中將最多 len 個字節(jié)的數(shù)據(jù)讀入一個 byte 數(shù)組中

InputStream讀取數(shù)據(jù)(一次讀取一個字節(jié))

import java.io.FileInputStream; import java.io.IOException;public class FileInputStreamDemo{public static void main(String[] args) throws IOException{FileInputStream file = new FileInputStream("/home/hadoop/1.txt"); /*int by = file.read();//一次讀取一個字節(jié)System.out.println(by);System.out.println((char) by); *///方式1(一次讀取一個字節(jié))int by = 0;while((by = file.read()) != -1){ // System.out.print(by);//不轉(zhuǎn)的話讀出來全是數(shù)字System.out.print((char)by);//這里需要注意文件中如果有中文將出問題,因為將中文也轉(zhuǎn)成char了;要讀取的文件中有換行符,直接讀取過來,所以這里不需要換行符}file.close();} }

InputStream讀取數(shù)據(jù)(一次讀取一個字節(jié)數(shù)組)

import java.io.FileInputStream; import java.io.IOException;public class FileInputStreamDemo2{public static void main(String[] args)throws IOException{FileInputStream fis = new FileInputStream("/home/hadoop/1.txt");byte[] bys = new byte[1024];int len = 0;while((len = fis.read(bys)) != -1){ //讀取到的實際長度是-1就沒有數(shù)據(jù)了System.out.print(new String(bys,0,len));//進行轉(zhuǎn)換}fis.close();} }

BufferedInputStream讀取數(shù)據(jù)(一次讀一個字節(jié))

import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException;public class BufferedInputStreamDemo{public static void main(String[] args)throws IOException{BufferedInputStream bis = new BufferedInputStream(new FileInputStream("/home/hadoop/1.txt"));int by =0;while((by = bis.read()) != -1){System.out.print((char)by);}bis.close();} }

BufferedInputStream讀取數(shù)據(jù)(一次讀一個字節(jié)數(shù)組)

import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException;public class BufferedInputStreamDemo{public static void main(String[] args)throws IOException{BufferedInputStream bis = new BufferedInputStream(new FileInputStream("/home/hadoop/1.txt"));byte[] bys = new byte[1024];int len = 0;while((len = bis.read(bys)) != -1){System.out.print(new String(bys,0,len));}bis.close();} }

寫數(shù)據(jù)

OutputStream寫數(shù)據(jù)

void write(byte[] b):將 b.length 個字節(jié)從指定的 byte 數(shù)組寫入此輸出流 void write(byte[] b, int off, int len): 將指定 byte 數(shù)組中從偏移量 off 開始的 len 個字節(jié)寫入此輸出流。 void write(int b): 將指定的字節(jié)寫入此輸出流。

FileOutputStream寫數(shù)據(jù)(寫一個字節(jié))

import java.io.FileOutputStream; import java.io.IOException;public class FileOutputStreamDemo{public static void main(String[] args)throws IOException{ /* //創(chuàng)建字節(jié)輸出流對象File file = new File("/home/hadoop/1.txt");FileOutputStream fos = new FileOutputStream(File file); */FileOutputStream fos = new FileOutputStream("/home/hadoop/wu.txt",true);//true表示追加數(shù)據(jù)fos.write("wujiadong".getBytes());//將字符串轉(zhuǎn)成字節(jié)數(shù)組fos.write(97);//寫一個字節(jié)fos.close();} }

FileOutputStream寫數(shù)據(jù)(寫一個字節(jié)數(shù)組和寫字節(jié)數(shù)組的部分)

import java.io.FileOutputStream; import java.io.IOException;public class FileOutputStreamDemo{public static void main(String[] args)throws IOException{ /* //創(chuàng)建字節(jié)輸出流對象File file = new File("/home/hadoop/1.txt");FileOutputStream fos = new FileOutputStream(File file); */FileOutputStream fos = new FileOutputStream("/home/hadoop/wu.txt",true);byte[] bys = {97,98,99,100}; fos.write(bys);//寫一個字節(jié)數(shù)組fos.write(bys,1,3);//寫一個字節(jié)數(shù)組的一部分 fos.close();} }

BufferedOutputStream寫數(shù)據(jù)

import java.io.FileOutputStream; import java.io.IOException; import java.io.BufferedOutputStream;public class BufferedOutputStreamDemo{public static void main(String[] args)throws IOException{// FileOutputStream fis = new FileOutputStream("/home/hadoop/wu2.txt");// BufferedOutputStream bos = new BufferedOutputStream(fis);//簡單寫法BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("/home/hadoop/wu3.txt"));//寫數(shù)據(jù)bos.write("hello,wujiadong".getBytes());//釋放資源bos.close();} }

讀寫數(shù)據(jù)練習

復制數(shù)據(jù)

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;public class CopyFileDemo{public static void main(String[] args) throws IOException {//封裝數(shù)據(jù)源FileInputStream fis = new FileInputStream("/home/hadoop/11.txt");//封裝目的地FileOutputStream fos = new FileOutputStream("/home/hadoop/a.txt");//復制數(shù)據(jù) int by = 0;while((by = fis.read()) != -1){fos.write(by);}//釋放資源fis.close();fos.close();} } --------------------------------------- 注:這種方法一次讀一個字節(jié),比較慢 ----------------------------------------

復制數(shù)據(jù)

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;public class CopyFileDemo1{public static void main(String[] args)throws IOException{//封裝數(shù)據(jù)源FileInputStream fis = new FileInputStream("/home/hadoop/11.txt");FileOutputStream fos = new FileOutputStream("/home/hadoop/wu.txt");//復制數(shù)據(jù)byte[] bys = new byte[1024];int len = 0;while((len = fis.read(bys)) != -1){fos.write(bys,0,len);}//釋放資源fis.close();fos.close();} }

字節(jié)流4種方式復制文件

/** 字節(jié)流4種方式復制文件* 基本字節(jié)流一次讀寫一個字節(jié)* 基本字節(jié)流一次讀寫一個字節(jié)數(shù)組* 高效字節(jié)流一次讀寫一個字節(jié)* 高效字節(jié)流一次讀寫一個字節(jié)數(shù)組*/import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;import java.io.BufferedInputStream; import java.io.BufferedOutputStream;public class CopyDemo{public static void main(String[] args)throws IOException{long start = System.currentTimeMillis();// method1("/home/hadoop/1.txt","/home/hadoop/javatest/1.txt"); // method2("/home/hadoop/1.txt","/home/hadoop/javatest/1.txt"); // method3("/home/hadoop/1.txt","/home/hadoop/javatest/1.txt");method4("/home/hadoop/1.txt","/home/hadoop/javatest/1.txt");long end = System.currentTimeMillis();System.out.println("共耗時:"+(end - start)+"mm");}public static void method1(String srcString,String desString)throws IOException{FileInputStream fis = new FileInputStream(srcString);FileOutputStream fos = new FileOutputStream(desString);//方法1:讀寫一個字節(jié)int by = 0;while((by = fis.read()) != -1){fos.write(by);}fis.close();fos.close();} public static void method2(String srcString,String desString)throws IOException{FileInputStream fis = new FileInputStream(srcString);FileOutputStream fos = new FileOutputStream(desString);//方式2:讀取一個字節(jié)數(shù)組byte[] bys = new byte[1024];int len = 0;while((len = fis.read(bys)) != -1){fos.write(bys,0,len);}fis.close();fos.close(); } public static void method3(String srcString,String desString)throws IOException{BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcString));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desString));//方式3:高效讀取一個字節(jié)int by = 0;while((by = bis.read()) != -1){bos.write(by);}bis.close();bos.close();}public static void method4(String srcString,String desString)throws IOException{BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcString));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desString));//高效讀取一個字節(jié)數(shù)組byte[] bys = new byte[1024];int len = 0;while((len = bis.read(bys)) != -1){bos.write(bys,0,len);}bis.close();bos.close();} }

轉(zhuǎn)載于:https://www.cnblogs.com/wujiadong2014/p/6156233.html

總結

以上是生活随笔為你收集整理的java基础10(IO流)-字节流的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。