Java編程中字節(jié)流與字符流IO操作示例
IO流基本概念
IO流用來處理設(shè)備之間的數(shù)據(jù)傳輸
Java對數(shù)據(jù)的操作是通過流的方式
Java用于操作流的對象都是在IO包上
流按操作數(shù)據(jù)分為兩種:字節(jié)流和字符流
流按流向分為:輸入流,輸出流。
字節(jié)流的抽象基類:InputStream,OutputStream
字符流的抽象基類:Reader,Writer
注:由這4個類派生出來的子類名稱都是以其父類名作為子類名的后綴。
如:InputStream的子類:FileInputStream
如:Reader的子類FileReader
如創(chuàng)建一個FileWriter對象,該對象一被初始化就必須要明確被操作的文件,而且該文件就會被創(chuàng)建到指定目錄下,如果該目錄下已有同名文件,將被覆蓋。
Demo :
package javase.day18;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterDemo {
public static void main(String[] args) {
FileWriter fw=null;
try {
fw = new FileWriter("C:\\java_test\\FileWriterTest.txt");
//調(diào)用write 方法,將字符串寫入到流中
fw.write("alex test23");
//刷新流對象中的緩沖中的數(shù)據(jù)
fw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(fw!=null){
//關(guān)閉流資源,但是關(guān)閉之前會刷新一次內(nèi)部的緩沖中的數(shù)據(jù)
fw.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
package javase.day18;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterDemo {
public static void main(String[] args) {
FileWriter fw=null;
try {
fw = new FileWriter("C:\\java_test\\FileWriterTest.txt");
//調(diào)用write 方法,將字符串寫入到流中
fw.write("alex test23");
//刷新流對象中的緩沖中的數(shù)據(jù)
fw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(fw!=null){
//關(guān)閉流資源,但是關(guān)閉之前會刷新一次內(nèi)部的緩沖中的數(shù)據(jù)
fw.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
打印Java文件的源代碼Demo code:
package javase.day18;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderTest {
public static void main(String[] args) {
try {
FileReader fr=new FileReader("C:\\java_test\\SystemDemo.java");
char[] buf=new char[1024];
int num=0;
while((num=fr.read(buf))!=-1){
System.out.println(new String(buf,0,num));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package javase.day18;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderTest {
public static void main(String[] args) {
try {
FileReader fr=new FileReader("C:\\java_test\\SystemDemo.java");
char[] buf=new char[1024];
int num=0;
while((num=fr.read(buf))!=-1){
System.out.println(new String(buf,0,num));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
復(fù)制文件Demo code:
copy_1() 使用的方法是讀取一個字符則寫入一個字符。
copy_2()使用的方法是把字符一次性讀取到一個字符數(shù)組中,最后再一次寫入到目標(biāo)文件。
package javase.day18;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyText {
public static void main(String[] args) {
try {
copy_1();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void copy_1() throws IOException{
FileWriter fw = new FileWriter("C:\\java_test\\Copy_SystemDemo.java");
FileReader fr = new FileReader("C:\\java_test\\SystemDemo.java");
int num=0;
while((num=fr.read())!=-1){
fw.write(num);
}
fw.close();
fr.close();
}
public static void copy_2() throws IOException{
FileWriter fw = new FileWriter("C:\\java_test\\Copy_SystemDemo.java");
FileReader fr = new FileReader("C:\\java_test\\SystemDemo.java");
int num=0;
char[] buf=new char[1024];
while((num=fr.read(buf))!=-1){
fw.write(buf,0,num);
}
fw.close();
fr.close();
}
}
package javase.day18;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyText {
public static void main(String[] args) {
try {
copy_1();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void copy_1() throws IOException{
FileWriter fw = new FileWriter("C:\\java_test\\Copy_SystemDemo.java");
FileReader fr = new FileReader("C:\\java_test\\SystemDemo.java");
int num=0;
while((num=fr.read())!=-1){
fw.write(num);
}
fw.close();
fr.close();
}
public static void copy_2() throws IOException{
FileWriter fw = new FileWriter("C:\\java_test\\Copy_SystemDemo.java");
FileReader fr = new FileReader("C:\\java_test\\SystemDemo.java");
int num=0;
char[] buf=new char[1024];
while((num=fr.read(buf))!=-1){
fw.write(buf,0,num);
}
fw.close();
fr.close();
}
}
字符流的緩沖區(qū):
緩沖區(qū)的出現(xiàn)提高了對數(shù)據(jù)的讀寫效率。
對應(yīng)類:BufferedWriter , BufferedReader .
緩沖區(qū)要結(jié)合流才可以使用。
在流的基礎(chǔ)上對流的功能進(jìn)行了增強(qiáng)。
IO流操作的基本規(guī)律:
1,明確源和目的:
源: 輸入流 InputStream , Reader
目的: 輸出流 OutputStream ,Writer
2,操作的數(shù)據(jù)是否是純文本:
是:字符流
否:字節(jié)流
即:(1) 當(dāng)為輸入字符流用Reader
(2) 當(dāng)為輸入字節(jié)流用InputStream
(3)當(dāng)為輸出字符流用Writer
(4)當(dāng)為輸出字節(jié)流用OutputStream
3,當(dāng)體系明確后,再明確要使用哪個具體的對象:
源設(shè)備:內(nèi)存,硬盤,鍵盤
目的設(shè)備:內(nèi)存,硬盤,控制臺
IO操作工具類
[1] String fileReaderStringHandle(String fileName)
將文件(由fileName指定)讀入到一個字符串;
[2] byte[] fileReaderByteHandle(String fileName)
將文件(由fileName指定)讀入到一個字節(jié)數(shù)組;
[3] void fileWriterHandle(String fileName, String text)
將字符串(由text指定)寫出到一個文件(由fileName指定)。
IOUtil.java
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class IOUtil {
/**
* 將文件讀入到一個String,利用FileReader+BufferedReader(提供readLine方法)
*
* @param fileName
* @return String
*/
public static String fileReaderStringHandle(String fileName) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new FileReader(new File(
fileName).getAbsoluteFile()));
try {
String s;
while ((s = in.readLine()) != null) {
sb.append(s);
sb.append("\n");
}
} finally {
in.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return sb.toString();
}
/**
* 使用FileInputStream+BufferedInputStream以byte的方式處理文件
*
* @param fileName
* @return byte[]
*/
public static byte[] fileReaderByteHandle(String fileName) {
byte[] data = null;
try {
BufferedInputStream bf = new BufferedInputStream(
new FileInputStream(fileName));
try {
data = new byte[bf.available()];
bf.read(data);
} finally {
bf.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return data == null ? new byte[] {} : data;
}
/**
* 將指定的text寫入到文件名為fileName的文件中
*
* @param fileName
* @param text
*/
public static void fileWriterHandle(String fileName, String text) {
try {
PrintWriter out = new PrintWriter(new File(fileName)
.getAbsoluteFile());
try {
out.print(text);
} finally {
out.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws IOException {
System.out.print(fileReaderStringHandle("src/IOUtil.java"));
for (byte b : fileReaderByteHandle("src/IOUtil.java"))
System.out.print(b);
fileWriterHandle("zj.txt",
fileReaderStringHandle("src/IOUtil.java"));
}
}
相關(guān)文章
Java讀取resources目錄下文件路徑的九種代碼示例教程
在Java開發(fā)中經(jīng)常需要讀取項目中resources目錄下的文件或獲取資源路徑,這篇文章主要給大家介紹了關(guān)于Java讀取resources目錄下文件路徑的九種代碼示例教程,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
SpringBoot 如何整合 ES 實現(xiàn) CRUD 操作
這篇文章主要介紹了SpringBoot 如何整合 ES 實現(xiàn) CRUD 操作,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-10-10
Apache Commons fileUpload文件上傳多個示例分享
這篇文章主要為大家分享了Apache Commons fileUpload文件上傳4個示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
使用RabbitMQ實現(xiàn)延時消息自動取消的案例詳解
這篇文章主要介紹了使用RabbitMQ實現(xiàn)延時消息自動取消的簡單案例,案例代碼包括導(dǎo)包的過程和相關(guān)配置文件,本文結(jié)合代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2024-03-03
Spring基于Aop實現(xiàn)事務(wù)管理流程詳細(xì)講解
這篇文章主要介紹了Spring基于Aop實現(xiàn)事務(wù)管理流程,事務(wù)管理對于企業(yè)應(yīng)用來說是至關(guān)重要的,即使出現(xiàn)異常情況,它也可以保證數(shù)據(jù)的一致性,感興趣想要詳細(xì)了解可以參考下文2023-05-05

