一、字节流
InputStream字节输入流 ,OutputStream字节输出流 ,用于以字节的形式读取和写入数据
所有的数据存放在计算机中都是以数字的形式存放的。 所以字母就需要转换为数字才能够存放。
比如A就对应的数字65,a对应的数字97. 不同的字母和符号对应不同的数字,就是一张码表。
ASCII是这样的一种码表。 只包含简单的英文字母,符号,数字等等。
参考代码
package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Date;
import javax.imageio.stream.FileImageInputStream;
public class TestFile {
public static void main(String[] args) throws IOException {
File f = new File("d:/Test1/1.txt");
File f1 = new File("d:/Test1/2.txt");
//创建父文件夹
f.getParentFile().mkdirs();
//创建1.txt,2.txt文件
f.createNewFile();
f1.createNewFile();
// 把流定义在try()里,try,catch或者finally结束的时候,会自动关闭
// 这种编写代码的方式叫做 try-with-resources, 这是从JDK7开始支持的技术
// 所有的流,都实现了一个接口叫做 AutoCloseable,任何类实现了这个接口,都可以在try()中进行实例化,
// 并且在try, catch, finally结束的时候自动关闭,回收相关资源。
try(FileInputStream FIS = new FileInputStream(f);
FileInputStream FIS1 = new FileInputStream(f1);
FileOutputStream FOS = new FileOutputStream(f);
FileOutputStream FOS1 = new FileOutputStream(f1);){
byte[] data = {'a','A'};
byte[] all = new byte[(int) data.length];
byte[] all1 = new byte[(int) data.length];
//将data入写1.txt
FOS.write(data);
//读1.txt
FIS.read(all);
//将1.txt的内容写到2.txt
FOS1.write(all);
//读2.txt
FIS1.read(all1);
for(byte a : all1) {
System.out.println((char)a);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//打印父文件夹中所有的文件
File[] r = f.getParentFile().listFiles();
for(File s : r){
System.out.println(s);
}
}
}
运行结果
控制台输出
二、字符流
Reader字符输入流
Writer字符输出流
专门用于字符的形式读取和写入数据
参考代码
package review4;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class TestStream {
public static void main(String[] args) throws IOException {
File f = new File("D:/Test/3.txt");
//创建Test文件夹
f.getParentFile().mkdirs();
//创建3.txt文件
f.createNewFile();
try (FileWriter fw = new FileWriter(f)){
String s = "abcdefg";
//将字符串s写进3.txt文件中
char[] write = s.toCharArray();
fw.write(write);
} catch (Exception e) {
e.printStackTrace();
}
try (FileReader fr = new FileReader(f)){
//从3.txt中读出数据
char[] read = new char[(int) f.length()];
System.out.println(f.length());
fr.read(read);
for(char r : read) {
System.out.print(r+"\t");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果
控制台输出
三、用FileInputStream 字节流正确读取中文
为了能够正确的读取中文内容必须了解文本是以哪种编码方式保存字符的, 使用字节流读取了文本后,再使用对应的编码方式去识别这些数字,得到正确的字符
参考代码
package review4;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class TestStream {
public static void main(String[] args) throws IOException {
File f = new File("d:Test/test.txt");
try (FileInputStream fis = new FileInputStream(f);) {
byte[] all = new byte[(int) f.length()];
fis.read(all);
//文件中读出来的数据是
System.out.println("文件中读出来的数据是:");
for (byte b : all)
{
int i = b&0x000000ff; //只取16进制的后两位
System.out.println(Integer.toHexString(i));
}
System.out.println("把这个数字,放在GBK的棋盘上去:");
String str = new String(all,"GBK");
System.out.println(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果
四、用FileReader 字符流正确读取中文
FileReader得到的是字符,所以一定是已经把字节根据某种编码识别成了字符了
而FileReader使用的编码方式是Charset.defaultCharset()的返回值,如果是中文的操作系统,就是GBK
FileReader是不能手动设置编码方式的,为了使用其他的编码方式,只能使用InputStreamReader来代替,比如:new InputStreamReader(new FileInputStream(f),Charset.forName(“UTF-8”));
在本例中,用记事本另存为UTF-8格式,然后用UTF-8就能识别对应的中文。
解释: 为什么中字前面有一个?
如果是使用记事本另存为UTF-8的格式,那么在第一个字节有一个标示符,叫做BOM用来标志这个文件是用UTF-8来编码的。
参考代码
package review4;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
public class TestStream {
public static void main(String[] args) throws IOException {
File f = new File("d:Test/test1.txt");
System.out.println("默认的编码方式:"+Charset.defaultCharset());
//FileReader得到的是字符,所以一定是已经把字节根据某种编码识别成了字符了
//而FileReader使用的编码方式是Charset.defaultCharset()的返回值,如果是中文的操作系统,就是GBK
try (FileReader fis = new FileReader(f);) {
char[] all = new char[(int) f.length()];
fis.read(all);
System.out.printf("FileReader会使用默认的编码方式%s,识别出来的字符是:%n",Charset.defaultCharset());
System.out.println(new String(all));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//FileReader是不能手动设置编码方式的,为了使用其他的编码方式,只能使用InputStreamReader来代替
//并且使用new InputStreamReader(new FileInputStream(f),Charset.forName("UTF-8")); 这样的形式
try(InputStreamReader isr = new InputStreamReader(new FileInputStream(f),Charset.forName("UTF-8"))) {
char[] all = new char[(int) f.length()];
isr.read(all);
System.out.printf("InputStreamReader 指定编码方式UTF-8,识别出来的字符是:%n");
System.out.println(new String(all));
} catch (Exception e) {
// TODO: handle exception
}
}
}
运行结果