java中io流案例_Java IO流的简单使用 通俗易懂 超详细 【内含案例】
IO流簡單使用
輸入和輸出是相對于程序來說的,讀取到程序中叫做輸入,寫到文件中叫輸出.
InputStream 字節輸入流
InputStream 字節輸入流基類,是字節輸入流所有類的超類
// 從輸入流中讀取數據中下一個字節
abstract int read();
// 讀取一定數量的字節,并將其緩沖到 b 數組中
int read(byte[] b);
// 讀取最多 len 個字節,并并將其緩沖到 b 數組中
int read(byte[] b, int off, int len);
// 跳過或丟棄數據中 n 個字節
long skip(lone n);
// 關閉流并釋放流關系中所有資源
void close();
OutputStream 字節輸出流
OutputStream 字節輸出流基類,是字節輸出流所有類的超類
// 講指定的字節寫入輸出流
abstract void write(int b);
// 將 b.length 個字節從 b 中寫入輸出流中
void write(byte[] b);
// 將 b 數組下標 off(b[off]) 后 len 個字節寫入輸出流
void write(byte[] b, int off, int len)
// 刷新輸出流并寫出所有緩沖的輸出字節數據
void flush();
// 關閉輸出流,并釋放輸出流相關的資源
// 關閉之前,使用flush()寫出緩沖的字節
void close();
Reader 字符輸入流
Reader 字符輸入流,是讀取字符流的抽象類
// 讀取單個字符
int read();
// 將字符讀入數組
int read(char[] cbuf);
// 將 len 個字符讀到char數組下標 off 后面
abstract int read(char[] cbuf, int off, int len);
// 跳過n個字符
long skip(long n);
// 關閉字符流,并釋放相關的資源
abstract void close();
Writer 字符輸出流
Writer 字符輸出流,是寫入字符流的抽象類
// 將 char 數組寫入字符流
void write(char[] cbuf);
// 將 char 數組下標 off 后 len 個字符寫入字符流
abstract void read(char[] cbuf, int off, int len);
// 寫入單個字符
void write(int c);
// 寫入字符串
void write(Stirng str);
// 寫入字符串的某一部分
// 也是將 string 轉成 char 然后執行 read(char[] cbuf, int off, int len);
void write(String str, int off, int len);
// 將指定的字符序列附加到此 Wirter
Writer append(CharSequence csq);
// 將指定字符序列的子序列追加到此 Writer
Writer append(CharSequence csq, int start, int end)
// 將指定字符追加到此 Writer
Writer append(char c);
// 刷新流的緩沖
abstract void flush();
// 關閉流,但是要刷新該流,否則關閉時會報錯 IOException
abstract void close();
實例
實例不全,需自己動手探討其中的奧秘
實例中使用了junit等注解,我在這里貼一。也可以不用,就是吧log換成println多寫幾個類
org.projectlombok
lombok
1.18.12
ch.qos.logback
logback-classic
1.2.3
junit
junit
4.13
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import java.io.*;
/**
* @author http://cnblogs.com/beixuan
*/
@Slf4j
public class StreamTest {
private static String fileName = "D:/Stream.txt";
private static String fileName1 = "D:/Reader.txt";
private static String fileName2 = "D:/紅色高跟鞋.mp3";
@Before
public void getFile(){
File file = new File(fileName);
if (!file.exists()){
try {
//創建文件
file.createNewFile();
log.debug("創建文件成功:{}", fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
File file1 = new File(fileName1);
if (!file1.exists()){
try {
//創建文件
file1.createNewFile();
log.debug("創建文件成功:{}", fileName1);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 字節輸出流
*/
@Test
public void testOutputStream(){
FileOutputStream fos = null;
try {
fos = new FileOutputStream(fileName);
fos.write("Hello world".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
//刷新緩沖區
if (fos != null) {
try {
fos.flush();
log.debug("寫入數據成功");
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 字節輸入流
*/
@Test
public void testInputStream(){
FileInputStream fis = null;
try {
fis = new FileInputStream(fileName);
int i = 0;
StringBuffer sb = new StringBuffer();
while ((i = fis.read()) != -1){
// i是字符對應的ASCII碼
sb.append((char) i);
}
log.debug("{}:\n{}", fileName, sb.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 字符輸出流
*/
@Test
public void testWriter(){
OutputStreamWriter osw = null;
try {
osw = new OutputStreamWriter(new FileOutputStream(fileName1));
osw.write("可以輸出中文哦!\n\r\t還又'\\n\\r\\t'");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (osw != null){
try {
osw.flush();
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 字符輸入流
*/
@Test
public void testReader(){
// InputStreamReader 轉換流
InputStreamReader isr = null;
try {
isr = new InputStreamReader(new FileInputStream(fileName2));
StringBuffer sb = new StringBuffer();
int i;
while ((i = isr.read()) != -1){
sb.append((char)i);
}
log.debug("{}:\n{}", fileName2, sb);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (isr != null){
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 高效流對比
*/
@Test
public void IoEquals() throws IOException{
//操作的是一個3.19MB的音頻文件 它們之間文件越大,效果就越明顯
/***********************************************************************/
FileInputStream fis1 = new FileInputStream(fileName2);
FileOutputStream fos1 = new FileOutputStream(fileName);
int i;
long startTime = System.currentTimeMillis();
while ((i = fis1.read()) != -1){
fos1.write(i);
}
long endTime = System.currentTimeMillis();
log.debug("第一種高效流:{}", endTime - startTime);
fos1.close();
fis1.close();
/***********************************************************************/
FileInputStream fis2 = new FileInputStream(fileName2);
FileOutputStream fos2 = new FileOutputStream(fileName);
byte[] bytes = new byte[1024];
startTime = System.currentTimeMillis();
while ((i = fis2.read(bytes)) != -1){
fos2.write(bytes, 0, i);
}
endTime = System.currentTimeMillis();
log.debug("第二種高效流:{}", endTime - startTime);
fos2.close();
fis2.close();
/***********************************************************************/
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName2));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName));
startTime = System.currentTimeMillis();
while ((i = bis.read(bytes)) != -1){
bos.write(bytes, 0, i);
}
endTime = System.currentTimeMillis();
log.debug("第三種高效流:{}", endTime - startTime);
bos.close();
bis.close();
/**
* 第一種高效流:20186
* 第三種高效流:30
* 第二種高效流:10
* 這么對比下 BufferedInputStream BufferedOutputStream 是最好的配合
*/
}
}
小結
字節流常用于圖片、音頻、視頻文件及PPT、Word文件.
字符流常用于處理文本類型的文件如txt、ini文件等
字節流也可以處理純文本文件,但字符流不可以處理圖片視頻等非純文本類型的文件
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的java中io流案例_Java IO流的简单使用 通俗易懂 超详细 【内含案例】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java super实例_java S
- 下一篇: 数组的升序 java_java – 以升