Java的IO流操作文件教程(直接上代码!!!,代码注释较多,容易看懂)

一、字节流

字节流从文件中读取内容

public void fileInputStreamTest(String intPath){//参数为读取文件地址
        try {
            FileInputStream in = new FileInputStream(intPath);//字节流
            byte[] b=new byte[200];
            //in.read(b);//读取文件内容,存到字节数组
            int len=0;
            while((len=in.read(b))!=-1) {
                System.out.println(new String(b,0,len));//输出文件内容
                //new Stirng(a,s,l) 参数1是缓冲数据的数组,参数2是从数组的哪个位置开始转化字符串,参数3是总共转化几个
            }
            in.close();//流在使用完之后要关闭
        }catch(Exception e){
            e.printStackTrace();
        }
}

字节流将内容写入文件

public void fileOutputStreamTest(String text,String outPath){//参数二为写入文件地址,如果文件不存在,则自动创建一个文件
        try{
            FileOutputStream out=new FileOutputStream(outPath);
            out.write(text.getBytes(StandardCharsets.UTF_8));//把数据写到内存
            out.flush();//把内存中的数据刷写到硬盘
            out.close();//关闭流
        }catch(Exception e){
            e.printStackTrace();
        }
}

字节流复制文件

public void copyFile(String intPath,String outPath){//复制文件
        try{
            FileInputStream in=new FileInputStream(intPath);//读取原文件
            FileOutputStream out=new FileOutputStream(outPath);//复制到新文件
            byte[] b=new byte[100];
            int len=0;
            while((len=in.read(b))!=-1){
                out.write(b,0,len);//把数据写到内存
                out.flush();//把写到内存的数据刷写到硬盘
            }
            out.close();//关闭流
            in.close();//关闭流
        } catch (Exception e) {
            e.printStackTrace();
        }
}

二、字符流

字符流从文件中读取内容

public void fileReaderTest(String inPath){//字符流读取文件内容
        try {
            FileReader in = new FileReader(inPath);
            char[] ch=new char[1024];
            int len=0;
            while((len=in.read(ch))!=-1)
            {
                System.out.println(new String(ch,0,len));
            }
            in.close();//流在使用完之后要关闭
        }catch(Exception e)
        {
            e.printStackTrace();
        }
}

字符流将内容写入文件

public void fileWriterTset(String text,String outPath){//字符流向文件存入数据
        try{
            FileWriter wr=new FileWriter(outPath);
            wr.write(text);//将内容写到内存中
            wr.flush();//把内存的数据刷写到硬盘
            wr.close();//流在使用完之后要关闭
        }catch(Exception e){
            e.printStackTrace();
        }
}

字符流复制文件

