黑马程序员——java--IO

本文详细介绍了Java中文件流的使用方法,包括字节流与字符流的原理、应用及常见API的使用,旨在帮助开发者高效进行文件读写操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

——- android培训java培训、期待与您交流! ———-

IO流:

如何对文件进行读写?IO(Input/Output):输入/输出    流:Stream

自来水厂   家里    水流   管道

Java中的文件流分为“输入流”和“输出流”,简称IO流,是对文件输入(写)输出(读)的抽象。

Java.io包提供了很多接口和类支持对各种文件的读写,负责架个管道并把数据扔到管道中或从管道中得到数据

字节流和字符流:

字节流,一次读写一个字节(8位),适用于图片、音视频等文件,也包括文本文件(注意编码要跟操作系统编码保持一致)
字符流,一次读写两个字节(16位),适用于文本文件、Word文件等unicode字符
字节流的抽象基类:InputStream和OutputStream
字符流的抽象基类:Reader和Writer

字节流的抽象基类:InputStream和OutputStream
    常用子类:FileInputStream和FileOutputStream
字符流的抽象基类:Reader和Writer
  常用子类:FileReader和FileWriter

字节输出流:

把二进制数据写到“输出流”中

FileOutputStream类的常用方法:
public FileOutputStream(File file)    文件不存在会自动创建
public FileOutputStream(String name)    文件不存在会自动创建
public FileOutputStream(File file,boolean append)
public FileOutputStream(String name,boolean append)
public void write(int b)     参数是ASCII表中的码值,不是普通数字
public void write(byte[] b)   String对象的getBytes方法
public void write(byte[] b, int off, int len)
public void close()    关闭流,释放资源

字节输入流:

从“输入流”中读取二进制数据

FileInputStream类常用方法:
public FileInputStream(File file)   文件不存在不会自动创建
public FileInputStream(String path)
public int read()
public int read(byte[] b)      new String(byte[] b)
public void close()
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class Test05 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("ang.gif");
            fos = new FileOutputStream("ang_new02.gif");
            byte ch[] = new byte[1024];
            int len =0;
            while ((len=fis.read(ch))!=-1) {
                fos.write(ch,0,len);
                }
        } catch (Exception e) {
            throw new RuntimeException("运行异常");

        }finally{
            if (fis!=null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
            if (fos!=null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }


        }

    }

}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class Test06Copy {

    /**
     * @param args
     */
    public static void main(String[] args) {
        BufferedOutputStream bos = null;
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream("ang.gif"));
            bos = new BufferedOutputStream(new FileOutputStream("anf02.gif"));
            int len = 0;
            try {
                while ((len=bis.read())!=-1) {
                    bos.write(len);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if (bis!=null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
            if (bos!=null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }

}

字符输出流:

把字符数据写到“输出流”中

FileWriter类的常用方法
public FileWriter(File file)
public FileWriter(String fileName)
public FileWriter(File file,boolean append)
public FileWriter(String fileName,boolean append)
public void write(String str)
public void write(char[] cbuf)
public void write(char[] b,int off, int len)
public void flush()     //清缓冲区,强制写入,后面讲
public void close()

字符输入流:

从“输入流”中读取字符数据

FileReader类的常用方法:
public FileReader(File file)文件不存在不会自动创建

public FileReader(String path)
public int read()
public int read(char[] cbuf)
public void close()

字符流缓冲区:

缓冲区就是内存中的一块儿区域,读写时可以先把数据放到此处,满了之后才进行真正的读写,避免了频繁读写硬盘,提高效率。

字符流默认缓冲区大小是8K

当使用输出流写入数据时,数据默认会先积攒到缓冲区,满了之后自动写入硬盘;没满就需要人工调用flush()或close()才会写入硬盘


当使用输入流读取数据时,数据默认会尽量先读取到缓冲区,满了之后才再次从硬盘上进行读取,否则直接从缓冲区取数据

注意细节:

文件读写        不是文件夹读写

IO操作必须进行异常处理和资源释放,资源释放应当放到finally代码块中

try-catch最好放到最外层

尽量不使用每次读写一个数据的方法read()  write(int c)

注意两个read方法返回值的不同
int  read()
int  read(byte[] b)或read(char[] c)
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


public class Test03 {
    public static void main(String[] args) {
        copy01();
    }

    public static void copy01(){
        FileReader fr = null;
        FileWriter fw = null;
        try {
            fr = new FileReader("test.txt");
            fw= new FileWriter("test01.txt");
            char[] array = new char[1024];
            int len = 0;
            while ((len=fr.read(array))!=-1) {
                fw.write(array, 0, len);

            }


        } catch (FileNotFoundException e) {
            System.out.println("454665");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("454665");
            e.printStackTrace();
        }finally{
            try {
                if (fw!=null) {
                    fw.close();
                }
            } catch (IOException e) {
                System.out.println("454665");
                e.printStackTrace();
            }

            try {
                if (fr!=null) {
                    fr.close();
                }
            } catch (IOException e) {
                System.out.println("454665");
                e.printStackTrace();
            }

        }
    }


}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


public class Test08 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        BufferedReader br = null;
        BufferedWriter bw = null;


        try {
            br = new BufferedReader(new FileReader("test01.txt"));
            bw = new BufferedWriter(new FileWriter("test03.txt"));
            String line = null;
            while((line= br.readLine())!=null)
            {

                bw.write(line);
                bw.newLine();
                bw.flush();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            br.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            bw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
java.io.BufferedOutputStream    字节缓冲流(写)

public BufferedOutputStream(OutputStream out)   默认大小是8k
public BufferedOutputStream(OutputStream out,  int size)可以自己指定缓冲区大小,单位字节

public void write(int b)  参数是ASCII表中的码值,不是普通数字
public void write(byte[] b)
public void write(byte[] b,int index,int length)
public void flush()  
public void close() 


java.io.BufferedInputStream    字节缓冲流(读)

public BufferedInputStream(InputStream in)  默认缓冲区是8k
public BufferedInputStream(InputStream out,  int size)可以自己指定缓冲区大小,单位字节


public int read()
public int read(byte[] b)
public void close()


java.io.BufferedWriter   字符缓冲流(写)

public BufferedWriter(Writer out)   默认大小是8k
public BufferedWriter(Writer out,  int sz)可以自己指定缓冲区大小,单位字节

public void write(char[] cbuf)
public void write(char[] cbuf,int index,int len)
public void write(String str)
public void newLine()   独有方法,优势是跨平台
public void flush()  
public void close()



java.io.BufferedReader   字符缓冲流(读)

public BufferedReader(Reader in)   默认大小是8k
public BufferedReader(Reader in,  int sz)可以自己指定缓冲区大小,单位字节

public int read()
public int read(char[] cbuf)
public String readLine()    独有方法   碰到换行或回车即一行结束
public void close()
package com.lsj.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Copy {

    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("ang.gif");

            FileOutputStream fos = new FileOutputStream("ang_new.gif");
            byte in[] = new byte[50];
            while (fis.read(in) != -1) {
                fos.write(in);
            }
            fis.close();
            fos.close();
            System.out.println("done");



        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
package com.lsj.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;




public class test {
    public static void main(String[] args) {
    File file = new File("tes.txt");
    try {
        FileInputStream fis = new FileInputStream(file);
        byte input[] = new byte[6];
        fis.read(input);
        String inputString = new String(input, "UTF-8");
        System.out.println(inputString);


    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    }

}


——- android培训java培训、期待与您交流! ———-

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值