java reader类 实例_java字符流-java writer-java reader-嗨客网

Java字符流教程

Java 语言对文件进行操作的时候是以流的方式进行操作的,主要有如下步骤:

使用 File 类打开一个文件

通过

进行读/写操作

关闭输入/输出

Java语言字符流

和字节不同点在于,字符是 char 格式,一个字符等于两个字节。Java 语言中主要提供流 Reader 和 Writer 两个类专门操作字符流。

Java Writer类详解

说明

Writer 是一个抽象类,它是一个字符输出类。Writer 类常用方法:

常用方法

方法

名称

abstract public void close() throws IOException;

关闭输出流

public void write(String str) throws IOException

将字符串输出

public void write(char cbuf[]) throws IOException

将字符串数组输出

abstract public void flush() throws IOException;

强制性清空缓存

案例

package com.haicoder.net.stream;

import java.io.*;

public class WriterTest{

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

System.out.println("嗨客网(www.haicoder.net)");

File file = new File("/Users/haicoder/Documents/code/hai/filetest.txt"); //如果文件不存在,会自动创建

Writer writer = new FileWriter(file);

String appendInfo = "嗨客网,你好啊!\r\n"; //\r\n 在追加文件的时候,表示换行

writer.write(appendInfo); //文件没有,创建并写入

writer.write(appendInfo); //追加文件内容

writer.close();

System.out.println("===结束==");

}

}

运行结果如下

837ecd5ca47084aea50b2b1d286a48f8.png

创建文件效果如下

ceb0599d83d4644e92fffee6d113a930.png

在对文件操作的时候,我们需要捕获异常,为了方便,我们在方法体外面 throws Exception。我们使用了Writer 类的子类 FileWriter 来创建对象。 Writer 可以将字符串直接写入到文件中,也可以将信息追加到文件里面。

Java Reader类

说明

Reader 类也是一个抽象类,它与 Writer 相反,是一个字符输入类,Reader 类常用方法:

常用方法

方法

描述

abstract public void close() throws IOException;

关闭输出流

public int read() throws IOException

读取单个字符

public int read(char cbuf[]) throws IOException

将内容读取到字符数组中,返回读入的长度

案例

package com.haicoder.net.stream;

import java.io.File;

import java.io.FileReader;

import java.io.Reader;

public class ReaderTest{

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

System.out.println("嗨客网(www.haicoder.net)");

File file = new File("/Users/haicoder/Documents/code/hai/filetest.txt"); //如果文件不存在,会自动创建

System.out.println("====第一种读取方式========");

Reader reader = new FileReader(file);

char c[] = new char[1024];

int len = reader.read(c);

reader.close();

System.out.println("内容为:\n" + new String(c, 0, len));

System.out.println("=========第二种读取方式========");

Reader reader1 = new FileReader(file);

int contentLen = 0;

char contentChar[] = new char[1024];

int tmp = 0;

while ((tmp = reader1.read()) != -1) { //如果为 -1 表示读取到文件内容末尾

contentChar[contentLen] = (char) tmp;

contentLen++;

}

reader1.close();

System.out.println("第二种读取内容为:\n" + new String(contentChar, 0, contentLen));

}

}

运行结果如下:

b3930fa26b19e21f75134c0915684495.png

在对文件操作的时候,我们需要捕获异常,为了方便,我们在方法体外面 throws Exception。我们使用了Reader 类的子类 FileReader 来创建对象。

可以通过一次性读取文件的格式,将所有的内容填写到字节数组中,也可以 read() 方法,一个一个字节读取,如果读取到的数据为 -1 表示已经读取到文件末尾。

Java字符流总结

字符流里面,输出流是 Writer ,它可以将字节信息填写到文件里面,Reader 是输入流,它可以将文件中的内容读取。Writer 和 Reader 是抽象类,如果要使用它们的方法或者定义它们的对象,需要使用它们的子类。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值