public void copyFiled(String inPath,String outPath){//字符流复制文件
        try{
            FileReader in=new FileReader(inPath);
            FileWriter out=new FileWriter(outPath);
            char[] ch=new char[1024];
            int len=0;
            while((len=in.read(ch))!=-1){
                out.write(ch,0,len);
                out.flush();
            }
            out.close();
            in.close();
        }catch(Exception e){
            e.printStackTrace();
}

三、缓冲流

使用缓冲数组以后,整体的读取、写入效率提升很大。降低了CPU通过内存访问硬盘的次数。提高效率,降低磁盘损耗。

  1. 缓冲字节流

缓冲字节流读取文件内容

//BufferedInputStream,缓冲字节输入流
public void bufferInputStream(String inPath) throws Exception{
        FileInputStream in=new FileInputStream(inPath);//创建字节输入流对象
        BufferedInputStream bi=new BufferedInputStream(in);//将字节输入流对象放到缓冲字节输入流中
        byte[] b=new byte[100];
        int len=0;
        while((len=bi.read(b))!=-1){
            System.out.println(new String(b,0,len));
        }
        bi.close();
        in.close();
}

缓冲字节流将内容写入文件

//BufferedOutputStream,缓冲字节输出流
public void bufferOutputStream(String text,String outPath) throws Exception{
        FileOutputStream out=new FileOutputStream(outPath);//创建字节输出流对象
        BufferedOutputStream bo=new BufferedOutputStream(out);//将字节输出流对象放到缓冲字节输出流中
        bo.write(text.getBytes(StandardCharsets.UTF_8));//写到内存中
        bo.flush();//刷写到硬盘上
        bo.close();
        out.close();
}

缓冲字节流复制文件

public void copyBuffer(String inPath,String outPath) throws Exception{
        FileInputStream in=new FileInputStream(inPath);//创建字节输入流对象
        BufferedInputStream bi=new BufferedInputStream(in);//将字节输入流对象放到缓冲字节输入流中
        FileOutputStream out=new FileOutputStream(outPath);//创建字节输出流对象
        BufferedOutputStream bo=new BufferedOutputStream(out);//将字节输出流对象放到缓冲字节输出流中
        byte[] b=new byte[1024];
        int len=0;
        while((len=bi.read(b))!=-1){
            bo.write(b,0,len);//写到内存
            bo.flush();//刷写到硬盘上
        }
        bo.close();//关闭流
        out.close();
        bi.close();
        in.close();
}
  1. 缓冲字符流

缓冲字符流读取文件内容

//BufferedReader,缓冲字符输入流
public void bufferInputReader(String inPath)throws Exception{
        FileReader in=new FileReader(inPath);
        BufferedReader br=new BufferedReader(in);
        char[] ch=new char[1024];
        int len=0;
        while((len=br.read(ch))!=-1){//read()方法读到文件的最后一个字符的下一位,就返回-1
            System.out.println(new String(ch,0,len));
        }
        br.close();
        in.close();
}

缓冲字符流将内容写到文件中

//BufferedWriter,缓冲字符输出流
public void bufferOutputWriter(String text,String outPath)throws Exception{
        FileWriter out=new FileWriter(outPath);
        BufferedWriter bw=new BufferedWriter(out);
        bw.write(text);
        bw.flush();
        bw.close();
        out.close();
}

缓冲字符流复制文件

public void copyBuffered(String inPath,String outPath)throws Exception{
        FileReader in=new FileReader(inPath);
        BufferedReader br=new BufferedReader(in);
        FileWriter out=new FileWriter(outPath);
        BufferedWriter bw=new BufferedWriter(out);
        char[] ch=new char[1024];
        int len=0;
        while((len=br.read(ch))!=-1){
            bw.write(ch,0,len);
            bw.flush();
        }
        bw.close();
        out.close();
        br.close();
        in.close();
}

四、转换流

字节输入流转换为字符输入流读取文件内容

//转换字节输入流为字符输入流
public void transformInputStream(String inPath)throws Exception{
        FileInputStream fi=new FileInputStream(inPath);
        InputStreamReader in=new InputStreamReader(fi,"UTF-8");
        char[] ch=new char[1024];
        int len=0;
        while((len=in.read(ch))!=-1){
            System.out.println(new String(ch,0,len));
        }
        in.close();
        fi.close();
}

字节输出流转换为字符输出流将内容写入文件

//转换字节输出流为字符输出流
public void transformOutputStream(String text,String outPath)throws Exception{
        FileOutputStream fo=new FileOutputStream(outPath);
        OutputStreamWriter out=new OutputStreamWriter(fo,"UTF-8");
        out.write(text);
        out.flush();
        out.close();
        fo.close();
}

转换流实现文件复制

//转换流复制文件
public void copyFileByTransformStream(String inPath,String outPath)throws Exception{
        FileInputStream fi=new FileInputStream(inPath);
        InputStreamReader in=new InputStreamReader(fi,"UTF-8");
        FileOutputStream fo=new FileOutputStream(outPath);
        OutputStreamWriter out=new OutputStreamWriter(fo,"UTF-8");
        char[] ch=new char[1024];
        int len=0;
        while((len=in.read(ch))!=-1){
            out.write(ch,0,len);
            out.flush();
        }
        out.close();
        fo.close();
        in.close();
        fi.close();
}

五、标准输入输出流

读取键盘输入的内容并且写到文件中

public void writerTxt()throws Exception{
        //创建一个接收键盘输入数据的输入流
        InputStreamReader is=new InputStreamReader(System.in);
        //把输入流放到缓冲流里
        BufferedReader br=new BufferedReader(is);

        BufferedWriter bw=new BufferedWriter(new FileWriter("D:/abc/a/dd3.txt"));//要写入的文件
        String line="";//定义一个临时接收数据的字符串

        while((line=br.readLine())!=null){
            //读取的每一行都写到指定的TXT文件中
            if(line.equals("over"))
            {
                break;
            }
            bw.write(line);
            bw.flush();
        }
        bw.close();
        br.close();
        is.close();
}

六、数据流

数据输出流

//用数据输出流写到文件中的基本数据类型的数据是乱码,不能直接辨认出来,需要数据的输入流来获取
public void testDataOutputStream()throws Exception{
        DataOutputStream out=new DataOutputStream(new FileOutputStream("D:/abc/a/dd6.txt"));
        //out.writeBoolean(true); //布尔类型
        out.writeDouble(1.35d);  //double型
        //out.writeInt(100); //int型
        out.flush();
        out.close();
}

数据输入流

//数据输入流
//用数据输入流读取数据输出流写到文件中的数据时,要保证使用和当时写的数据类型一致的类型方法来读取
//也就是说,如果写的是writeInt(),那么读的时候就得是readInt();
public void testDataInputStrem()throws Exception{
        DataInputStream in=new DataInputStream(new FileInputStream("D:/abc/a/dd6.txt"));
        System.out.println(in.readDouble());
        in.close();
}

以上就是全部内容,希望可以帮到各位小伙伴!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值