• 输入流、输出流


    1、

    // mkdir() 必须保证路径上的父文件夹都存在
    System.out.println("创建是否成功:" + f1.mkdir());

    // mkdirs() 创建路径上所有不存在的文件夹
    System.out.println("创建是否成功:" + f1.mkdirs());

    2、读取文件

    try {
    FileInputStream fis = new FileInputStream("D:/JavaTest/1.txt");

    // C:Usersyblh0Desktop

    // 可以读取任意类型的文件,都是 0 和 1 组成的
    // FileInputStream fis = new FileInputStream("C:/Users/yblh0/Desktop/1.ppt");

    byte[] bytes = new byte[5]; // 1024,2048

    int length = 0;

    // 1. 把 read 方法的返回值赋值给 length,length 就是每次读取数据的长度了
    // 2. 再使用 length 和 -1 进行比较
    while ((length = fis.read(bytes)) != -1) {

    // offset The index(索引) of the first byte to decode(转换)
    // length The number of bytes to decode
    // 从哪儿开始转换
    // 转换多长的数据
    String text = new String(bytes, 0, length);

    System.out.println(text);
    }

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

      3、复制文件

    		// 复制文件
    		try {
    			FileInputStream fis = new FileInputStream("D:/JavaTest/1.txt");
    			
    			// 自动创建不存在的文件
    			FileOutputStream fos = new FileOutputStream("D:/JavaTest/3.txt", true);
    			
    			byte[] bytes = new byte[5];
    			
    			int length = 0;
    			
    			while ((length = fis.read(bytes)) != -1) {
    				
    				fos.write(bytes, 0, length);
    			}
    			
    			// 先打开的后关闭,后打开的先关闭
    			fos.close();
    			fis.close();
    			
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    

      

  • 相关阅读:
    张建(北京工业大学软件学院副教授)_百度百科
    孤独患者马天宇:独自生存我也会适应得很好_红人访_腾讯娱乐_腾讯网
    zz
    java~springboot~ibatis Invalid bound statement (not found)原因
    数据库~dotnetcore连接Mysql插入中文失败
    k8s~术语解释
    springboot~让我习惯了TDD的开发模式
    POJ 2498 Martian Mining
    Java中的DeskTop类
    我和ip_conntrack不得不说的一些事
  • 原文地址:https://www.cnblogs.com/niuxiao12---/p/7247856.html
Copyright © 2020-2023  润新知