IO流

IO流是指将数据输入或者输出In&Out。

IO流分类:

IO流API分类1:

IO流API分类2:

1.创建文件/文件夹

public class _02FileConstraction {
	/*File(File parent, String child)  根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。 
	  File(String pathname)  通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。 
	  File(String parent, String child) 根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例 
*/
	public static void main(String[] args) throws IOException {
		File file = new File("G:/C");
		System.out.println(file);
		file.mkdir();
		
		File file2 = new File(file,"/A");
		file2.mkdir();
		System.out.println(file2);
		
		File file3 = new File("G:/A/C/sy.txt");
		file3.mkdir();
		System.out.println(file3);
	}
}

2.文件过滤器

public class FileFilter2 {

	public static void main(String[] args) {
	//使用别人已经定义好的过滤器
		File file = new File("G:/C/A/D/E/O/P");
		String[] list = file.list(new A());
		System.out.println(Arrays.toString(list));
	}

}

3.一次性删除多重--多个文件

public class DeleteFile {

	public static void main(String[] args) {
		//单单使用delete不能删除掉一个目录中有多个文件或者文件夹的
		File s = new File("G:/C/A/D/E/O/P");
		boolean delete = s.delete();
		System.out.println(delete);
	}
	public void de(File f){
		//判断是否是文件夹
		if (f.isDirectory()) {
			//将f文件夹中的文件或者文件夹都装成一个file数组
			File[] l = f.listFiles();
			//判断Bug
			System.out.println(Arrays.toString(l));
			//遍历File数组
			for (File file : l) {
				//每次遍历都会判断是否是文件夹,如果是文件夹再执行此操作,如果是文件就直接删除
				de(file);//调自己,再判断是否是文件夹,如果不是文件夹那么就直接删除
				//每次调方法都是独立的一次
			}
		}
		f.delete();//递归出口。当每次是文件的时候就会执行
	}
}

4.FileInputStream与FileOutPutStream(字节IO)

public class FileInputStreamConstraction {
	public static void main(String[] args) throws Exception {
		//创建字节输入流
		FileInputStream fi1 = new FileInputStream("G:/作业/Day23_何杰/code-练习/Day23/123");
		//直接阅读完文件中所有的值
		byte[] a = new byte[4];
		while (true) {
			int read = fi1.read(a,0,3);//0+3必须要小于数组的长度(访问的是索引值),否则索引越界
			if (read == -1) {
				break;
			}
			System.out.print(new String(a,0,read));
		}
		fi1.close();
	}
}
public class FileOutputSreamDemo {
	public static void main(String[] args) throws Exception {
		FileOutputStream f = new FileOutputStream("G:/作业/Day23_何杰/code-练习/Day23
		byte[] bytes = "jhsadhasuif".getBytes();
		f.write(bytes, 3, 3);
		f.close();
	}
}

5.FileReader与FileWriter(字符IO)

public class _01FIleRederDemo {
//若read方法无参,那么就返回此值的ASCII编码,有参那么就返回几个数字的个数
	public static void main(String[] args) throws Exception {
		FileReader f = new FileReader("G:/作业/Day23_何杰/code-练习/Day23/123");
		char[] a = new char[3];
		while (true) {
			int read = f.read(a, 0, 2);//最后一个数字限制了每次传出的值的个数
			if (read == -1) {
				break;
			}
			System.out.println(new String(a,0,read));
		}
		f.close();
	}
}
public class FileWriterDemo {
        public static void main(String[] args) throws Exception {
                f.write(88);//存入ASCII码值
		f.close();
	}
}

6.将一个文件的内容拷贝到另一个文件中去

public class FileWriterDemo {
	public static void main(String[] args) throws Exception {
		//FileWriter f = new FileWriter("G:/作业/Day23_何杰/code-练习/Day23/789",true);
		FileInputStream f1 = new FileInputStream("G:/Java20190220/eclipse代码/Day23_IO2/1.jpg");
		FileOutputStream f2 = new FileOutputStream("G:/Java20190220/eclipse代码/Day23_IO2/22.jpg");
		while (true) {
			int read = f1.read();
			if (read == -1) {
				break;
			}
			f2.write(read);
		}
		f2.close();
		f1.close();
	}
}

7.转换流--输入转换流InputStreamReader--输出转换流OutputStreamWriter

public class InputStreamRreaderDemo {
	public static void main(String[] args) throws Exception {
		//FileInputStream将路径存储,多态写法
		InputStream io = new FileInputStream("G:/作业/Day23_何杰/code-练习/Day23/123");
		//将路径放入,此时是将字节输入流转换为字符输入流(可以得到字符了)
		InputStreamReader in = new InputStreamReader(io);	
		while (true) {
			//使用转换流中的方法,
			int read = in.read();
			if (read == -1) {
				break;
			}
			System.out.print((char)read);
		}
		in.close();
		io.close();
	}
}
public class OutputStreamWriterDemo {
//转换流,OutputStreamWriter
	public static void main(String[] args) throws Exception {
		OutputStream f1 = new FileOutputStream("G:/作业/Day23_何杰/code-练习/Day23/852",true);
		OutputStreamWriter f2 = new OutputStreamWriter(f1);
		f2.write("撒娇看得见啊数据库的");
		f2.close();
	}
}

 

转载于:https://my.oschina.net/u/4083606/blog/3029864

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值