码迷,mamicode.com
首页 > 其他好文 > 详细

输入输出

时间:2017-07-27 23:39:35      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:try   输出   nbsp   ring   dir   div   pre   fileinput   文件夹   

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

  

 

输入输出

标签:try   输出   nbsp   ring   dir   div   pre   fileinput   文件夹   

原文地址:http://www.cnblogs.com/niuxiao12---/p/7247856.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!