Java--I/O流

I/O流

InputStream、OutputStream、FileInputStream、FileOutputStream(字节流)

字节输入流InputStream主要方法:

read() :从此输入流中读取一个数据字节。

read(byte[] b) :从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。

read(byte[] b, int off, int len) :从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。

close():关闭此输入流并释放与该流关联的所有系统资源。

字节输出流OutputStream主要方法:

write(byte[] b) :将 b.length 个字节从指定 byte 数组写入此文件输出流中。

write(byte[] b, int off, int len) :将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。

write(int b) :将指定字节写入此文件输出流。

close() :关闭此输入流并释放与该流关联的所有系统资源。

字节流的方式效率较低,不建议使用

案例

public class IOTest {

public static void main(String[] args) throws IOException {

File file = new File("D:/test.txt");

write(file);

System.out.println(read(file));

}

public static void write(File file) throws IOException {

OutputStream os = new FileOutputStream(file, true);

// 要写入的字符串

String string = "松下问童子,言师采药去。只在此山中,云深不知处。";

// 写入文件

os.write(string.getBytes());

// 关闭流

os.close();

}

public static String read(File file) throws IOException {

InputStream in = new FileInputStream(file);

// 一次性取多少个字节

byte[] bytes = new byte[1024];

// 用来接收读取的字节数组

StringBuilder sb = new StringBuilder();

// 读取到的字节数组长度,为-1时表示没有数据

int length = 0;

// 循环取数据

while ((length = in.read(bytes)) != -1) {

// 将读取的内容转换成字符串

sb.append(new String(bytes, 0, length));

}

// 关闭流

in.close();

return sb.toString();

}

}

BufferedInputStream、BufferedOutputStream(缓冲字节流)

缓冲字节流是为高效率而设计的,真正的读写操作还是靠FileOutputStream和FileInputStream,所以其构造方法入参是这两个类的对象也就不奇怪了。

案例

public class IOTest {

public static void write(File file) throws IOException {

// 缓冲字节流,提高了效率

BufferedOutputStream bis = new BufferedOutputStream(new FileOutputStream(file, true));

// 要写入的字符串

String string = "松下问童子,言师采药去。只在此山中,云深不知处。";

// 写入文件

bis.write(string.getBytes());

// 关闭流

bis.close();

}

public static String read(File file) throws IOException {

BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));

// 一次性取多少个字节

byte[] bytes = new byte[1024];

// 用来接收读取的字节数组

StringBuilder sb = new StringBuilder();

// 读取到的字节数组长度,为-1时表示没有数据

int length = 0;

// 循环取数据

while ((length = fis.read(bytes)) != -1) {

// 将读取的内容转换成字符串

sb.append(new String(bytes, 0, length));

}

// 关闭流

fis.close();

return sb.toString();

}

}

InputStreamReader、OutputStreamWriter(字符流)

字符输入流Reader主要方法:

read():读取单个字符。

read(char[] cbuf) :将字符读入数组。

read(char[] cbuf, int off, int len) : 将字符读入数组的某一部分。

read(CharBuffer target) :试图将字符读入指定的字符缓冲区。

flush() :刷新该流的缓冲。

close() :关闭此流,但要先刷新它。

字符输出流Writer主要方法:

write(char[] cbuf) :写入字符数组。

write(char[] cbuf, int off, int len) :写入字符数组的某一部分。

write(int c) :写入单个字符。

write(String str) :写入字符串。

write(String str, int off, int len) :写入字符串的某一部分。

flush() :刷新该流的缓冲。

close() :关闭此流,但要先刷新它。

另外,字符缓冲流还有两个独特的方法:

BufferedWriter类newLine() :写入一个行分隔符。这个方法会自动适配所在系统的行分隔符。

BufferedReader类readLine() :读取一个文本行。

字符流适用于文本文件的读写,OutputStreamWriter类其实也是借助FileOutputStream类实现的,故其构造方法是FileOutputStream的对象

案例

public class IOTest {

public static void write(File file) throws IOException {

// OutputStreamWriter可以显示指定字符集,否则使用默认字符集

OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8");

// 要写入的字符串

String string = "松下问童子,言师采药去。只在此山中,云深不知处。";

osw.write(string);

osw.close();

}

public static String read(File file) throws IOException {

InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");

// 字符数组:一次读取多少个字符

char[] chars = new char[1024];

// 每次读取的字符数组先append到StringBuilder中

StringBuilder sb = new StringBuilder();

// 读取到的字符数组长度,为-1时表示没有数据

int length;

// 循环取数据

while ((length = isr.read(chars)) != -1) {

// 将读取的内容转换成字符串

sb.append(chars, 0, length);

}

// 关闭流

isr.close();

return sb.toString()

}

}

//缓冲字符流

public static void write(File file) throws IOException {

// BufferedWriter fw = new BufferedWriter(new OutputStreamWriter(new

// FileOutputStream(file, true), "UTF-8"));

// FileWriter可以大幅度简化代码

BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));

// 要写入的字符串

String string = "松下问童子,言师采药去。只在此山中,云深不知处。";

bw.write(string);

bw.close();

}

public static String read(File file) throws IOException {

BufferedReader br = new BufferedReader(new FileReader(file));

// 用来接收读取的字节数组

StringBuilder sb = new StringBuilder();

// 按行读数据

String line;

// 循环取数据 readLine读取一行

while ((line = br.readLine()) != null) {

// 将读取的内容转换成字符串

sb.append(line);

}

// 关闭流

br.close();

return sb.toString();

}

  • 34
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小张同学(恩师白云)

感谢您的打赏,我们一起进步

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值