Java之IO流
Java中的IO流基础概念
在Java中,IO流(Input/Output Stream)用于处理输入和输出操作。IO流可以分为两大类:
字节流(Byte Streams):以字节为单位进行数据的读写,主要用于处理二进制数据。
InputStream
和 OutputStream
是所有字节流的基类。
字符流(Character Streams):以字符为单位进行数据的读写,主要用于处理文本数据。
Reader
和 Writer
是所有字符流的基类。
如何定义和使用IO流
字节流:
InputStream
和 OutputStream
是抽象类,常用的实现类有 FileInputStream
和 FileOutputStream
。
字符流:
Reader
和 Writer
是抽象类,常用的实现类有 FileReader
和 FileWriter
。
文件读取和写入的代码示例
1. 使用字节流读取文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import java.io.FileInputStream; import java.io.IOException;
public class ByteStreamReadExample { public static void main(String[] args) { try (FileInputStream fis = new FileInputStream("input.txt")) { int data; while ((data = fis.read()) != -1) { System.out.print((char) data); } } catch (IOException e) { e.printStackTrace(); } } }
|
2. 使用字节流写入文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import java.io.FileOutputStream; import java.io.IOException;
public class ByteStreamWriteExample { public static void main(String[] args) { try (FileOutputStream fos = new FileOutputStream("output.txt")) { String text = "Hello, World!"; byte[] bytes = text.getBytes(); fos.write(bytes); } catch (IOException e) { e.printStackTrace(); } } }
|
3. 使用字符流读取文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import java.io.FileReader; import java.io.IOException;
public class CharacterStreamReadExample { public static void main(String[] args) { try (FileReader fr = new FileReader("input.txt")) { int data; while ((data = fr.read()) != -1) { System.out.print((char) data); } } catch (IOException e) { e.printStackTrace(); } } }
|
4. 使用字符流写入文件
1 2 3 4 5 6 7 8 9 10 11 12 13
| import java.io.FileWriter; import java.io.IOException;
public class CharacterStreamWriteExample { public static void main(String[] args) { try (FileWriter fw = new FileWriter("output.txt")) { String text = "Hello, World!"; fw.write(text); } catch (IOException e) { e.printStackTrace(); } } }
|
使用缓冲流提高效率
为了提高IO操作的效率,Java提供了缓冲流(Buffered Streams),它们通过在内存中缓冲数据来减少实际的IO操作次数。
1. 使用缓冲字节流读取文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException;
public class BufferedByteStreamReadExample { public static void main(String[] args) { try (FileInputStream fis = new FileInputStream("input.txt"); BufferedInputStream bis = new BufferedInputStream(fis)) { int data; while ((data = bis.read()) != -1) { System.out.print((char) data); } } catch (IOException e) { e.printStackTrace(); } } }
|
2. 使用缓冲字节流写入文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException;
public class BufferedByteStreamWriteExample { public static void main(String[] args) { try (FileOutputStream fos = new FileOutputStream("output.txt"); BufferedOutputStream bos = new BufferedOutputStream(fos)) { String text = "Hello, World!"; byte[] bytes = text.getBytes(); bos.write(bytes); } catch (IOException e) { e.printStackTrace(); } } }
|
3. 使用缓冲字符流读取文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException;
public class BufferedCharacterStreamReadExample { public static void main(String[] args) { try (FileReader fr = new FileReader("input.txt"); BufferedReader br = new BufferedReader(fr)) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } }
|
4. 使用缓冲字符流写入文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException;
public class BufferedCharacterStreamWriteExample { public static void main(String[] args) { try (FileWriter fw = new FileWriter("output.txt"); BufferedWriter bw = new BufferedWriter(fw)) { String text = "Hello, World!"; bw.write(text); } catch (IOException e) { e.printStackTrace(); } } }
|
总结
- 字节流:适用于处理二进制数据,如图片、音频等。
- 字符流:适用于处理文本数据。
- 缓冲流:通过缓冲数据来提高IO操作的效率。
在实际开发中,根据具体需求选择合适的流类型,并结合缓冲流来提高性能。
蚂蚁🐜再小也是肉🥩!