Java中IO流 字節(jié)流實(shí)例詳解
Java中IO流 字節(jié)流實(shí)例詳解
IO流(輸入流、輸出流),又分為字節(jié)流、字符流。
流是磁盤或其它外圍設(shè)備中存儲(chǔ)的數(shù)據(jù)的源點(diǎn)或終點(diǎn)。
輸入流:程序從輸入流讀取數(shù)據(jù)源。數(shù)據(jù)源包括外界(鍵盤、文件、網(wǎng)絡(luò)…),即是將數(shù)據(jù)源讀入到程序的通信通道。
輸出流:程序向輸出流寫入數(shù)據(jù)。將程序中的數(shù)據(jù)輸出到外界(顯示器、打印機(jī)、文件、網(wǎng)絡(luò)…)的通信通道。
字節(jié)流
1.InputStream、OutputStream
InputStream抽象了應(yīng)用程序讀取數(shù)據(jù)的方式
OutputStream抽象了應(yīng)用程序?qū)懗鰯?shù)據(jù)的方式
2.讀到文件結(jié)尾,稱為EOF = end,讀到-1就讀到結(jié)尾
3.輸入流基本方法
int b = in.read();讀取一個(gè)字節(jié),無符號(hào)填充到int的低八位.-1是EOF
int.read(byte[] buf)讀取數(shù)據(jù)填充到字節(jié)數(shù)組buf
int.read(byte[] buf, int start, int size)讀取數(shù)據(jù)填充到字節(jié)數(shù)組buf,從buf的start位置開始存儲(chǔ)size長度的數(shù)據(jù)
4.輸出流基本方法
out.write(int b);寫出一個(gè)byte到流,b的低八位
out.write(byte[] buf);將buf字節(jié)數(shù)組都寫入到流
out.write(byte[] buf, int start, int size);字節(jié)數(shù)組buf從start位置開始寫size長度的字節(jié)到流
5.FileInputStream是InputStream的子類,具體實(shí)現(xiàn)了在文件上讀取數(shù)據(jù)
6.FileOutputStream是OutputStream的子類,實(shí)現(xiàn)了向文件中寫出字節(jié)數(shù)據(jù)的方法
FileInputStream的demo:
package com.test.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class IOUtils {
/**
* 讀取指定文件內(nèi)容,按照十六進(jìn)制輸出到控制臺(tái)
* 并且每輸出10個(gè)byte換行
* @param fileName
* @throws IOException
*/
public static void printHex(String fileName) throws IOException {
//把文件作為字節(jié)流進(jìn)行讀操作
FileInputStream in = new FileInputStream(fileName);
int b;
int i = 1;
while ((b = in.read()) != -1) {
if (b <= 0xf) {
System.out.print("0");
}
System.out.print(Integer.toHexString(b) + " ");
if (i % 10 == 0) {
System.out.println("");
}
i++;
}
in.close();
}
public static void printHexByByteArray(String fileName) throws IOException {
FileInputStream in = new FileInputStream(fileName);
byte[] buf = new byte[20*1024];
//如果字節(jié)數(shù)組夠大,可以一次性讀完
//從in中批量讀取字節(jié),放入到buf這個(gè)字節(jié)數(shù)組中,從第0個(gè)位置開始放,最多放buf.length個(gè),返回的是讀到的字節(jié)的個(gè)數(shù)
/* int bytes = in.read(buf, 0, buf.length);
int j = 1;
for(int i = 0;i < bytes; i++) {
if (buf[i] <= 0xf) {
System.out.print("0");
}
System.out.print(Integer.toHexString(buf[i] & 0xff) + " ");
if (j % 10 == 0) {
System.out.println("");
}
j++;
} */
//如果字節(jié)數(shù)組不夠大,不能一次性讀完
int bytes = 0;
int j = 1;
while ((bytes = in.read(buf, 0, buf.length)) != -1) {
for (int i = 0; i <bytes; i++) {
if (buf[i] <= 0xf) {
System.out.print("0");
}
System.out.print(Integer.toHexString(buf[i] & 0xff) + " ");
if (j % 10 == 0) {
System.out.println("");
}
j++;
}
}
}
}
FileOutputStream的demo:
package com.test.io;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputDemo {
public static void main(String[] args) throws IOException {
//如果該文件不存在,則直接創(chuàng)建,如果存在,刪除后創(chuàng)建。(如果第二個(gè)參數(shù)為 true,則將字節(jié)寫入文件末尾處,而不是寫入文件開始處。)
FileOutputStream out = new FileOutputStream("F:\\javaio\\out.dat");
out.write('A');//寫入了‘A'的低八位(一次只寫入一個(gè)字節(jié))
int a = 10;
out.write(a >>> 24);
out.write(a >>> 16);
out.write(a >>> 8);
out.write(a);
byte[] b = "10".getBytes();
out.write(b);
out.close();
IOUtils.printHex("F:\\javaio\\out.dat");
}
}
7.DataOutputStream和DataInputStream,對流功能的擴(kuò)展,可以更加方便的讀取int,long,字符等類型數(shù)據(jù)。
package com.test.io;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataOutputDemo {
public static void main(String[] args) throws IOException {
String file = "F:\\javaio\\b.txt";
DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
dos.writeInt(10);
dos.writeInt(-10);
dos.writeLong(10l);
dos.writeDouble(10.5);
dos.writeUTF("你好");
dos.writeChars("中國");
dos.close();
IOUtils.printHex(file);
}
}
運(yùn)行結(jié)果:
00 00 00 0a ff ff ff f6 00 00 00 00 00 00 00 0a 40 25 00 00 00 00 00 00 00 06 e4 bd a0 e5 a5 bd 4e 2d 56 fd
其中,00 06兩個(gè)字節(jié)是“你好”這兩個(gè)中文的字節(jié)個(gè)數(shù)。
package com.test.io;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class DataInputDemo {
public static void main(String[] args) throws IOException {
String file = "F:\\javaio\\b.txt";
DataInputStream dis = new DataInputStream(new FileInputStream(file));
int i = dis.readInt();
System.out.println(i);
i = dis.readInt();
System.out.println(i);
long l = dis.readLong();
System.out.println(l);
double d = dis.readDouble();
System.out.println(d);
String s = dis.readUTF();
System.out.println(s);
dis.close();
}
}
運(yùn)行結(jié)果:
10 -10 10 10.5 你好
8.BufferedInputStream&BufferedOutputStream,這兩個(gè)流類為IO提供了帶緩沖區(qū)的操作,一般打開文件進(jìn)行寫入或讀取操作時(shí),都會(huì)加上緩沖,這種流模式提高了IO的性能。
文件的拷貝:
package com.test.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class IOUtils {/**
* 拷貝文件,字節(jié)批量讀取
* @param srcFile 源文件
* @param destFile 目標(biāo)文件
* @throws IOException
*/
public static void copyFile(File srcFile, File destFile) throws IOException {
if (!srcFile.exists()) {
throw new IllegalArgumentException("文件" + srcFile + "不存在");
}
if (!srcFile.isFile()) {
throw new IllegalArgumentException(srcFile + "不是文件");
}
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
byte[] buf = new byte[10*1024];
int b;
while ((b = in.read(buf, 0, buf.length)) != -1) {
out.write(buf,0,b);
out.flush();//最好加上,刷新此輸出流并強(qiáng)制寫出所有緩沖的輸出字節(jié)。
}
in.close();
out.close();
}
/**
* 拷貝文件,利用帶緩沖的字節(jié)流
* @param srcFile
* @param destFile
* @throws IOException
*/
public static void copyFileByBuffer(File srcFile, File destFile) throws IOException {
if (!srcFile.exists()) {
throw new IllegalArgumentException("文件" + srcFile + "不存在");
}
if (!srcFile.isFile()) {
throw new IllegalArgumentException(srcFile + "不是文件");
}
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
BufferedInputStream bis = new BufferedInputStream(in);
BufferedOutputStream bos = new BufferedOutputStream(out);
int c;
while ((c = bis.read()) != -1) {
bos.write(c);
bos.flush();
}
bis.close();
bos.close();
}
/**
* 拷貝文件,通過單字節(jié)讀取
* @param srcFile
* @param destFile
* @throws IOException
*/
public static void copyFileByByte(File srcFile, File destFile) throws IOException {
if (!srcFile.exists()) {
throw new IllegalArgumentException("文件" + srcFile + "不存在");
}
if (!srcFile.isFile()) {
throw new IllegalArgumentException(srcFile + "不是文件");
}
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
int c;
while ((c = in.read()) != -1) {
out.write(c);
out.flush();
}
in.close();
out.close();
}
}
測試文件拷貝:
package com.test.io;
import java.io.File;
import java.io.IOException;
public class IOUtilsTest {
public static void main(String[] args) {
//IOUtils.printHex("D:\\javaProgram\\Hello.java");
try {
long start = System.currentTimeMillis();
//IOUtils.copyFile(new File("F:\\javaio\\1.mp3"), new File("F:\\javaio\\2.mp3"));//211ms
//IOUtils.copyFileByBuffer(new File("F:\\javaio\\1.mp3"), new File("F:\\javaio\\3.mp3"));//18583ms
IOUtils.copyFileByByte(new File("F:\\javaio\\1.mp3"), new File("F:\\javaio\\4.mp3"));//37822ms
long end = System.currentTimeMillis();
System.out.println(end - start);
} catch (IOException e) {
e.printStackTrace();
}
}
}
根據(jù)以上測試看出,文件拷貝,最快的方式是通過字節(jié)的批量讀取。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
OutOfMemoryError內(nèi)存不足和StackOverflowError堆棧溢出示例詳解
這篇文章主要為大家介紹了OutOfMemoryError內(nèi)存不足和StackOverflowError堆棧溢出示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Maven打包SpringBoot工程的實(shí)現(xiàn)示例
在使用Spring Boot和Maven的項(xiàng)目中,你可以使用Maven來打包你的項(xiàng)目,本文主要介紹了Maven打包SpringBoot工程的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05
常用數(shù)據(jù)庫的驅(qū)動(dòng)程序及JDBC URL分享
這篇文章主要介紹了常用數(shù)據(jù)庫的驅(qū)動(dòng)程序及 JDBC URL,需要的朋友可以看下2014-01-01
Netty網(wǎng)絡(luò)編程實(shí)戰(zhàn)之搭建Netty服務(wù)器
Netty是JBOSS開源的一款NIO網(wǎng)絡(luò)編程框架,可用于快速開發(fā)網(wǎng)絡(luò)的應(yīng)用。Netty是一個(gè)異步的、基于事件驅(qū)動(dòng)的網(wǎng)絡(luò)應(yīng)用框架,用于快速開發(fā)高性能的服務(wù)端和客戶端。本文將詳細(xì)說說如何搭建Netty服務(wù)器,需要的可以參考一下2022-10-10
Java和Rust實(shí)現(xiàn)JSON序列化互轉(zhuǎn)的解決方案詳解
這篇文章主要為大家詳細(xì)介紹了Java和Rust實(shí)現(xiàn)JSON序列化互轉(zhuǎn)的解決方案,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
基于mybatis中數(shù)組傳遞注意事項(xiàng)
這篇文章主要介紹了mybatis中數(shù)組傳遞注意事項(xiàng),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
詳解如何在SpringBoot中優(yōu)雅地重試調(diào)用第三方API
在實(shí)際的應(yīng)用中,我們經(jīng)常需要調(diào)用第三方API來獲取數(shù)據(jù)或執(zhí)行某些操作,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12

