• Java里的File I/O


    Java的输入流主要由:InputStream和Reader作为基类,把持久化数据读入内存。输出流由OutputStream和Write类作为父类。

    其中读如内存的时候,不可能一下去全读进去,需要一个缓存区,一块一块的读。如果要再把数据复制到其他磁盘,也可以在循环读的时候把数据再写入目标地址。

    Reader抽象类:下有InputStreamReader和BufferedReader(读取字符流文件,自带缓存区,有ReaderLine方法可每次读一行。需要和FileReader类一起使用);

    InputStreamReader下有FileReader类(读取字符流数据)

    InputStream抽象类有子类:FileInputStream类(读取字节流数据);FileInputStream类有子类:DataInputStream(用来读取二进制数据)类。

    Writer抽象类:下有OutputStreamWriter和【BufferedWriter子类(需要和FileWriter类仪器连用)】;(和Reader类的子类相对应,写入的时候需要标识偏移量从哪开始读,读到哪里。OutputStream读到-1停止,BufferedWriter读到null停止)。

    OutputStreamWriter类:下有FileWriter类。

    OutputStream抽象类下有子类:FileOutputStream类(用来写入字节流数据);FileOutputStream类下有子类:DataOutputStream类(用来写入二进制数据);

    几个案列加深记忆:

    1:File的常用方法

    File file=new File("D:\我的青春谁做主.txt");
    if(!file.exists()){
    try {
    file.createNewFile();
    } catch (IOException e) {
    // TODO 自动生成的 catch 块
    e.printStackTrace();
    }
    }
    if(file.isFile()){
    System.out.println("名称:"+file.getName());
    System.out.println("绝对路径:"+file.getAbsolutePath());
    System.out.println("文件大小:"+file.length());
    }
    if( file.delete()){
    System.out.println("删除文件成功!");
    }

    2:用读取字节流的方法读取文件并把读到数据写入别的磁盘下

    FileInputStream fis=null;//
    FileOutputStream fos=null;
    try {
    fis=new FileInputStream("D:\我的青春谁做主.txt");
    fos=new FileOutputStream("E:\lianxi.txt");
    byte[] bytes=new byte[1024];
    StringBuffer sb=new StringBuffer();
    int size=0;
    while((size=fis.read(bytes))!=-1){
    String str=new String(bytes);
    sb.append(str);
    fos.write(bytes, 0, size);
    }
    System.out.println(sb);
    } catch (Exception e) {
    // TODO 自动生成的 catch 块
    e.printStackTrace();
    }finally{
    try {
    fis.close();
    fos.close();
    } catch (Exception e2) {
    // TODO: handle exception
    }
    }

    3:用读取字符流的方法读取文件并把读到数据写入别的磁盘下

    BufferedReader br=null;
    BufferedWriter bw=null;
    try {

    FileReader fr=new FileReader("D:\pet.template.txt");
    FileWriter fw=new FileWriter("E:\pet.txt");
    br=new BufferedReader(fr);
    bw=new BufferedWriter(fw);
    String line=null;
    StringBuffer sb=new StringBuffer();
    while((line=br.readLine())!=null){
    sb.append(line);
    }
    System.out.println("替换前:"+sb);
    String newStr=sb.toString().replace("{name}", "欧欧");
    newStr=newStr.replace("{type}", "狗狗");
    newStr=newStr.replace("{master}", "李伟");
    fw.write(newStr);
    } catch (Exception e) {
    // TODO 自动生成的 catch 块
    e.printStackTrace();
    }finally{
    try {
    if(br!=null){
    br.close();
    }
    if(bw!=null){
    bw.close();
    }
    } catch (Exception e2) {
    e2.printStackTrace();
    }
    }

    4:使用DataInputStream类和DataOutputStream类读取写入二进制文件(如:图片)

    Scanner input=new Scanner(System.in);
    System.out.println("请输入要复制的文件绝对路径:");
    String filepath=input.nextLine();
    System.out.println("请输入复制后的磁盘路径:");
    String targetpath=input.nextLine();
    DataInputStream dis=null;
    DataOutputStream dos=null;
    try {
    FileInputStream fis=new FileInputStream(filepath);
    dis=new DataInputStream(fis);
    FileOutputStream fos=new FileOutputStream(targetpath);
    dos=new DataOutputStream(fos);
    byte[] bytes=new byte[1024];
    StringBuffer sb=new StringBuffer();
    int size=0;
    while((size=dis.read(bytes))!=-1){
    String str=new String(bytes);
    sb.append(str);
    dos.write(bytes);
    }
    System.out.println("复制成功!");
    } catch (Exception e) {
    // TODO: handle exception
    }finally{
    try {
    if(dis!=null){
    dis.close();
    }
    if(dos!=null){
    dos.close();
    }
    } catch (Exception e2) {
    // TODO: handle exception
    }
    }

  • 相关阅读:
    PHP Session 变量
    PHP Cookie是什么
    PHP 文件上传
    PHP 文件处理
    PHP include 和 require 语句
    Mac pycharm专业版安装以及破解方法
    bzoj3946: 无聊的游戏
    win10 uwp 九幽图床
    win10 uwp 九幽图床
    git无法pull仓库refusing to merge unrelated histories
  • 原文地址:https://www.cnblogs.com/345214483-qq/p/3822176.html
Copyright © 2020-2023  润新知