感觉JavaIO流好抽象啊,各位有没有什么书籍或者文章能帮助理解IO流的。感谢了Ծ‸ Ծ
java.io包中提供了一些接口和类,对文件进行基本操作,包括对文件和目录属性的操作、对文件读写的操作等。
方法 | 说明 |
---|---|
File(String pathname) | 用指定的文件路径构造文件 |
File(String dir,String subpath) | 在指定目录下创建指定文件名的文件,dir:目录路径,subpath:文件名 |
File(File parent,String subpath) | 根据一个文件对象和一个字文件构造文件对象,parent:目录文件,subpath:文件名 |
方法名称 | 说明 |
---|---|
boolean exists( ) | 判断文件或目录是否存在 |
boolean isFile( ) | 判断是否是文件 |
boolean isDirectory( ) | 判断是否是目录 |
String getPath( ) | 返回此对象表示的文件的相对路径名 |
String getAbsolutePath( ) | 返回此对象表示的文件的绝对路径名 |
String getName( ) | 返回此对象表示的文件或目录的名称 |
boolean delete( ) | 删除此对象指定的文件或目录 |
boolean createNewFile( ) | 创建名称的空文件,不创建文件夹 |
long length() | 返回文件的长度,单位为字节,如果文件不存在,则返回0L |
String getParent() | 返回File对象的路径名的上一级,如果没有返回null |
boolean mkdir() | 创建一个目录,路径名由File对象指定 |
boolean mkdirs() | 创建包括父目录的目录 |
import java.io.File;
File file=new File("test.txt");
file.lastModified(); //获取文件最后修改日期
方法 | 说明 |
---|---|
int read() | 从输入流读取下一个字节数据 |
int read(byte[] b) | 从输入流中读取数据,并将数据存储在缓冲区数组b中,并返回时机读取的字节数 |
int read(byte[] b,int off,int len) | 从输入流中读取最多len长度的字节,保存到字节数组b中,保存的位置从off开始,返回时机读取字符数 |
void close() | 关闭输入流 |
方法 | 说明 |
---|---|
int read() | 从输入流读取单个字符,返回所读取的字符数据 |
int read(byte[] c) | 从输入流读取最多c.length个字符,保存到字符数组c中,返回实际读取的字符数 |
int read(byte[] c,int off,int len) | 从输入流中读取最多len长度的字节,保存到字节数组c中,保存的位置从off开始,返回时机读取字符数 |
void close() | 关闭输入流 |
方法 | 描述 |
---|---|
void write(int c) | 将指定的字节数据写入到输出流中 |
void write(byte[] buf) | 将数组buf中的所有字节写入输出流中 |
void write(byte b,int off,int len) | 将字节数组中从偏移量off开始的长度为len的字节数据输出到输出流中 |
void close() | 关闭输入流 |
方法 | 说明 |
---|---|
void write(String str) | 将str字符串里包含的字符输出到指定的输出流中 |
void write(Stringstr,int off,int len) | 将str字符串里从off位置开始,长度为len的多个字符输出到输出流中 |
void close() | 关闭输出流 |
void flush() | 刷新输出流 |
使用字节流FileInputStream读文本文件:
FileInputStream称为文件输入流,是字节输入流inputStream抽象类的子类,作用是将文件中的数据输入到内存中.
步骤:
File file=new File("1.txt")
//创建流对象
FileInputStream fis=new FileInputStream(file);
int data;
System.out.println("可读取的字节数:"+fis.available());
System.out.println("文件内容为:");
//循环读数据
while ((data=fis.read())!=-1){
System.out.println(data+"");
}
fis.close();
使用字节流FileOutputStream写文件:
FileOutputStream称为文件输出流,是字节输出流OutputStream抽象类的子类,作用是把内存中的数据输出到文件中
步骤:
String str="java";
byte[] b=str.getBytes();//字节数组
FileOutputStream fos=new FileOutputStream("1.txt",true);
fos.write(b,0,b.length);
System.out.println("文件已更新");
fos.flush();
fos.close();
使用字符流BufferedReader和FileReader读文本文件
BufferedReader和FileReader两个类都是Reader抽象类的子类,可以通过字符流方式读取文件,并使用缓冲区,提高了读文本文件效率
步骤
FileReader fr=new FileReader("1.txt");
BufferedReader br=new BufferedReader(fr);
String line=br.readLine();
while(line!=null){
System.out.println(line);
line=br.readLine();
}
br.close();
fr.close();
使用字符流BufferedWriter和FileWriter写文本文件
BufferedWriter和FileWriter都是字符输出流Writer抽象类的子类,可以通过字符流的方式并通过缓冲区把数据写入文本文件,提高写文本文件的效率
步骤
FileWriter fw=new FileWriter("1.txt",true);
BufferedWriter bw=new BufferedWriter(fw);
bw.write("java");
bw.newLine();
bw.write("bdqn")
System.out.println("写入成功!");
bw.flush();
bw.close();
fw.close();
//字节流读文件
FileInputStream fis=new FileInputStream("C:/Users/29616/desktop/1.class");
//字节流读二进制文件
DataInputStream dis=new DataInputStream(fis);
//字节流写文件
FileOutputStream fos=new FileOutputStream("C:/Users/29616/desktop/2.class");
//字节流写二进制文件
DataOutputStream dos=new DataOutputStream(fos);
int temp;
while ((temp=dis.read())!=-1){ //读数据
dos.write(temp); //把读取数据写入到2.clss中
}
System.out.println("读写成功");
fis.close();
fos.close();
dis.close();
dos.close();
标准输入/输出流: System.in/System.out,主要用于从键盘接收数据和向屏幕输出数据。
System.in方法:
System.out方法:
标准I/O重新定向到其他I/O设备:
方法 | 说明 |
---|---|
static void setErr(printStream err) | 重定向标准错误输出流 |
static void setIn(InputStream in) | 重定向标准输入流 |
static void setOut(PrintStream out) | 重定向标准输出流 |
//创建PrintStream输出流
PrintStream ps=new PrintStream(new FileOutputStream("C:/Users/29616/desktop/1.txt"));
//将标准输出流重定向到文件
System.setOut(ps);
//输出内容
System.out.print("测试,重定向到文件");
//student类
public class Student implements Serializable {
//属性:姓名、年龄、性别
//省略构造方法和getter、setter方法
}
//序列化
ObjectOutputStream oos=null;
try {
//创建ObjectOutputStream对象
oos=new ObjectOutputStream(new FileOutputStream("1.txt"));
Student stu1=new Student("小薛","男",20);
Student stu2=new Student("小王","女",21);
ArrayList<Student> list=new ArrayList<>();
list.add(stu1);
list.add(stu2);
//对象序列化,写入输出流
oos.writeObject(list);
}catch (IOException ex){
ex.printStackTrace();
}finally {
if(oos!=null){
try{
oos.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
//反序列化
ObjectInputStream ois=null;
try {
//创建ObjectInputStream输入流
ois=new ObjectInputStream(new FileInputStream("1.txt"));
//反序列化,进行类型强制转换
ArrayList<Student> list=(ArrayList<Student>)ois.readObject();
//输出生成后的对象信息
for (Student student : list) {
System.out.println("姓名:"+student.getName()+"\t性别:"+student.getSex()+"\t年龄:"+student.getAge());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
注意: 1. 如果文件中使用序列化机制写入多个对象,反序列化恢复对象时,必须按照写入顺序读取.
2. 如果一个可序列化的类,有多个父类(包括直接或间接父类),则这些父类要么是可序列化的,有么有无参构造器,否则会抛出异常