Java中的I/O操作可以分为两种类型:字节流和字符流。字节流用于处理二进制数据,而字符流用于处理文本数据。本文将详细介绍Java中的字节流和字符流,并给出相应的示例代码。
Java中的字节流主要用于处理二进制数据,如图像、音频和视频等。Java中的字节流主要分为InputStream和OutputStream两种类型。
(资料图)
InputStream是Java中所有输入流的基类。InputStream中定义了许多方法用于读取字节。以下是InputStream中一些常用的方法:
int read(): 从输入流中读取一个字节的数据。如果已到达文件结尾,则返回-1。int read(byte[] b): 从输入流中读取最多b.length个字节的数据,并将其存储到缓冲区b中。返回实际读取的字节数。int available(): 返回可以从输入流中读取的字节数。下面是一个从文件中读取数据并将其显示在控制台上的示例:
import java.io.*;public class InputStreamExample { public static void main(String[] args) throws IOException { InputStream in = new FileInputStream("test.txt"); int b; while ((b = in.read()) != -1) { System.out.print((char) b); } in.close(); }}
在上述示例中,我们使用FileInputStream打开一个文件,并使用read()方法读取文件中的每一个字节,并将其显示在控制台上。
OutputStream是Java中所有输出流的基类。OutputStream中定义了许多方法用于写入字节。以下是OutputStream中一些常用的方法:
void write(int b): 将指定的字节写入输出流中。void write(byte[] b): 将指定的字节数组中的数据写入输出流中。void flush(): 刷新输出流并强制写入所有缓冲的输出字节。下面是一个将数据写入文件的示例:
javaCopy codeimport java.io.*;public class OutputStreamExample { public static void main(String[] args) throws IOException { OutputStream out = new FileOutputStream("test.txt"); String str = "Hello, world!"; byte[] b = str.getBytes(); out.write(b); out.close(); }}
在上述示例中,我们使用FileOutputStream打开一个文件,并使用write()方法将一个字符串转换为字节数组,然后将其写入文件中。
Java中的字符流主要用于处理文本数据。字符流可以直接处理Unicode字符,因此在处理中文等特殊字符时很方便。Java中的字符流主要分为Reader和Writer两种类型。
Reader是Java中所有读取字符流的基类。Reader中定义了许多方法用于读取字符。以下是Reader中一些常用的方法:
int read(): 从输入流中读取一个字符的数据。如果已到达文件结尾,则返回-1。int read(char[] c): 从输入流中读取最多c.length个字符的数据,并将其存储到缓冲区c中。返回实际读取的字符数。下面是一个从文件中读取数据并将其显示在控制台上的示例:
import java.io.*;public class ReaderExample { public static void main(String[] args) throws IOException { Reader reader = new FileReader("test.txt"); int c; while ((c = reader.read()) != -1) { System.out.print((char) c); } reader.close(); }}
在上述示例中,我们使用FileReader打开一个文件,并使用read()方法读取文件中的每一个字符,并将其显示在控制台上。
Writer是Java中所有写入字符流的基类。Writer中定义了许多方法用于写入字符。以下是Writer中一些常用的方法:
void write(int c): 将指定的字符写入输出流中。void write(char[] c): 将指定的字符数组中的数据写入输出流中。void flush(): 刷新输出流并强制写入所有缓冲的输出字符。下面是一个将数据写入文件的示例:
import java.io.*;public class WriterExample { public static void main(String[] args) throws IOException { Writer writer = new FileWriter("test.txt"); String str = "Hello, world!"; writer.write(str); writer.close(); }}
在上述示例中,我们使用FileWriter打开一个文件,并使用write()方法将一个字符串写入文件中。
在Java中,字节流和字符流有以下区别:
字节流主要用于处理二进制数据,如图像、音频和视频等。字符流主要用于处理文本数据。
字节流以字节为单位进行读取和写入,而字符流以字符为单位进行读取和写入。
字节流不关心数据的编码方式,可以处理任何类型的数据。而字符流必须使用特定的字符编码方式,如UTF-8或GBK,以正确地处理文本数据。
X 关闭
2023-09-09 14:19:31
2023-09-09 11:59:09
2023-09-09 10:36:21
2023-09-09 08:48:56
2023-09-09 07:04:59
2023-09-09 02:44:09
2023-09-08 21:57:33
2023-09-08 18:55:44
2023-09-08 17:34:41
2023-09-08 16:41:31
2023-09-08 15:23:20
2023-09-08 14:04:10
2023-09-08 12:58:36
2023-09-08 11:51:20
2023-09-08 10:44:21
2023-09-08 10:13:52
2023-09-01 10:45:46
2023-09-01 09:52:45
2023-09-01 08:09:14
2023-09-01 06:05:16
2023-09-01 02:12:58
2023-08-31 22:05:08
2023-08-31 20:33:04
2023-08-31 18:56:53
2023-08-31 17:48:23
2023-08-31 16:56:45
2023-08-31 15:57:59
2023-08-31 14:13:27
2023-08-31 12:54:30
2023-08-31 11:53:58
2023-08-31 10:13:11
2023-08-31 09:49:17
2023-08-31 08:31:53
2023-08-31 07:08:37
2023-08-31 04:39:57
2023-08-30 23:04:51
2023-08-30 21:23:00
2023-08-30 18:53:33
2023-08-30 17:35:13
2023-08-30 16:18:50
2023-08-30 15:23:29
2023-08-30 14:12:59
2023-08-30 12:50:49
2023-08-30 11:19:00
2023-08-30 09:54:25
2023-08-30 09:01:57
2023-08-30 07:56:49
2023-08-30 06:17:07
2023-08-30 02:10:55
2023-08-29 20:59:27
2023-08-29 19:03:45
2023-08-29 17:59:31
2023-08-29 15:06:46
2023-08-29 13:36:59
2023-08-29 11:42:29
2023-08-29 09:41:58
2023-08-29 08:48:30
2023-08-29 07:19:28
2023-08-29 04:55:20
2023-08-28 23:00:14
2023-08-28 19:46:14
2023-08-28 18:02:53
2023-08-28 16:52:39
2023-08-28 15:19:53
2023-08-28 12:42:48
2023-08-28 11:02:11
2023-08-28 08:54:27
2023-08-28 07:08:40
2023-08-28 00:48:32
2023-08-27 21:02:36
2023-08-27 19:06:51
2023-08-27 16:58:02
2023-08-27 14:51:35
2023-08-27 11:31:48
2023-08-27 09:48:18
2023-08-27 07:59:18
2023-08-27 03:42:53
2023-08-26 21:52:51
2023-08-26 19:31:13
2023-08-26 17:10:28
2023-08-26 15:21:10
2023-08-26 13:14:01
2023-08-26 11:07:50
2023-08-26 08:52:21
2023-08-26 06:48:44
2023-08-26 03:21:21
2023-08-25 22:42:52
2023-08-25 20:47:17
2023-08-25 19:12:50
2023-08-25 17:51:04
2023-08-25 16:16:54
2023-08-25 14:50:01
2023-08-25 13:16:08
2023-08-25 11:44:06
2023-08-25 10:15:45
2023-08-25 09:12:59
2023-08-25 07:58:31
2023-08-25 05:55:29
2023-08-25 01:00:37
2023-08-24 21:53:45
Copyright © 2015-2022 华尔街化工网版权所有 备案号:沪ICP备2022005074号-44 联系邮箱:58 55 97 3@qq.com