java基础10(IO流)-字节流
生活随笔
收集整理的這篇文章主要介紹了
java基础10(IO流)-字节流
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
IO流
輸入與輸出【參照物是程序】
如果從鍵盤、文件、網(wǎng)絡(luò)甚至是另一個(gè)進(jìn)程(程序或系統(tǒng))將數(shù)據(jù)讀入到程序或系統(tǒng)中,稱為輸入
如果是將程序或系統(tǒng)中的數(shù)據(jù)寫到屏幕、硬件上的文件、網(wǎng)絡(luò)上的另一端或者是一個(gè)進(jìn)程(程序或系統(tǒng)),稱為輸出
IO流的分類
- 根據(jù)數(shù)據(jù)流向不同分為:輸入流和輸出流
輸入流: 程序可以從中讀取數(shù)據(jù)的流
輸出流: 程序能向其中寫入數(shù)據(jù)的流 - 根據(jù)數(shù)據(jù)處理類不同分為:字節(jié)流和字符流
字節(jié)流:以字節(jié)為單位傳輸數(shù)據(jù)的流
字符流:以字符為單位傳輸數(shù)據(jù)的流
注:數(shù)據(jù)所在文件若能用win下記事本打開(kāi)則用字符流
IO流類結(jié)構(gòu)圖
讀數(shù)據(jù)
InputStream讀取數(shù)據(jù)
int read() : 從此輸入流中讀取一個(gè)數(shù)據(jù)字節(jié) int read(byte[] b): 從此輸入流中將最多 b.length 個(gè)字節(jié)的數(shù)據(jù)讀入一個(gè) byte 數(shù)組中。返回值是一次讀取字節(jié)數(shù)組的個(gè)數(shù) int read(byte[] b, int off, int len): 從此輸入流中將最多 len 個(gè)字節(jié)的數(shù)據(jù)讀入一個(gè) byte 數(shù)組中InputStream讀取數(shù)據(jù)(一次讀取一個(gè)字節(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();//一次讀取一個(gè)字節(jié)System.out.println(by);System.out.println((char) by); *///方式1(一次讀取一個(gè)字節(jié))int by = 0;while((by = file.read()) != -1){ // System.out.print(by);//不轉(zhuǎn)的話讀出來(lái)全是數(shù)字System.out.print((char)by);//這里需要注意文件中如果有中文將出問(wèn)題,因?yàn)閷⒅形囊厕D(zhuǎn)成char了;要讀取的文件中有換行符,直接讀取過(guò)來(lái),所以這里不需要換行符}file.close();} }InputStream讀取數(shù)據(jù)(一次讀取一個(gè)字節(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){ //讀取到的實(shí)際長(zhǎng)度是-1就沒(méi)有數(shù)據(jù)了System.out.print(new String(bys,0,len));//進(jìn)行轉(zhuǎn)換}fis.close();} }BufferedInputStream讀取數(shù)據(jù)(一次讀一個(gè)字節(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ù)(一次讀一個(gè)字節(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 個(gè)字節(jié)從指定的 byte 數(shù)組寫入此輸出流 void write(byte[] b, int off, int len): 將指定 byte 數(shù)組中從偏移量 off 開(kāi)始的 len 個(gè)字節(jié)寫入此輸出流。 void write(int b): 將指定的字節(jié)寫入此輸出流。FileOutputStream寫數(shù)據(jù)(寫一個(gè)字節(jié))
import java.io.FileOutputStream; import java.io.IOException;public class FileOutputStreamDemo{public static void main(String[] args)throws IOException{ /* //創(chuàng)建字節(jié)輸出流對(duì)象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);//寫一個(gè)字節(jié)fos.close();} }FileOutputStream寫數(shù)據(jù)(寫一個(gè)字節(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é)輸出流對(duì)象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);//寫一個(gè)字節(jié)數(shù)組fos.write(bys,1,3);//寫一個(gè)字節(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);//簡(jiǎn)單寫法BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("/home/hadoop/wu3.txt"));//寫數(shù)據(jù)bos.write("hello,wujiadong".getBytes());//釋放資源bos.close();} }讀寫數(shù)據(jù)練習(xí)
復(fù)制數(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");//復(fù)制數(shù)據(jù) int by = 0;while((by = fis.read()) != -1){fos.write(by);}//釋放資源fis.close();fos.close();} } --------------------------------------- 注:這種方法一次讀一個(gè)字節(jié),比較慢 ----------------------------------------復(fù)制數(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");//復(fù)制數(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種方式復(fù)制文件
/** 字節(jié)流4種方式復(fù)制文件* 基本字節(jié)流一次讀寫一個(gè)字節(jié)* 基本字節(jié)流一次讀寫一個(gè)字節(jié)數(shù)組* 高效字節(jié)流一次讀寫一個(gè)字節(jié)* 高效字節(jié)流一次讀寫一個(gè)字節(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("共耗時(shí):"+(end - start)+"mm");}public static void method1(String srcString,String desString)throws IOException{FileInputStream fis = new FileInputStream(srcString);FileOutputStream fos = new FileOutputStream(desString);//方法1:讀寫一個(gè)字節(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:讀取一個(gè)字節(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:高效讀取一個(gè)字節(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));//高效讀取一個(gè)字節(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
總結(jié)
以上是生活随笔為你收集整理的java基础10(IO流)-字节流的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ubuntu修改启动为文本模式
- 下一篇: BZOJ 3262 cdq分治 OR 树