输入/输出(三)

Reader类的方法
close() 关闭该流并释放与之关联的所有资源。
mark(int readAheadLimit) 标记流中的当前位置。1
markSupported() 判断此流是否支持 mark() 操作。
read() 读取单个字符。
read(char[] cbuf) 将字符读入数组。
read(char[] cbuf, int off, int len) 将字符读入数组的某一部分。
read(CharBuffer target) 试图将字符读入指定的字符缓冲区。
ready() 判断是否准备读取此流。
reset() 重置该流。
skip(long n) 跳过字符。
InputStream和Reader的方法基本一致,只是InputStream读取的是字节,使用的参数是byte数组 (byte[]),而Reader读取的是字符,使用的参数是char数组(char[]),他俩都是抽象类,本身不能创建实例,但它们分别有一个用于读取文件的输入流: FileInputStream和 FileReader,它们都是节点流需要直接和指定文件关联。
OutputStream和Write是所有输出流的基类,它们的方法也是非常类似的,它们的方法是所有输出流都 可使用的方法。
OutputStream类的方法
public static void main(String[] args) {
String strContent=“二、OutputStream和Writer \n OutputStream和Write是所有输出流的基类,它们的方法也是非常类似的,它们的方法是所有输出流都可使用的方法。”;
File file1=new File(“G:/java/L/OutputStreamDemo.txt”);
OutputStream out=null;
try {
out=new FileOutputStream(file1);
//String 转 byte数组
byte[] bs=(strContent.getBytes(Charset.forName(“UTF-8”)));
//通过流把byte数组写到文件
out.write(bs);
//刷新此输出流并强制写出所有缓冲的输出字节。
out.flush();
} catch (IOException e) {

}finally {
	if(out!=null) {
		try {
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

}
close() 关闭此输出流并释放与此流有关的所有系统资源。
flush() 刷新此输出流并强制写出所有缓冲的输出字节。
write(byte[] b) 将 b.length 个字节从指定的 byte 数组写入此输出 流。
write(byte[] b, int off, int len) 将指定 byte 数组中从偏移量 off 开始的 len 个字节 写入此输出流。
write(int b) 将指定的字节写入此输出流。
Writer类的方法:
public static void main(String[] args) {
String strWriter=“二、OutputStream和Writer \n OutputStream和Write是所有输出流的基类,它们的方法也是非常类似的,它们的方法是所有输出流都可使用的方法。”;
File file=new File(“G:/java/L/WriterDemo.txt”);
Writer out=null;
try {
out=new FileWriter(file);
//写入字符数组。
out.write(strWriter);
//刷新该流的缓冲。
out.flush();
} catch (IOException e) {
}finally {
if(out!=null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
append(char c) 将指定字符添加到此 writer。
append(CharSequence csq) 将指定字符序列添加到此 writer。
append(CharSequence csq, int start, int end) 将指定字符序列的子序列添加到此 writer.Appendable。
close() 关闭此流,但要先刷新它。
flush() 刷新该流的缓冲。 void write(char[] cbuf) 写入字符数组。
write(char[] cbuf, int off, int len) 写入字符数组的某一部分。
write(int c) 写入单个字符。 void write(String str) 写入字符串。
write(String str, int off, int len) 写入字符串的某一部分。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值