zoukankan      html  css  js  c++  java
  • 输入流、输出流

    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();
    		}
    

      

  • 相关阅读:
    redis client 2.0.0 pipeline 的list的rpop bug
    Python解释器镜像源修改
    全连接层
    测试(张量)- 实战
    数据加载
    Python之微信-微信好友头像合成
    高阶操作
    MYSQL 查询缓存
    SQL Server 查看指定表上的索引
    MYSQL 查看表上索引的 1 方法
  • 原文地址:https://www.cnblogs.com/niuxiao12---/p/7247856.html
Copyright © 2011-2022 走看看