1:你理解什么是io?
io其实就是输入和输出,input,ouput再加上Stream流的概念,还有一个就是数据的概念,应该io都是操作数据的,总结就是io就是用来做数据传输的
2:介绍一下java的File类
1:操作流的时候必须是先拿到这个文件,只有拿到这个文件才能对文件进行操作,所以File类是拿到这个文件的一个类
2:拿到一个文件
3:File下的方法,File提供了操作文件的各种方法,支持我们操作文件
4:通过File创建文件,注意这块需要判断文件是否已经存在与这个路劲下
3:介绍一个io中的流怎么操作
1:首先对应不同的字节数,用不同的类来操作文件
字节流
对于字节流,它是用来操作我们的二进制文件的,为什么呢?因为字节流可以操作的数据是8位,也就是一字节,我们知道1 byte = 8bit,而像一些数字和字母等都是占一个字节,这就可以使用字节流来操作,但是对于中文的话就不能使用字节流了
字符流
因为一个汉字是占两个字节,那么就是16位,字节流是操作不了的,而字符流则可以操作16位,所以对于文本文件则常用字符流操作了
操作字节常用的有:
FileInputStrea
FileOutputStrem
操作字符常用的有:
BufferedReader
FileReader
InputStremReader
BufferedWriter
OutputStreamWriter
FileWirter
字节流
先来看看读取文本
对于到底是使用字节流还是字符流我们要明白重点是什么,重点是你要操作的数据,如果是中文的话我们当然不能使用字节流而要使用字符流,但是如果你的文本内容是一些数字或者字母,这就可以使用我们的字节流啊,对于字节流和字符流是都可以用来操作文本文件的,关键看你的文本内容
public class java_io {
public static void main(String[] args) throws IOException {
//先得知道要操作谁
File file = new File("E:/abc.txt");
//判断文件是否存在
if (!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}else {
System.out.println("File exists");
}
//创建输入流,参数为File
FileInputStream fileInputStream = new FileInputStream(file);
//进行读取文件,并转为char类型的,因为默认返回的是int类型的
char read = (char)fileInputStream.read();
//read的读取方式为一个个的读取,也就是说你调用一次read就会读取下一个字节
while (read>0){
System.out.println(read);
read = (char)fileInputStream.read();
}
}
}
我们知道FileInputStrem是一个输入流,可能说输入流你会犯晕,不理解,我们其实是可以把输入流理解为”拿到“,比如这里我们使用FileInputStrem就是拿到a.txt中文本内容,我的文本中有abc三个字母,然后我们第一次调用read方法会帮你拿到a,我们看返回的是一个int类型,可以知道返回的其实是a的ASCII码,我们需要使用char来进行转换,我们在代码中也说了,当read的返回值是-1的话也就代表文件读取完毕,这里我们要注意的就是read每调用一次就会读取文本内容的下一个字节
其实实际开发中,这样使用效率太低了,一般都这样
package com.example.demo;
import java.io.*;
public class java_io {
public static void main(String[] args) throws IOException {
//先得知道要操作谁
File file = new File("E:/abc.txt");
//判断文件是否存在
if (!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}else {
System.out.println("File exists");
}
byte[] bytes = new byte[4]; //1kb
//创建输入流,参数为File
FileInputStream fileInputStream = new FileInputStream(file);
//以一个kb的方式去读取,如果文本内容不超过一个kb的话就会全部读取,将读取到的内容放在byte数组中并且返回文本内容的字节数
int read = fileInputStream.read(bytes);
//read的读取方式为一个个的读取,也就是说你调用一次read就会读取下一个字节
System.out.println(new String(bytes));
}
}
为什么要定义这么一个数组,我们看这个byte数组其实就是1kb大小,我们之前是一个字节一个字节读取,我们现在是1kb1kb的去读,然后这样做
再来看看写入文本
package com.example.demo;
import java.io.*;
public class java_io {
public static void main(String[] args) throws IOException {
//先得知道要操作谁
File file = new File("E:/abc.txt");
File file1 = new File("E:/cba.txt");
//判断文件是否存在
if (!file.exists()){
try {
file.createNewFile();
file1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}else {
System.out.println("File exists");
}
byte[] bytes = new byte[4]; //1kb
//创建输入流,参数为File
//读
FileInputStream fileInputStream = new FileInputStream(file);
//写
FileOutputStream fileOutputStream = new FileOutputStream(file1);
//以一个kb的方式去读取,如果文本内容不超过一个kb的话就会全部读取,将读取到的内容放在byte数组中并且返回文本内容的字节数
//read的读取方式为一个个的读取,也就是说你调用一次read就会读取下一个字节
int read = fileInputStream.read(bytes);
//直接用bytes写进去
fileOutputStream.write(bytes);
System.out.println(new String(bytes));
}
}
这种方式其实是覆盖文件本生的内容,如果不想覆盖的话:
FileOutputStream fileOutputStream = new FileOutputStream("F:/b.txt",true);
字符流
字节流是按字节读取那就是byte了
对于字符流,像字节流中的InputStrem和OutputStrem一样,也有两个接口那就是Reader和Writer,当然我们也是需要去学习它们的实现类,同样,它们的实现类有好几个,我们不可能每一个都学,我们会说几个比较重要的。
说一下字符流中的Reader
对于Reader,我们需要首先熟悉并且会使用它的这几个实现类,对了,这里说的可都是输入流
BufferedReader
FileReader
InputStremReader
InputStremReader这个方法可以传入char类型的参数,这样就可以识别中文了,英文中文占两个字节,而char类型正好是两个字节
import java.io.*;
public class java_io_input {
public static void main(String[] args) {
File file = new File("E:/io.txt");
try {
boolean newFile = file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
FileWriter fileWriter = new FileWriter(file);
fileWriter.write("你好我是陈丽");
fileWriter.close();
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"utf-8");
char[] chars = new char[1024];
inputStreamReader.read(chars);
System.out.println(new String(chars));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
说说FileReader的使用,跟InputStremReader有什么区别
package com.example.demo;
import java.io.*;
public class java_io_input {
public static void main(String[] args) {
File file = new File("E:/io.txt");
try {
boolean newFile = file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
FileWriter fileWriter = new FileWriter(file);
fileWriter.write("你好我是陈丽");
fileWriter.close();
// InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"utf-8");
char[] chars = new char[1024];
FileReader fileReader = new FileReader(file);
fileReader.read(chars);
// inputStreamReader.read(chars);
System.out.println(new String(chars));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
对于FileReader它不需要像InputStremReader一样传入一个字节流,同样好像也不能直接传入编码格式吧,至于其他的操作几乎就是一样的了
聊一下BufferedReader的使用
在平常使用的挺多的,相信你应该也有印象,我们使用BufferedReader读取文本内容可以保证文本内容的格式不被打乱,反正吧,关于BufferedReader的用法一定要熟练,BufferedReader我们叫做缓冲流,当然是对Reader做缓冲的,所以使用BufferedReader一定会用到Reader,接下来我们看看代码就知道了
package com.example.demo;
import java.io.*;
public class java_io_input {
public static void main(String[] args) {
File file = new File("E:/io.txt");
try {
boolean newFile = file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line = null;
while ((line = bufferedReader.readLine())!=null){
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这段代码一定要记住了,烂熟于心,我们看代码知道这个BufferedReader需要接收一个字符流 InputStreamReader,然后就可以直接调取readLine方法进行整行读取,会得到一个字符串,可以直接输出,因为是整行读取可以保持格式不乱,真的很好用,所以要记住了
聊一聊io有多少类
其实IO这块一些实现类很多,我们不可能每一个都掌握,我们只需要熟悉常见的并且会使用即可,至于其他的一些我们用到的时候查一下即可,下面一张图,给你感受下
那你聊下Writer这个类吧,都读取了,还没有输入呢
package com.example.demo;
import java.io.*;
public class java_io_input {
public static void main(String[] args) {
File file = new File("E:/io.txt");
try {
boolean newFile = file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
try (
//输出
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
//输入
FileOutputStream fileOutputStream = new FileOutputStream("E:/abc.txt");
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
) {
//循环读取并放入到
String line = null;
while ((line = bufferedReader.readLine())!=null){
System.out.println(line);
bufferedWriter.write(line);
//换行
bufferedWriter.newLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
注意这里必须需要把用过的流进行关闭,使用到了:
try(创建的流对象){
对输入输出流进行操作的语句
}
这样的方式去自动关闭创建好的流,如果不关闭的话,是无法写进去的