什么是IO流
IO(Input/Output)流是Java中用于输入和输出操作的一种机制。Java提供了丰富的IO流类,包括字节流和字符流,用于处理文件、网络等输入输出流。
IO流的分类
Java中的IO流主要分为两大类:字节流和字符流。
- 字节流:以字节为单位进行读写操作,主要由InputStream和OutputStream两个抽象类派生出来的具体流类来实现。
- 字符流:以字符为单位进行读写操作,主要由Reader和Writer两个抽象类派生出来的具体流类来实现。
常用的IO流类
以下是Java中常用的IO流类及其用途:
- FileInputStream/FileOutputStream:用于对文件进行字节流读写操作。
- FileReader/FileWriter:用于对文件进行字符流读写操作。
- ByteArrayInputStream/ByteArrayOutputStream:用于对字节数组进行读写操作。
- CharArrayReader/CharArrayWriter:用于对字符数组进行读写操作。
- InputStreamReader/OutputStreamWriter:用于将字节流转换为字符流进行读写操作,可以指定字符集。
- BufferedReader/BufferedWriter:用于加速字符流读写操作,通过内部缓冲区提高性能。
- DataInputStream/DataOutputStream:用于以基本数据类型的形式读写二进制数据。
IO流的基本操作
1. 字节流读写文件
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("input.txt");
fos = new FileOutputStream("output.txt");
int data;
while ((data = fis.read()) != -1) {
fos.write(data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. 字符流读写文件
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader("input.txt");
fw = new FileWriter("output.txt");
int data;
while ((data = fr.read()) != -1) {
fw.write(data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. 使用BufferedReader和BufferedWriter加速字符流读写操作
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader("input.txt"));
bw = new BufferedWriter(new FileWriter("output.txt"));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
IO流的异常处理
在使用IO流进行读写操作时,需要注意异常的处理。常见的IO流异常包括IOException、FileNotFoundException等。
在处理IO流异常时,一般采用try-catch-finally语句块来捕获异常并进行相应处理。在finally块中需要确保关闭流的操作,以释放资源。
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("input.txt"));
// 具体的读取操作
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
IO流的性能优化
在进行大文件读写操作时,可以使用缓冲区来提高性能。Java中提供了BufferedReader和BufferedWriter类,可以通过在内存中创建一个缓冲区来加速读写操作。
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader("input.txt"));
bw = new BufferedWriter(new FileWriter("output.txt"));
char[] buffer = new char[1024];
int length;
while ((length = br.read(buffer)) != -1) {
bw.write(buffer, 0, length);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
总结
IO流是Java中进行输入输出操作的常用机制,学会使用IO流操作可以帮助我们更高效地处理文件、网络等输入输出流。在使用IO流时,需要注意异常的处理,以及采用合适的技巧来提高读写的性能。以上是我对Java中IO流操作的一些分享,希望能对你有所帮助。
本文来自极简博客,作者:秋天的童话,转载请注明原文链接:Java中的IO流操作技巧分享