Java文件流操作是Java語言中常用的一種文件操作方式。Java中的文件流可以用於讀取和寫入文件,是對底層位元組流的封裝,可以非常方便地對文件進行操作。在本文中,我們將從多個方面對Java文件流操作進行詳細的闡述,旨在幫助讀者更好地掌握Java文件流操作的知識。
一、文件流的概念
文件流,顧名思義,就是針對文件進行操作的流。在Java中提供了多個用於讀寫文件的類,它們都繼承自位元組流(InputStream / OutputStream)。其中,InputStream和OutputStream是位元組輸入和輸出流的基類,具體的子類包括FileInputStream、FileOutputStream、ByteArrayInputStream、ByteArrayOutputStream等。這些類在進行文件操作時,數據流會被轉換為位元組流,在處理完之後再轉回數據流。
二、文件讀取流
文件讀取流用於從文件中讀取數據,常用的文件讀取流有:FileInputStream和BufferedInputStream。
1. FileInputStream
FileInputStream是一個阻塞式的文件讀取流,讀取文件時直接把文件讀入內存,適合較小的文件。如果文件過大,將會發生內存溢出。以下是FileInputStream的使用示例:
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(new File("file.txt"));
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fileInputStream.read(buffer)) != -1) {
String str = new String(buffer, 0, len, "UTF-8");
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. BufferedInputStream
BufferedInputStream是一個高效的文件讀取流,它提供了緩衝區,可以對文件進行緩存,從而提高讀取文件的效率。以下是BufferedInputStream的使用示例:
BufferedInputStream bufferedInputStream = null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(new File("file.txt")));
byte[] buffer = new byte[1024];
int len = 0;
while ((len = bufferedInputStream.read(buffer)) != -1) {
String str = new String(buffer, 0, len, "UTF-8");
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedInputStream != null) {
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
三、文件寫入流
文件寫入流用於往文件中寫入數據,常用的文件寫入流有:FileOutputStream和BufferedOutputStream。
1. FileOutputStream
FileOutputStream是一個阻塞式的文件寫入流,數據寫入文件時通過write()方法直接寫入內存,適合較小的文件。如果文件過大,將會發生內存溢出。以下是FileOutputStream的使用示例:
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(new File("newFile.txt"));
String str = "Hello World!";
byte[] bytes = str.getBytes("UTF-8");
fileOutputStream.write(bytes, 0, bytes.length);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. BufferedOutputStream
BufferedOutputStream是一個高效的文件寫入流,它提供了緩衝區,可以對文件進行緩存,從而提高寫入文件的效率。以下是BufferedOutputStream的使用示例:
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File("newFile.txt")));
String str = "Hello World! Buffered.";
byte[] bytes = str.getBytes("UTF-8");
bufferedOutputStream.write(bytes, 0, bytes.length);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedOutputStream != null) {
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
四、文件複製
文件複製是文件操作的一個常見需求,通過Java文件流操作可非常方便地實現文件複製。
public static void copyFile(File sourceFile, File targetFile) {
InputStream inputStream = null;
BufferedInputStream bufferedInputStream = null;
OutputStream outputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
inputStream = new FileInputStream(sourceFile);
bufferedInputStream = new BufferedInputStream(inputStream);
outputStream = new FileOutputStream(targetFile);
bufferedOutputStream = new BufferedOutputStream(outputStream);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = bufferedInputStream.read(buffer)) != -1) {
bufferedOutputStream.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedOutputStream != null) {
bufferedOutputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
if (bufferedInputStream != null) {
bufferedInputStream.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
五、文件刪除
文件刪除是Java文件操作中的常見需求,刪除文件也可通過文件流操作來實現。
public static boolean deleteFile(File file) {
if (file.exists() && file.isFile()) {
return file.delete();
}
return false;
}
總結
本篇文章詳細地闡述了Java文件流操作的各個方面,從概念、文件讀取流、文件寫入流、文件複製、文件刪除等多個方面進行了詳細的介紹。掌握這些知識,可以幫助我們更好地進行Java文件操作。
原創文章,作者:IBEU,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/141115.html
微信掃一掃
支付寶掃一掃