输入流、输出流

1、

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

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

2、读取文件

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

// C:\Users\yblh0\Desktop

// 可以读取任意类型的文件,都是 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();
		}

  

 

转载于:https://www.cnblogs.com/niuxiao12---/p/7247856.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值