IO流顾名思义,就是输入输出流。用于处理设备之间数据的传输。
流的分类
- 按操作数据单位不同分为:字节流(8bit)、字符流(16bit)
- 按数据流的流向分为:输入流、输出流
- 按流的角色分为:节点流、处理流
字节流(非文本文件) byte | 字符流(文本文件) char | |
输入流 | InputStream | Reader |
输出流 | OutputStream | Writer |
具体分类如下图所示:
FileInputStream实现数据的读入操作
注:inputStream读取中文时会出现乱码,因为它采用的是byte存储,而一个汉字在utf-8编码中占3个字节。
@Test
public void testInputStream() throws IOException {
//1.造文件
File file = new File("hello.txt");
//2.造流
FileInputStream fi = new FileInputStream(file);
//3.读入数据的操作
byte[] b = new byte[(int) file.length()];//根据文件长度来设定数组的长度
int len;
while ((len = fi.read(b)) != -1) {
String s = new String(b, 0, len);
System.out.print(s);
}
//4.关闭流操作
fi.close();
}
常用的几种方法:
- read() 读取一个字节并将读取的字节作为int返回。当到达输入流的结尾时,它返回-1。
- read(byte[] buffer)读取最大值直到指定缓冲区的长度。它返回在缓冲区中读取的字节数。如果到达输入流的结尾,则返回-1。
- read(byte [] buffer,int offset,int length)读取最大值到指定长度字节。 数据从偏移索引开始写入缓冲区。它返回读取的字节数或-1,如果到达输入流的结束。
- close()关闭流
- write(byte b[])把指定数组中b.length长度的字节写到OutputStream中
- write(byte b[], int off, int len)写出最大值到指定长度字节
利用FileInputStream和FileOutputStream实现文件的复制,也就是利用输入和输出流进行的读写操作(这里采用的是图片的复制)
@Test
public void test() throws IOException {
//1.造文件(源文件和目的文件)
File srcfile = new File("bz.jpg");
File decfile = new File("bz1.jpg");
//2.造流(输入流和输出流)
FileInputStream fi = new FileInputStream(srcfile);
FileOutputStream fos = new FileOutputStream(decfile);
//3.读取和写出操作
byte[] b = new byte[(int) srcfile.length()];
int len;
while ((len = fi.read(b)) != -1) {
fos.write(b, 0, len);
}
//4.关闭两个流
fi.close();
fos.close();
}
另外补充:在idea中,在当前Moudule下创建的文本文件,在不同的方法中,其路径书写也有不同,下面解释了在main 方法中和单元测试方法中路径的书写方法。
public static void main(String[] args) {
File file = new File( pathname: "hello.txt");//相较于当前工程
System.out.print1n(file.getAbsolutePath());
File file1 = new File( pathname: "day09\\hello.txt");//相较于当前Module
System.out.print1n(file1.getAbsolutePath());
}
@Test
public void testFileReader(){
File file = new File( pathname: "hello.txt");//相较于当前Module
}
打印流
转换流
关于字符流和字节流的一些练习
/**
* 使用字节流把a文件中的数据写到b文件中
* @throws IOException
*/
public static void aTob() throws IOException {
File file1= new File("D:\\a.txt");
File file2 = new File("E:\\b.txt");
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2,true);
byte[] b=new byte[(int) file1.length()];
int len=-1;
while ((len=fis.read(b))!=-1){
fos.write(b);
}
fis.close();
fos.close();
}
/**
* 使用字节流把a文件中的数据转换后写到b文件中 大写转换为小写 小写转换为大写 删除数字
* @throws IOException
*/
public static void input() throws IOException {
File file1= new File("D:\\a.txt");
File file2 = new File("E:\\c.txt");
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
byte[] bytes=new byte[(int) file1.length()];
int len;
while ((len=fis.read(bytes))!=-1){
String s = new String(bytes);
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
if(Character.isUpperCase(chars[i])){
chars[i]=Character.toLowerCase(chars[i]);
}else if(Character.isLowerCase(chars[i])){
chars[i]=Character.toUpperCase(chars[i]);
}else if(Character.isDigit(chars[i])){
chars[i]=' ';
}
}
String s1=new String(chars);
String s2 = s1.replaceAll(" ", "");
System.out.println(s2.length());
byte[] bytes1 = s2.getBytes();
fos.write(bytes1);
}
fis.close();
fos.close();
}
/**
* 使用字符流把a文件中的数据转换后写到b文件中 大写转换为小写 小写转换为大写 删除数字
* @throws IOException
*/
public static void reader() throws IOException{
File file1= new File("D:\\a.txt");
File file2 = new File("E:\\b.txt");
FileReader fr = new FileReader(file1);
FileWriter fw = new FileWriter(file2);
char[] chars = new char[(int) file1.length()];
int len;
while ((len=fr.read(chars))!=-1){
//将读取到chars数组的文件转换为字符串
String str=new String(chars);
//将字符串转换为字符进行大小写转换
char[] chars2 = str.toCharArray();
for (int i = 0; i < chars2.length; i++) {
if(Character.isUpperCase(chars2[i])){
chars2[i]=Character.toLowerCase(chars2[i]);
}else if(Character.isLowerCase(chars2[i])){
chars2[i]=Character.toUpperCase(chars2[i]);
}else if(Character.isDigit(chars2[i])){
chars2[i]=' ';
}
}
//创建一个新数组把转换后字符存储到里面
String s1=new String(chars2);
//把新数组中去除空字符后再放入一个新数组中
String s3 = s1.replaceAll(" ", "");
System.out.println(s3.length());//输出下需要被写的字符串长度
char[] chars1 = s3.toCharArray();//字符串转换为字符数组,传入writer方法中
fw.write(chars1);
}
fr.close();
fw.close();
}
/**
* 把键盘写入的数据 数字写入a文件中 非数字写入b文件中
* @throws IOException
*/
public static void panduan() throws IOException{
Scanner scanner = new Scanner(System.in);
String next = scanner.next();
File file1 = new File("E:\\shuzi.txt");
File file2 = new File("E:\\fs.txt");
char[] chars = next.toCharArray();
for (int i = 0; i < chars.length; i++) {
if(Character.isDigit(chars[i])){
FileOutputStream fos = new FileOutputStream(file1,true);
fos.write(chars[i]);
fos.close();
}else {
FileOutputStream fos2 = new FileOutputStream(file2,true);
fos2.write(chars[i]);
fos2.close();
}
}
}