java filereader()参数_java FileReader/Writer

字符流的引入

读取:

也就是说字节流的传输都是以字节的为单位的,其本身是不具备任何编码解码能力

while((content = fileInputStream.read())!=-1){

System.out.print((char)content);//该方法是通过强转字节码才能显示出英文字符

}

while((length = fileInputStream.read(buf))!=-1){ //最起码要读到两个字节才能转换成一个中文。

System.out.print(new String(buf,0,length));//该方法是通过String的构造方法解码才能显示出中英文字符,并且中文字符必须2个字节以上才有效

}

Constructs a new String by decoding the specified subarray of bytes using the platform's default charset

写入:

当我们把字节/字节数组写入到文件后,打开文件的软件也会做一个编码解码的工作,这样我们就能看到正确的文本

所以:

在非文本的文件处理上,字节流当然是不二之选,因为文件的最小单位就是字节,字节流可以操作一切文件;

但是如果要对文本进行处理,字节流显得不是那么得心应手,于是乎---字符流出来了

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Arrays;

/*

使用字节去读写中文数据。

字符流: 读取 的数据是以字符为单位的, 会把读取到字节数据转换成我们看得懂的字符。 字符流 = 字节流 + 编码(解码)

*/

public class Demo1 {

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

// writeTest();

readTest();

}

//读取中文

public static void readTest() throws IOException{

File file = new File("F:\\a.txt");

FileInputStream fileInputStream = new FileInputStream(file);

byte[] buf = new byte[1024];

int length = 0 ;

while((length = fileInputStream.read(buf))!=-1){ //最起码要读到两个字节才能转换成一个中文。

System.out.print(new String(buf,0,length));

}

//关闭资源

fileInputStream.close();

}

//使用字节流写中文 字节流是不具备编码或者解码的功能。

public static void writeTest() throws IOException{

File file = new File("F:\\a.txt");

// 建立数据的输出通道

FileOutputStream fileOutputStream = new FileOutputStream(file);

//准备数据,把数据写出。

String data = "中国";

byte[] buf = data.getBytes(); // [-42, -48, -71, -6] 注意:字节流之所以可以写中文 ,是因为它借用了String类的getBytes方法,把字符串转成了数字。

System.out.println("字节数组:" +Arrays.toString(buf));

fileOutputStream.write(buf);

//关闭资源

fileOutputStream.close();

}

}

字符流

输入字符流:

----------| Reader 抽象类 所有输入字符流的基类。

----------------| FileReader 读取文件数据的输入字符流。

FileReader的 使用步骤:

1.找到目标文件。

2.建立传输管道

3.建立缓冲区

4.传输数据

5.关闭资源

注意:FileReader读取的字符可以是英文可以是中文,而字节流只能是英文.....

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

public class Demo2 {

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

// readTest();

readTest2();

}

//方式二:使用缓冲字符数组读取文件的数据

public static void readTest2() throws IOException{

//找到目标文件

File file = new File("F:\\Demo1.java");

//建立数据的输入通道

FileReader fileReader = new FileReader(file);

//建立缓冲字符数组,读取文件的数据

char[] buf = new char[1024];

int length = 0;

while((length = fileReader.read(buf))!=-1){ // read(char[] buf) 读取到的字符数组存储到了字符数组中,返回了本次读取到的字符个数。

System.out.print(new String(buf,0,length));

}

//关闭资源

fileReader.close();

}

//方式一:每次只会读取一个字符的数据

public static void readTest() throws IOException{

//找到目标对象

File file = new File("f:\\a.txt");

//建立数据的输入通道

FileReader fileReader = new FileReader(file);

//读取文件数据

int content= 0;

while((content=fileReader.read())!=-1){ // FileReader的read()方法每次读取一个字符的数据,如果读到 了文件末尾返回-1表示。

System.out.print((char)content);

}

//关闭资源

fileReader.close();

}

}

输出字符流

--------| Writer 抽象类 输出字符流的基类。

------------| FileWriter 向文件写出数据输出字符流.

FileWriter 使用步骤: FileReader

找到目标文件

建立数据的输出通道

写出数据

关闭资源

FileWriter 要注意的事项:

new FileWriter(file)的时候 , 如果目标文件不存在,那么会创建目标文件对象, 如果目标文件已经存在了,那么则不再重新创建。

使用new FileWriter(file) 这个构造方法的时候,默认是会先清空文本的数据,然后再写入新的数据。如果需要追加数据则需要使用 new FileWriter(file,true)这个构造方法。

使用FileWriter的write方法的时候,数据是写入了FileWriter内部维护的字符数组中,如果需要把数据真正的写到硬盘上去,需要调用flush方法或者 是close方法

或者是内部维护的字符数组已经满了,这时候也会写到硬盘上。

public class Demo1 {

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

writerTest();

}

public static void writerTest() throws IOException{

//找到目标文件

File file = new File("F:\\a.txt");

//建立数据 的输出通道

FileWriter fileWriter = new FileWriter(file); //第二个参数为true的意义是指追加数据。

//准备数据,把数据写出

String data = "现在雨停了";

fileWriter.write(data);

//刷新输出字符流

fileWriter.flush();

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值