一、Java文件讀取基礎
使用Java讀取文件是Java開發中常用的功能。Java提供了多種方式用於讀取文件內容,常見的有FileInputStream、FileReader、BufferedReader等。
其中,FileInputStream和FileReader都是基於文件字節流的讀取方式。BufferedReader則是基於字符緩衝區的讀取方式,可以提高讀取效率。
以下是使用FileInputStream讀取文件內容的示例代碼:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class ReadFileExample {
public static void main(String[] args) {
File file = new File("file.txt");
try (FileInputStream fis = new FileInputStream(file)) {
byte[] buffer = new byte[(int) file.length()];
fis.read(buffer);
String content = new String(buffer);
System.out.println(content);
} catch (IOException e) {
e.printStackTrace();
}
}
}
上述代碼中,首先創建File對象,並在try-with-resources語句中創建FileInputStream對象。通過FileInputStream對象讀取文件內容,將讀取的字節存儲在byte數組中,並通過String構造器將byte數組轉換為字符串。
二、使用BufferedReader讀取文件
使用BufferedReader讀取文件內容相比FileInputStream和FileReader更加高效,因為它可以一次讀取多個字符並將它們緩存在內存中。
使用BufferedReader讀取文件內容的示例代碼如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class ReadFileExample {
public static void main(String[] args) {
File file = new File("file.txt");
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
上述代碼中,首先創建File對象。在try-with-resources語句中創建BufferedReader對象,同時創建FileReader對象以逐行讀取文件內容,並將讀取的內容存儲在line字符串變量中。
三、使用Java NIO讀取文件
Java NIO(New IO)是Java SE 1.4中引入的一組新的Input/Output(I/O)API,可以提供高速的、面向緩衝的I/O操作。
使用Java NIO讀取文件內容的示例代碼如下:
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class ReadFileExample {
public static void main(String[] args) {
String fileName = "file.txt";
try (Stream stream = Files.lines(Paths.get(fileName), StandardCharsets.UTF_8)) {
stream.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
}
上述代碼中,通過Paths.get方法獲取文件路徑。在try-with-resources語句中創建Stream對象,通過Files.lines方法讀取文件內容,將讀取的每一行直接輸出。
四、讀取二進制文件
除了常規的文本文件,Java也可以讀取二進制文件。讀取二進制文件時,需要使用字節流讀取器。
讀取二進制文件的示例代碼如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class ReadFileExample {
public static void main(String[] args) {
File file = new File("file.bin");
try (InputStream is = new FileInputStream(file)) {
byte[] buffer = new byte[(int) file.length()];
is.read(buffer);
for (byte b : buffer) {
System.out.print(b + " ");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
上述代碼中,首先創建File對象。在try-with-resources語句中創建FileInputStream對象以讀取二進制文件內容。讀取的字節存儲在byte數組中,並逐個輸出每個字節值。
五、使用Scanner讀取文件
Java還提供了Scanner類,可以用於讀取文件中的各種類型的數據,包括文本和二進制數據。
使用Scanner讀取文件內容的示例代碼如下:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFileExample {
public static void main(String[] args) {
File file = new File("file.txt");
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
上述代碼中,首先創建File對象。在try-with-resources語句中創建Scanner對象以逐行讀取文件內容,並將讀取的每一行輸出。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/151058.html