柚子快報(bào)激活碼778899分享:開發(fā)語言 Java操作文件
柚子快報(bào)激活碼778899分享:開發(fā)語言 Java操作文件
本文將從兩個(gè)方面來闡述Java中的文件操作,可以使用java.nio.file包中的類來處理文件的元信息和路徑操作以及用InputStream等類來進(jìn)行文件讀寫操作。
文件的元信息和路徑的處理
獲取文件的元信息
以下是具體示范:
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.io.IOException;
public class FileInfoExample {
public static void main(String[] args) {
Path path = Paths.get("example.txt");
try {
BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
System.out.println("File creation time: " + attr.creationTime());
System.out.println("File last modified time: " + attr.lastModifiedTime());
System.out.println("Is directory? " + attr.isDirectory());
System.out.println("Is regular file? " + attr.isRegularFile());
System.out.println("File size: " + attr.size());
} catch (IOException e) {
System.err.println("Unable to read file attributes: " + e.getMessage());
}
}
}
路徑操作:
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathExample {
public static void main(String[] args) {
Path path = Paths.get("C:/Users/Username/Documents/example.txt");
// 獲取文件名
System.out.println("File name: " + path.getFileName());
// 獲取父目錄
System.out.println("Parent directory: " + path.getParent());
// 獲取絕對路徑
System.out.println("Absolute path: " + path.toAbsolutePath());
// 轉(zhuǎn)換為字符串
String pathString = path.toString();
System.out.println("Path as string: " + pathString);
// 檢查路徑是否是絕對路徑
System.out.println("Is absolute path? " + path.isAbsolute());
}
}
?文件讀寫
在Java中,流(Stream)是一種用于在程序之間傳輸數(shù)據(jù)的抽象概念。流可以是輸入流(Input Stream)用于讀取數(shù)據(jù),也可以是輸出流(Output Stream)用于寫入數(shù)據(jù)。流可以從不同的數(shù)據(jù)源(如文件、網(wǎng)絡(luò)連接、內(nèi)存等)讀取數(shù)據(jù),也可以將數(shù)據(jù)寫入到不同的目的地。
Java中的流可以分為兩種主要類型:字節(jié)流和字符流。
字節(jié)流(Byte Streams):
字節(jié)流以字節(jié)為單位進(jìn)行讀寫操作,適用于處理二進(jìn)制數(shù)據(jù)或無格式的文本數(shù)據(jù)。主要的字節(jié)流類有:
InputStream:所有字節(jié)輸入流的基類。OutputStream:所有字節(jié)輸出流的基類。FileInputStream:用于從文件中讀取數(shù)據(jù)的輸入流。FileOutputStream:用于向文件中寫入數(shù)據(jù)的輸出流。ByteArrayInputStream:從字節(jié)數(shù)組中讀取數(shù)據(jù)的輸入流。ByteArrayOutputStream:向字節(jié)數(shù)組中寫入數(shù)據(jù)的輸出流。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ByteStreamExample {
public static void main(String[] args) {
try {
// 寫入文件
FileOutputStream outputStream = new FileOutputStream("example.txt");
String data = "Hello, World!";
byte[] bytes = data.getBytes();
outputStream.write(bytes);
outputStream.close();
System.out.println("Data written to file successfully.");
// 讀取文件
FileInputStream inputStream = new FileInputStream("example.txt");
int byteData;
while ((byteData = inputStream.read()) != -1) {
System.out.print((char) byteData);
}
inputStream.close();
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
字符流(Character Streams):
字符流以字符為單位進(jìn)行讀寫操作,適用于處理文本數(shù)據(jù)。字符流會自動處理字符編碼和解碼,因此更適合處理文本數(shù)據(jù),特別是涉及到國際化的情況。主要的字符流類有:
Reader:所有字符輸入流的基類。Writer:所有字符輸出流的基類。FileReader:用于從文件中讀取字符數(shù)據(jù)的輸入流。FileWriter:用于向文件中寫入字符數(shù)據(jù)的輸出流。BufferedReader:提供緩沖功能,用于高效讀取字符數(shù)據(jù)。BufferedWriter:提供緩沖功能,用于高效寫入字符數(shù)據(jù)。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CharacterStreamExample {
public static void main(String[] args) {
try {
// 寫入文件
BufferedWriter writer = new BufferedWriter(new FileWriter("example.txt"));
writer.write("Hello, World!");
writer.close();
System.out.println("Data written to file successfully.");
// 讀取文件
BufferedReader reader = new BufferedReader(new FileReader("example.txt"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
區(qū)別:
處理數(shù)據(jù)單位:字節(jié)流以字節(jié)為單位進(jìn)行讀寫操作,而字符流以字符為單位進(jìn)行讀寫操作。適用數(shù)據(jù)類型:字節(jié)流適用于處理二進(jìn)制數(shù)據(jù)或無格式的文本數(shù)據(jù),而字符流適用于處理文本數(shù)據(jù)。處理文本編碼:字符流會自動處理字符編碼和解碼,而字節(jié)流不會,需要手動進(jìn)行處理。
在選擇流類型時(shí),需要根據(jù)所處理的數(shù)據(jù)類型以及需求來決定使用字節(jié)流還是字符流。通常情況下,如果處理的是文本數(shù)據(jù),推薦使用字符流;如果處理的是二進(jìn)制數(shù)據(jù)或無格式的文本數(shù)據(jù),可以使用字節(jié)流。
柚子快報(bào)激活碼778899分享:開發(fā)語言 Java操作文件
文章鏈接
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。