在Java中,文件与输入输出(I/O)操作是编程中经常需要处理的任务之一。Java提供了一套丰富的类和方法来支持文件的读取、写入以及标准输入输出的操作。下面是一些关于Java文件与输入输出操作的基本概念和示例。
文件操作
文件读取
Java中可以使用java.io.File类来操作文件,结合java.io.FileInputStream、java.io.FileReader、java.nio.file.Files等类来读取文件内容。
示例:
java复制代码
|
import java.io.File; |
|
import java.io.FileReader; |
|
import java.io.BufferedReader; |
|
import java.io.IOException; |
|
|
|
public class FileReadingExample { |
|
public static void main(String[] args) { |
|
File file = new File("example.txt"); |
|
FileReader fr = null; |
|
BufferedReader br = null; |
|
|
|
try { |
|
fr = new FileReader(file); |
|
br = new BufferedReader(fr); |
|
String line; |
|
while ((line = br.readLine()) != null) { |
|
System.out.println(line); |
|
} |
|
} catch (IOException e) { |
|
e.printStackTrace(); |
|
} finally { |
|
try { |
|
if (br != null) br.close(); |
|
if (fr != null) fr.close(); |
|
} catch (IOException ex) { |
|
ex.printStackTrace(); |
|
} |
|
} |
|
} |
|
} |
文件写入
可以使用java.io.FileOutputStream、java.io.FileWriter等类来写入文件内容。
示例:
java复制代码
|
import java.io.File; |
|
import java.io.FileWriter; |
|
import java.io.IOException; |
|
|
|
public class FileWritingExample { |
|
public static void main(String[] args) { |
|
File file = new File("output.txt"); |
|
FileWriter fw = null; |
|
|
|
try { |
|
fw = new FileWriter(file); |
|
fw.write("Hello, World!"); |
|
} catch (IOException e) { |
|
e.printStackTrace(); |
|
} finally { |
|
try { |
|
if (fw != null) fw.close(); |
|
} catch (IOException ex) { |
|
ex.printStackTrace(); |
|
} |
|
} |
|
} |
|
} |
标准输入输出
Java使用System.in作为标准输入流,System.out和System.err作为标准输出流和错误输出流。通常与java.io.BufferedReader、java.io.PrintStream或java.io.PrintWriter结合使用。
示例:从控制台读取输入:
java复制代码
|
import java.io.BufferedReader; |
|
import java.io.InputStreamReader; |
|
import java.io.IOException; |
|
|
|
public class ConsoleInputExample { |
|
public static void main(String[] args) { |
|
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
|
System.out.print("Enter your name: "); |
|
String name = null; |
|
try { |
|
name = br.readLine(); |
|
System.out.println("Hello, " + name + "!"); |
|
} catch (IOException e) { |
|
e.printStackTrace(); |
|
} |
|
} |
|
} |
使用NIO(New I/O)
从Java NIO(New I/O)开始,Java引入了新的I/O API,提供了更高效的I/O处理方式,如通道(Channel)、缓冲区(Buffer)等。这些API通常用于处理大量数据或需要高性能I/O的场景。
示例:使用NIO读取文件:
java复制代码
|
import java.io.IOException; |
|
import java.nio.ByteBuffer; |
|
import java.nio.channels.FileChannel; |
|
import java.nio.file.Paths; |
|
import java.nio.file.StandardOpenOption; |
|
import java.nio.charset.StandardCharsets; |
|
|
|
public class NIOFileReadingExample { |
|
public static void main(String[] args) { |
|
try (FileChannel fileChannel = new FileInputStream(Paths.get("example.txt").toFile()) |
|
.getChannel()) { |
|
ByteBuffer buffer = ByteBuffer.allocate(1024); |
|
while (fileChannel.read(buffer) > 0) { |
|
buffer.flip(); // 准备从缓冲区读取数据 |
|
while (buffer.hasRemaining()) { |
|
System.out.print((char) buffer.get()); |
|
} |
|
buffer.clear(); // 清除缓冲区,准备下一次读取 |
|
} |
|
} catch (IOException |