Java中IO流常用函数及使用方法详解
Java的IO流是Java中非常重要的一部分,它为开发者提供了文件操作的便利,也能进行网络通信。Java中的IO流分为输入流和输出流,它们专门用来处理来自文件、键盘等的输入或者将输出发送给文件、屏幕等。
本文将介绍IO流中的常用函数及使用方法。
1. FileInputStream类
FileInputStream类是Java中文件输入流类,可以用来读取文件的内容。常用方法有以下几个:
a. int read():读取输入流中的一个字节,并返回该字节的ASCII码。如果文件到达末尾,则返回-1。
b. int read(byte[] b):读取一定数量的字节,并将它们存储在缓冲区b中。它会返回实际读取的字节数,如果文件到达末尾则会返回-1。
c. int read(byte[] b, int off, int len):从输入流中读取len个字节,并将这些字节存储在数组b的偏移量为off的位置。方法也会返回读取的字节数。如果文件到达末尾,则返回-1。
d. void close():关闭流。
示例代码:
import java.io.*;
public class FileInputStreamDemo {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("test.txt");
int i = 0;
while ((i = fis.read()) != -1) {
System.out.print((char) i + " ");
}
fis.close();
}
}
2. FileOutputStream类
FileOutputStream类是Java中的文件输出流类,可以用来写入文件内容。常用方法有以下几个:
a. void write(int b):将指定的字节写入输出流。
b. void write(byte[] b):将指定数组的字节写入输出流。
c. void write(byte[] b, int off, int len):将指定的字节数组从偏移量off开始写入len个字节。
d. void close():关闭流。
示例代码:
import java.io.*;
public class FileOutputStreamDemo {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("test.txt");
String str = "Hello World";
fos.write(str.getBytes());
fos.close();
}
}
3. FileReader类
FileReader类是Java中的文件读取类,可以用来读取文本文件。常用方法有以下几个:
a. int read():读取一个字符,并返回该字符的Unicode码。如果到达文件结尾,则返回-1。
b. int read(char[] cbuf):读取一定数量的字符,并将它们存储在字符数组cbuf中。返回实际读取的字符数。如果到达文件结尾,则返回-1。
c. int read(char[] cbuf, int off, int len):从输入流中读取len个字符,并将这些字符存储在字符数组cbuf的偏移量为off的位置。方法也会返回读取的字符数。如果文件到达结尾,则返回-1。
d. void close():关闭流。
示例代码:
import java.io.*;
public class FileReaderDemo {
public static void main(String[] args) throws Exception {
FileReader fr = new FileReader("test.txt");
int i = 0;
while ((i = fr.read()) != -1) {
System.out.print((char) i + " ");
}
fr.close();
}
}
4. FileWriter类
FileWriter类是Java中文件写入类,可以用来写入字符到文本文件中。常用方法有以下几个:
a. void write(int c):将指定的字符写入到输出流中。
b. void write(char[] cbuf):将指定的字符数组写入到输出流中。
c. void write(String str):将指定的字符串写入到输出流中。
d. void write(char[] cbuf, int off, int len):将指定的字符数组的内容从偏移量off开始写入len个字符。
e. void write(String str, int off, int len):将指定的字符串的内容从偏移量off开始写入len个字符。
f. void close():关闭流。
示例代码:
import java.io.*;
public class FileWriterDemo {
public static void main(String[] args) throws Exception {
FileWriter fw = new FileWriter("test.txt");
String str = "Hello World";
fw.write(str);
fw.close();
}
}
5. BufferedReader类
BufferedReader类是Java中缓存读取类,用来读取文本。常用方法有以下几个:
a. int read():读取一个字符,并返回该字符的Unicode码。如果到达文件结尾,则返回-1。
b. int read(char[] cbuf):读取一定数量的字符,并将它们存储在字符数组cbuf中。返回实际读取的字符数。如果到达文件结尾,则返回-1。
c. int read(char[] cbuf, int off, int len):从输入流中读取len个字符,并将这些字符存储在字符数组cbuf的偏移量为off的位置。方法也会返回读取的字符数。如果文件到达结尾,则返回-1。
d. String readLine():读取文件中的一行,并返回该行的内容。如果到达文件结尾,则返回null。
e. void close():关闭流。
示例代码:
import java.io.*;
public class BufferedReaderDemo {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("test.txt"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
}
6. BufferedWriter类
BufferedWriter类是Java中的缓存写入类,用来写入字符到文本文件中。常用方法有以下几个:
a. void write(int c):将指定的字符写入到输出流中。
b. void write(char[] cbuf):将指定的字符数组写入到输出流中。
c. void write(String str):将指定的字符串写入到输出流中。
d. void write(char[] cbuf, int off, int len):将指定的字符数组的内容从偏移量off开始写入len个字符。
e. void write(String str, int off, int len):将指定的字符串的内容从偏移量off开始写入len个字符。
f. void flush():将缓冲区中的内容写入到文件中。
g. void close():关闭流。
示例代码:
import java.io.*;
public class BufferedWriterDemo {
public static void main(String[] args) throws Exception {
BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt"));
bw.write("Hello World");
bw.newLine();
bw.flush();
bw.close();
}
}
以上就是Java中IO流常用函数及使用方法的详细介绍,开发者可以根据项目需要选择合适的IO流进行操作。
