IO 字节流和字符流

IO分为字节流和字符流

字节流:是8位通用字节流,其单位是字节。(FileInputStream,FileOutputStream)

字符流:是16位unicode字符流,基本单位是unicode字符,最适合处理文字或字符串。(BufferedReader,BufferedWriter)


// --字节流
FileOutputStream out = null;
try {
out = new FileOutputStream(new File("c:\\a.txt"));
byte[] by = "accept".getBytes();
out.write(by);


} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}


FileInputStream input = null;
try {
input = new FileInputStream(new File("c:\\a.txt"));
while (input.available() != 0) {
System.out.println((char) input.read());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}


// --字符流
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("c:\\a.txt"));
String str;
while ((str = reader.readLine()) != null) {
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedWriter bwout = null;
try {
bwout = new BufferedWriter(new FileWriter("c:\\a.txt"));
String str2;
str2 = "hello world!世界,你好!";
bwout.write(str2);
bwout.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bwout.close();
} catch (IOException e) {
e.printStackTrace();
}
}

### 字节流字符流分类详解 在Java IO流中,根据处理数据的单不同,可以将流分为**字节流****字符流**。字节流主要用于处理二进制文件,而字符流则用于处理文本文件。字节流字符流各自又分为**输入流****输出流**两种类型,分别用于从数据源读取数据或将数据写入目标。 #### 字节流 字节流以字节为单进行数据的读取写入,适用于处理所有类型的二进制文件,如图片、音频、视频等。字节流的主要类包括: - **InputStream**:字节输入流的基类,提供了读取字节的基本方法。 - **FileInputStream**:用于从文件中读取字节数据。 - **BufferedInputStream**:为其他输入流提供缓冲功能,提高读取效率。 - **ObjectInputStream**:用于读取对象序列化数据。 - **OutputStream**:字节输出流的基类,提供了写入字节的基本方法。 - **FileOutputStream**:用于将字节数据写入文件。 - **BufferedOutputStream**:为其他输出流提供缓冲功能,提高写入效率。 - **ObjectOutputStream**:用于写入对象序列化数据。 #### 字符流 字符流以字符为单进行数据的读取写入,适用于处理文本文件,能够处理字符编码问题,确保字符的正确性。字符流的主要类包括: - **Reader**:字符输入流的基类,提供了读取字符的基本方法。 - **FileReader**:用于从文件中读取字符数据。 - **BufferedReader**:为其他输入流提供缓冲功能,提高读取效率,并支持按行读取。 - **InputStreamReader**:将字节流转换为字符流,支持指定字符编码。 - **Writer**:字符输出流的基类,提供了写入字符的基本方法。 - **FileWriter**:用于将字符数据写入文件。 - **BufferedWriter**:为其他输出流提供缓冲功能,提高写入效率,并支持按行写入。 - **OutputStreamWriter**:将字节流转换为字符流,支持指定字符编码。 - **PrintWriter**:提供格式化输出功能,支持打印各种类型的数据。 #### 字节流字符流的区别 字节流字符流的主要区别在于处理数据的方式适用场景: - **数据单不同**:字节流以字节(8)为单进行数据的读写,适用于处理二进制文件;而字符流以字符(通常是16Unicode字符)为单进行数据的读写,适用于处理文本文件。 - **编码处理**:字符流在读写过程中会自动处理字符编码转换,确保字符的正确性;而字节流不会处理编码问题,直接操作字节数据。 - **性能差异**:由于字符流内部使用了缓冲区,并且可以按行读写,因此在处理文本文件时,字符流通常比字节流更高效。而字节流在处理大文件时可能需要手动添加缓冲机制来提高性能。 #### 示例代码 以下是一个使用字节流复制文件的示例代码,展示了如何使用`FileInputStream``FileOutputStream`进行文件复制: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileCopyExample { public static void main(String[] args) { try (FileInputStream fis = new FileInputStream("source.txt"); FileOutputStream fos = new FileOutputStream("destination.txt")) { int by; while ((by = fis.read()) != -1) { fos.write(by); } } catch (IOException e) { e.printStackTrace(); } } } ``` 该示例中,`FileInputStream`用于从源文件读取字节数据,`FileOutputStream`用于将字节数据写入目标文件。通过`try-with-resources`语句确保流在使用完毕后自动关闭,避免资源泄漏。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值