Java IO流

一.字节输出流-FileOutputStream

try {
    FileOutputStream fileOutputStream = new FileOutputStream("test.txt");
    fileOutputStream.write(97);//向文件输入一个字节
    fileOutputStream.close();
} catch (IOException e) {
    e.printStackTrace();
}

当执行上述代码时表示创建test.txt文件并向其中写入内容,由于使用字节输出流,因此此处的十进制97将转换为二进制1100001输入文件内,则文件所占大小为1字节,且由于记事本打开文件时会查询编码表,将字节转化为字符,若值大小在0-127则查询Ascii表,其余将查询系统默认编码表,如中文系统则为GBK,因此此时打开记事本文件显示的字符为a。

FileOutputStream fileOutputStream = new FileOutputStream("test.txt");
//byte[] bytes = {65,66,67,68}; 输出'ABCD'
byte[] bytes = {-65,66,67,68};  //输出'緽CD'
fileOutputStream.write(bytes);
fileOutputStream.close();

若想一次输出多个字节则可以采用上述代码形式,且若第一个字节范围为0~127则会查询ASCII表,而若第一个字节为负数则将与第二个字节组成一个中文显示,查询系统默认表(GBK)。除了上述使用形式外还有以下两种使用形式。

fileOutputStream.write(bytes,1,2);//输入BC
String sentence = "I love eating";
fileOutputStream.write(sentence.getBytes());//输入字符串

二.字节输入流-FileInputStream

FileInputStream fileInputStream = new FileInputStream("test.txt");
int len = fileInputStream.read();
System.out.println(len);//输出97
fileInputStream.close();

test.txt文件内容为abc,若采用 fileInputStream.read() 则读入一个字节,而a的ASCII码为97,因此读入后转换为十进制则输出97,当读入到结尾时将输出-1,则输出全部内容可使用如下代码

FileInputStream fileInputStream = new FileInputStream("test.txt");
int len;
while((len = fileInputStream.read())!=-1){
    System.out.print((char)len);
}
fileInputStream.close();

除了上述读入方式还可以使用byte[]数组作为缓冲区,从文件内读入byte小于等于数组大小的字节数,且可以使用 String(byte[]) 构造方法将内容输出。通常byte数组长度取1024(1kb)或1024的整数倍。

FileInputStream fileInputStream = new FileInputStream("test.txt");
int len;
byte[] bytes = new byte[5];//5个字节大小的缓冲区,则能够读入5个字节数据
len = fileInputStream.read(bytes);//len表示读入的字节数
System.out.println(Arrays.toString(bytes));
System.out.println(new String(bytes));//文件内容为abcdefg,输出abcde
fileInputStream.close();

若文件已经读到结尾继续读入,即在len<byte.length的情况下继续读入,则由于byte数组没有被完全覆盖,因此输出的值将残留上次输出的内容,如上述示例中若继续读入将输出fgcde,同样的,可以利用循环读取数据

FileInputStream fileInputStream = new FileInputStream("test.txt");
int len;
byte[] bytes = new byte[1024];
while((len = fileInputStream.read(bytes))!=-1){
    System.out.print(new String(bytes,0,len));//字节数组转字符串
    //offset:0,length:len,从而防止输出无效空格
}
fileInputStream.close();

三.利用字节输入/输出流实现文件复制

File fileIn = new File("in.png");
File fileOut = new File("out.png");
FileInputStream fileInputStream = new FileInputStream(fileIn);
FileOutputStream fileOutputStream = new FileOutputStream(fileOut);
byte[] bytes = new byte[1024];
int len;
while((len = fileInputStream.read(bytes))!=-1){
    fileOutputStream.write(bytes,0,len);
}
fileInputStream.close();
fileOutputStream.close();
System.out.println("finish");

四.字符输入流-FileReader

read() 函数一次读入一个字符,且java中的一个中文字符也为一个char类型字符,因此可以直接输出中文句子

FileReader fileReader = new FileReader("test.txt");
int len;
while((len = fileReader.read())!=-1){
    System.out.print((char)len);
}
fileReader.close();

同样可以采用char[]数组作为缓冲区加快存储

FileReader fileReader = new FileReader("test.txt");
int len;
char[] chars = new char[1024];
while((len = fileReader.read(chars))!=-1){
    System.out.print(new String(chars,0,len));//字符数组转字符串
}
fileReader.close();

五.字符输出流-FileWriter

字符输出流与字节输出流的最大区别在于字符输出流的write()为写入内存缓冲区而不是硬盘中,因此若不使用close()flush()方法则文件内不会有内容写入

FileWriter fileWriter = new FileWriter("out.txt");
fileWriter.write(97);//写入一个字符,97为a的ascii码,也可直接输入字符'a'
//fileWriter.flush();
fileWriter.close();//关闭文件前自动调用flush函数

以下为输入文件的其他常用形式

//字符数组
FileWriter fileWriter = new FileWriter("out.txt");
char[] word = {'a','b','c','d','e'};
fileWriter.write(word);
//fileWriter.write(word,2,2);  off:2,len:2
fileWriter.close();

//字符串
FileWriter fileWriter = new FileWriter("out.txt");
String sentence = "abcdefg";
fileWriter.write(sentence);
//fileWriter.write(sentence,0,5);  off:0,len:5
fileWriter.close();

六.字节缓冲输入流-BufferedInputStream

为字节输入流(FileInputStream)增加一个缓冲区,增加写入效率

缓冲流原理
FileInputStream fileInputStream = new FileInputStream("out.txt");
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
byte[] bytes = new byte[1024];
int len = 0;
while((len = bufferedInputStream.read(bytes))!= -1){
    System.out.print(new String(bytes,0,len));
}
bufferedInputStream.close();

七.字节缓冲输出流-BufferedOutputStream

FileOutputStream fileOutputStream = new FileOutputStream("out.txt");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
String sentence = "将中文写入文件内作为测试";
bufferedOutputStream.write(sentence.getBytes());
bufferedOutputStream.flush();
bufferedOutputStream.close();

八.利用字节缓冲输入/输出流实现文件复制

FileInputStream fileInputStream = new FileInputStream("in.png");
FileOutputStream fileOutputStream = new FileOutputStream("out.png");
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
byte[] bytes = new byte[1024];
int len = 0;
while((len = bufferedInputStream.read(bytes))!=-1){
    bufferedOutputStream.write(bytes,0,len);
}
bufferedInputStream.close();
bufferedOutputStream.close();

九.字符缓冲输出流-BufferedWriter

BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("out.txt"));
for(int i=0;i<5;i++){
    bufferedWriter.write("中文");
    bufferedWriter.newLine();//自带的换行符号,适应各个系统的换行
}
bufferedWriter.flush();
bufferedWriter.close();

十.字符缓冲输入流-BufferedReader

BufferedReader bufferedReader = new BufferedReader(new FileReader("test.txt"));
String line;
while((line = bufferedReader.readLine())!=null){
    //readLine()函数每次读入一行,读到结尾返回null
    System.out.print(line);
}
bufferedReader.close();

Related post

  1. php调用python脚本

    2020-07-05

  2. attention model

    2020-07-26

  3. Memory Network

    2020-07-27

  4. 基于vue的微信小程序开发

    2020-07-30

There are no comment yet.

COMMENT

Take a Coffee Break

Recommend post

  1. 常用工具指令

    2022-09-18

Category list

ABOUT

Welcome to FullStar, a captivating online destination where the realms of software development and personal reflections intertwine.

April 2025
M T W T F S S
 123456
78910111213
14151617181920
21222324252627
282930  

Life Logs

  1. 回首

    2023-07-14

Return Top