IO流文件读写

发布于:2023-01-13 ⋅ 阅读:(1315) ⋅ 点赞:(1)

IO流


IO流的作用

IO可以将数据存储到本地文档中,也可以从本地文档中读取数据。



对文件的相关操作

代码实例

 public static void main(String[] args){
 	String path="F:\file\ooo\亚索.txt";//文件路径
	File file=new File(path);
	file.creatNewFile();//创文件
	file.mkdir();//创文件加
	file.exists();//判断文件是否存在
	file.isFile();//判断是否是文件
	file.isDirectory();//判断是否是目录
	Arrays.toString(file.list());//显现目录下的所有文件名
	file.getAbsolutePth();//获取文件对象的路径
	file.delete();//文件删除
	file.rename To(new File("名称"));//文件重命名
}

以上是对文件的基本操作,知道就可以


File常用的实现类:
字节输入流:FileInputStream
字节输出流:FileOutputStream
字符输入流:FileReader
字符输出流:FileWriter
缓冲字符输入流:BufferedReader
缓冲字符输出流:bufferedWriter
缓冲字节输入流:BufferedInputStream
缓冲字节输出流:BufferedOutputStream
打印流:PrintWriter,PrintSteam
对象输入流:ObjectInputStream
对象输出流:ObjectOutputStream

字节输入流


文件信息以字节的形式传输,单位为byte,所以用byte[]


代码实例:


import java.io.*;

public class Test{
	public static void mian(String[] args){
		String path="F:\file\ooo\亚索.txt";//读取文件的地址
		File file=new File(path);
		FileInputStream fis=null;
        try {
            fis=new FileInputStream(file);
            byte[] b=new byte[1024];//准备一个byte类型的数组
            int len=fis.read(b);//将读取到的信息存到数组中
            while(len!=-1){
                String s=new String(b);//将byte数组转换成字符串
                System.out.println(s);
                len=fis.read(b);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
	}
}

字节输出流


文件信息以字节的形式传输


缓冲区
我们再向磁盘写东西时,主要是往缓冲区写,缓冲区再负责将数据真正的写到磁盘中,读文件信息同理。

大文件信息在传输过程中,会在一次性写完在一次性输出,会出现等待,这样导致速率低下。这时,缓冲区就起到了提高效率的作用,我们只用在缓冲区写东西,缓冲区在一次或分批次写入到磁盘中,实现边写边存,提高了效率。


代码实例:


import java.io.*;

public class Zjshuchu {
    public static void main(String[] args) {
        String path="F:\\file\\ooo\\one.txt";
        String str="你好,世界";
        File file = new File(path);
        FileOutputStream fos=null;
        try {
            fos=new FileOutputStream(path,true);
            byte[] b=str.getBytes();
            fos.write(b);
            fos.flush();//清空缓冲区,并强制写入
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


字符输入流


文件信息以字符的形式传输,所以用char[]


代码实例:


import java.io.*; 

public class Zifshuru {
    public static void main(String[] args) {
        String path="F:\\file\\ooo\\亚索.txt" ;
        File file=new File(path);
        FileReader fr=null;
        try {
           fr=new FileReader(file);
           char[] c=new char[1024];
           int len=fr.read(c);
           while (len!=-1){
               String s=new String(c);
               System.out.println(s);
               len=fr.read(c);
           }
           
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

字符输出流


文件信息以字符的形式传输


代码实例:


import java.io.*;

public class Zfshuchu {
    public static void main(String[] args) {
        String str="世界";
        String path="F:\\file\\ooo\\lll.txt";
        File file =new File(path);
        FileWriter fw=null;
        try {
           fw=new FileWriter(file,true);
           fw.write(str);
           fw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

缓冲流


对缓冲区的功能一个加强
这里以字符的缓冲流来进行代码演示(字节同理)
对文件信息进行逐行读取


代码实例:


import java.io.*;

public class Hc {
    public static void main(String[] args) {
        File file=new File("F:\\file\\ooo\\亚索.txt");
        BufferedWriter bw=null;
        BufferedReader br=null;
        try {
            bw=new BufferedWriter(new FileWriter("F:\\file\\ooo\\oe.txt"));
            br=new BufferedReader(new FileReader(file));
            String str=br.readLine();
            while(str!=null){
                System.out.println(str);
                bw.write(str+"\n");
                bw.flush();
                str=br.readLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

打印流

只有输出流,用于数据的打印和显示

代码实例:以PrintWriter为例


import java.io.*;

public class dayin {
    public static void main(String[] args) {
        //打印到控制台
        String s="hello world";
        PrintWriter pw=new PrintWriter(System.out);
        pw.println(s);
        pw.close();

        //打印到文件 文件地址为F:\file\ooo\hellp.txt
        PrintWriter pw1=null;
        try {
            pw1=new PrintWriter(new FileWriter("F:\\file\\ooo\\hellp.txt",true));
            pw1.print("hello world");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            pw1.close();
        }
    }
}

对象输出流

将对象信息写入本地文件
注意:要将对象类的信息实现序列化,即对象类要实现Serializable接口

代码实例:


import java.io.*;
import java.util.ArrayList;

public class Duixiang {
    public static void main(String[] args) {
        Student s1=new Student("小明",12);
        Student s2=new Student("小明",12);
        Student s3=new Student("小明",12);
        ArrayList<Student> al=new ArrayList<>();
        al.add(s1);
        al.add(s2);
        al.add(s3);

        File file=new File("F:\\file\\ooo\\jjj.txt");
        ObjectOutputStream oos=null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(al);//写入对象集合
            oos.flush();
            oos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}


对象输入流

将本地文件的对象信息写入程序中


代码展示:


import java.io.*;
import java.util.ArrayList;

public class Duixiang2 {
    public static void main(String[] args){

        ObjectInputStream ois=null;
        try {
            ois = new ObjectInputStream(new FileInputStream("F:\\file\\ooo\\jjj.txt"));
            ArrayList<Student> Stuarray = (ArrayList<Student>) ois.readObject();
            for(Student ss:Stuarray){
                System.out.println(ss);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

总结

以上就是这次要讲的内容,本文仅仅简单介绍了IO的使用,多多练习,技术硬,升职加薪才有希望。