Java IO

IO流结构图

在这里插入图片描述

File

创建功能:
public boolean createNewFile () :创建文件如果存在这样的文件,就不创建了
public boolean mkdir () :创建文件夹如果存在这样的文件夹,就不创建了
public boolean mkdirs () :创建文件夹,如果父文件夹不存在,会帮你创建出来

public class FileDemo {
	public static void main(String[] args) throws IOException {
		// 需求:在e盘目录下创建一个文件夹demo
		File file = new File("e:\\demo");
		System.out.println("mkdir:" + file.mkdir());

		// 需求:在e盘目录demo下创建一个文件a.txt
		File file2 = new File("e:\\demo\\a.txt");
		System.out.println("createNewFile:" + file2.createNewFile());

		// 需求:在e盘目录test下创建一个文件b.txt
		// Exception in thread "main" java.io.IOException: 系统找不到指定的路径。
		// 注意:要想在某个目录下创建内容,该目录首先必须存在。
		// File file3 = new File("e:\\test\\b.txt");
		// System.out.println("createNewFile:" + file3.createNewFile());

		// 需求:在e盘目录test下创建aaa目录
		// File file4 = new File("e:\\test\\aaa");
		// System.out.println("mkdir:" + file4.mkdir());

		// File file5 = new File("e:\\test");
		// File file6 = new File("e:\\test\\aaa");
		// System.out.println("mkdir:" + file5.mkdir());
		// System.out.println("mkdir:" + file6.mkdir());

		// 简单方法
		File file7 = new File("e:\\aaa\\bbb\\ccc\\ddd");
		System.out.println("mkdirs:" + file7.mkdirs());

	}
}

册除功能:public boolean delete ()

public class FileDemo {
	public static void main(String[] args) throws IOException {
		// 创建文件
		// File file = new File("e:\\a.txt");
		// System.out.println("createNewFile:" + file.createNewFile());

		// 默认当前项目下
		File file = new File("a.txt");
		System.out.println("createNewFile:" + file.createNewFile());

		// 创建多级
		File file2 = new File("aaa\\bbb\\ccc");
		System.out.println("mkdirs:" + file2.mkdirs());

		// 删除功能:删除a.txt这个文件
		File file3 = new File("a.txt");
		System.out.println("delete:" + file3.delete());

		// 删除功能:删除ccc这个文件夹
		File file4 = new File("aaa\\bbb\\ccc");
		System.out.println("delete:" + file4.delete());

		// 删除功能:删除aaa文件夹
		// File file5 = new File("aaa");
		// System.out.println("delete:" + file5.delete());

		File file6 = new File("aaa\\bbb");
		File file7 = new File("aaa");
		System.out.println("delete:" + file6.delete());
		System.out.println("delete:" + file7.delete());
	}
}

重命名功能:public boolean renameTo (File dest)


重命名功能:public boolean renameTo(File dest)
如果路径名相同,就是改名。
如果路径名不同,就是改名并剪切。
路径以盘符开始:绝对路径	c:\\a.txt
路径不以盘符开始:相对路径	a.txt

public class FileDemo {
	public static void main(String[] args) {
		// 创建一个文件对象
		// File file = new File("a.jpg");
		// // 需求:我要修改这个文件的名称为"东方不败.jpg"
		// File newFile = new File("b.jpg");
		// System.out.println("renameTo:" + file.renameTo(newFile));

		File file2 = new File("b.jpg");
		File newFile2 = new File("e:\\a.jpg");
		System.out.println("renameTo:" + file2.renameTo(newFile2));
	}
}

判断功能:
public boolean isDirectory() :判断是否是目录
public boolean isFile () :判断是否是文件
public boolean exists () :判断是否存在
public boolean canRead() :判断是否可读
public boolean canWrite () :判断是否可写
public boolean isHidden () :判断是否隐藏

public class FileDemo {
	public static void main(String[] args) {
		// 创建文件对象
		File file = new File("a.txt");

		System.out.println("isDirectory:" + file.isDirectory());// false
		System.out.println("isFile:" + file.isFile());// true
		System.out.println("exists:" + file.exists());// true
		System.out.println("canRead:" + file.canRead());// true
		System.out.println("canWrite:" + file.canWrite());// true
		System.out.println("isHidden:" + file.isHidden());// false
	}
}

获取功能:
public String getAbsolutePath() :获取绝对路径
public String getPath () :获取相对路径
public String getName () :获取名称
public long length() :获取长度。字节数
public long lastModified() :获取最后一次的修改时间,亳秒值

public class FileDemo {
	public static void main(String[] args) {
		// 创建文件对象
		File file = new File("demo\\test.txt");

		System.out.println("getAbsolutePath:" + file.getAbsolutePath());
		System.out.println("getPath:" + file.getPath());
		System.out.println("getName:" + file.getName());
		System.out.println("length:" + file.length());
		System.out.println("lastModified:" + file.lastModified());

		// 1416471971031
		Date d = new Date(1416471971031L);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String s = sdf.format(d);
		System.out.println(s);
	}
}

获取功能:
public String[] list () :获取指定目录下的所有文件或者文件夹的名称数组
public File[] listFiles () :获取指定目录下的所有文件或者文件夹的Fil e数组

public class FileDemo {
	public static void main(String[] args) {
		// 指定一个目录
		File file = new File("e:\\");

		// public String[] list():获取指定目录下的所有文件或者文件夹的名称数组
		String[] strArray = file.list();
		for (String s : strArray) {
			System.out.println(s);
		}
		System.out.println("------------");

		// public File[] listFiles():获取指定目录下的所有文件或者文件夹的File数组
		File[] fileArray = file.listFiles();
		for (File f : fileArray) {
			System.out.println(f.getName());
		}
	}
}

递归:
递归求阶乘

public class DiGuiDemo {
	public static void main(String[] args) {
		int jc = 1;
		for (int x = 2; x <= 5; x++) {
			jc *= x;
		}
		System.out.println("5的阶乘是:" + jc);
		
		System.out.println("5的阶乘是:"+jieCheng(5));
	}
	
	/*
	 * 做递归要写一个方法:
	 * 		返回值类型:int
	 * 		参数列表:int n
	 * 出口条件:
	 * 		if(n == 1) {return 1;}
	 * 规律:
	 * 		if(n != 1) {return n*方法名(n-1);}
	 */
	public static int jieCheng(int n){
		if(n==1){
			return 1;
		}else {
			return n*jieCheng(n-1);
		}
	}
}

有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问第二十个月的兔子对数为多少?
分析:找规律
兔子对数
第一个月: 1
第二个月: 1
第三个月: 2
第四个月: 3
第五个月: 5
第六个月: 8

规则:
A:从第三项开始,每一项是前两项之和
B:而且说明前两项是已知的

A如何实现这个程序呢?
B:变量的变化实现
C:递归实现

假如相邻的两个月的兔子对数是a,b
第一个相邻的数据:a=1,b=1
第三个相邻的数据:a=2,b=3
第四个相邻的数据:a=3,b=5
看到了:下一次的a是以前的b,下一次是以前的a+b

public class DiGuiDemo2 {
	public static void main(String[] args) {
		// 定义一个数组
		int[] arr = new int[20];
		arr[0] = 1;
		arr[1] = 1;
		// arr[2] = arr[0] + arr[1];
		// arr[3] = arr[1] + arr[2];
		// ...
		for (int x = 2; x < arr.length; x++) {
			arr[x] = arr[x - 2] + arr[x - 1];
		}
		System.out.println(arr[19]);// 6765
		System.out.println("----------------");

		int a = 1;
		int b = 1;
		for (int x = 0; x < 18; x++) {
			// 临时变量存储上一次的a
			int temp = a;
			a = b;
			b = temp + b;
		}
		System.out.println(b);
		System.out.println("----------------");

		System.out.println(fib(20));
	}

	/*
	 * 方法: 返回值类型:int 参数列表:int n 出口条件: 第一个月是1,第二个月是1 规律: 从第三个月开始,每一个月是前两个月之和
	 */
	public static int fib(int n) {
		if (n == 1 || n == 2) {
			return 1;
		} else {
			return fib(n - 1) + fib(n - 2);
		}
	}
}

字节流

FileOutputStream:
写出的三种方式:
public void write (int, b) :写一个字节
public void write (byte[] b) :写一个字节数组
public void write (byte[] b,int off, int len) :写一个字节数组的-部分

public class Test {
	public static void main(String[] args) throws Exception {
		FileOutputStream f = new FileOutputStream("a.txt");
		// 第一种
		f.write(97);// a
		// 第二种
		byte[] bys = { 67, 68, 69, 70, 71 };
		f.write(bys);// C D E F G
		// 第三种
		f.write(bys, 1, 4);// D E F G
		f.close();
	}
}

写出的异常处理:

public class Test {
	public static void main(String[] args) {
		FileOutputStream f = null;
		try {
			f = new FileOutputStream("b.txt");

			f.write("hello".getBytes());
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		} finally {
			try {
				f.close();
			} catch (IOException e) {

				e.printStackTrace();
			}
		}

	}
}

字节流复制文本文件:

//将b复制到c
public class Test {
	public static void main(String[] args) throws Exception {
		FileOutputStream f = new FileOutputStream("c.txt");
		FileInputStream f1 = new FileInputStream("b.txt");
		int a = 0;
		while ((a = f1.read()) != -1) {
			f.write(a);
		}
		f1.close();
		f.close();
	}
}

一次读取一个字节数组

public class Test {
	public static void main(String[] args) throws Exception {

		FileInputStream f1 = new FileInputStream("b.txt");
	      byte[] bys = new byte[1024];
	      int len=0;
	      while((len=f1.read(bys))!=-1) {
	    	  System.out.println(new String(bys,0,len));
	      }
	      f1.close();
	}
}

高效缓冲流


BufferedOutputStream写出数据

public class Test {
	public static void main(String[] args) throws Exception {
     //为什么不传递一一个具体的文件或者文件路径,而是传递-个OutputStream对象呢?
//		原因很简单,宇节缓冲区流仅仅提供缓冲区,为高效而设计的。但是呢,真正的读写操作还得靠基本的流对象实现。

		FileOutputStream f = new FileOutputStream("d.txt");
		BufferedOutputStream b = new BufferedOutputStream(f);
		b.write("我爱你中国".getBytes());
		b.close();
	}
}

BufferedInputStream读入数据

public class Test {
	public static void main(String[] args) throws Exception {
		BufferedInputStream f = new BufferedInputStream(new FileInputStream("b.txt"));

		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = f.read(bys)) != -1) {
			System.out.println(new String(bys, 0, len));
		}
		f.close();
	}
}

字节流四种方式读写:
字节流四种方式复制文件:
基本字节流一次读写一个字节:
基本字节流一次读写一个字节数组:
高效字节流一次读写一个字节:
高效字节流-次读写一个字节数组:

public class Test {
	public static void main(String[] args) throws Exception {
		// 基本字节流一次读写一个字节
		FileInputStream f1 = new FileInputStream("b.txt");
		FileOutputStream f2 = new FileOutputStream("e.txt");
		int len = 0;
		while ((len = f1.read()) != -1) {
			f2.write(len);
		}
		f1.close();
		f2.close();
		// 基本字节流一次读写一个字节数组
		FileInputStream f3 = new FileInputStream("b.txt");
		FileOutputStream f4 = new FileOutputStream("f.txt");

		byte[] bys = new byte[1024];
		int len1 = 0;
		while ((len1 = f3.read(bys)) != -1) {
			f4.write(bys, 0, len1);
		}
		f3.close();
		f4.close();
		// 高效字节流一次读写一个字节
		BufferedInputStream f5 = new BufferedInputStream(new FileInputStream("b.txt"));
		BufferedOutputStream f6 = new BufferedOutputStream(new FileOutputStream("g.txt"));
		int len2 = 0;
		while ((len2 = f5.read()) != -1) {
			f6.write(len2);
		}
		f5.close();
		f6.close();
		// 高效字节流一次读写一个字节数组
		BufferedInputStream f7 = new BufferedInputStream(new FileInputStream("b.txt"));
		BufferedOutputStream f8 = new BufferedOutputStream(new FileOutputStream("h.txt"));
		byte[] bys1 = new byte[1024];
		int len3 = 0;
		while ((len3 = f7.read(bys1)) != -1) {
			f8.write(bys1, 0, len3);
		}
		f7.close();
		f8.close();
	}
}

转换流


OutputStreamWriter:

public class Test {
	public static void main(String[] args) throws Exception {

		FileOutputStream f = new FileOutputStream("i.txt");
		OutputStreamWriter o = new OutputStreamWriter(f);
		o.write("我们都有一个家,名字叫中国");
		o.close();
	}
}

InputStreamReader:

public class Test {
	public static void main(String[] args) throws Exception {
		InputStreamReader i = new InputStreamReader(new FileInputStream("b.txt"));
		int ch = 0;
		while ((ch = i.read()) != -1) {
			System.out.println((char) ch);//强转成字符
		}
		i.close();
	}
}

字符流

OutputStreamWriter:
5种写出:
OutputStreamWriter的方法:
public void write(int c):写一个字符
public void write(char[] cbuf):写一个字符数组
public void write(char[] cbuf,int off,int len):写一个字符数组的一部分
public void write(String str):写一个字符串
public void write(String str,int off,int len):写一个字符串的一部分

面试题:close()和flush()的区别?
A:close()关闭流对象,但是先刷新一次缓冲区。关闭之后,流对象不可以继续再使用了。
B:flush()仅仅刷新缓冲区,刷新之后,流对象还可以继续使用。


public class OutputStreamWriterDemo {
	public static void main(String[] args) throws IOException {
		// 创建对象
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
				"osw2.txt"));

		// 写数据
		// public void write(int c):写一个字符
		// osw.write('a');
		// osw.write(97);
		// 为什么数据没有进去呢?
		// 原因是:字符 = 2字节
		// 文件中数据存储的基本单位是字节。
		// void flush()

		// public void write(char[] cbuf):写一个字符数组
		// char[] chs = {'a','b','c','d','e'};
		// osw.write(chs);

		// public void write(char[] cbuf,int off,int len):写一个字符数组的一部分
		// osw.write(chs,1,3);

		// public void write(String str):写一个字符串
		// osw.write("我爱你中国");

		// public void write(String str,int off,int len):写一个字符串的一部分
		osw.write("我爱你中国", 2, 3);

		// 刷新缓冲区
		osw.flush()
		// 释放资源
		osw.close();
	}
}

InputStreamReader:
2种读入:

public class Test {
	public static void main(String[] args) throws Exception {
		InputStreamReader i = new InputStreamReader(new FileInputStream("b.txt"));
		// 一次一个字符
//         int a=0;
//         while((a=i.read())!=-1) {
//       System.out.println((char)a);
//	}
		// 一次一个字符数组
		char[] ch = new char[1024];
		int len = 0;
		while ((len = i.read(ch)) != -1) {
			System.out.println(new String(ch, 0, len));
		}
		i.close();
	}
}

字符流简化写法复制文本:

public class Test {
	public static void main(String[] args) throws Exception {
		FileReader f = new FileReader("b.txt");
		FileWriter f1 = new FileWriter("k.txt");
		// 一次一个字符
//        int len=0;
//        while((len=f.read())!=-1) {
//        	f1.write(len);
//        }
//        f.close();
//        f1.close();
		// 一次一个字符数组
		char[] ch = new char[1024];
		int len1 = 0;
		while ((len1 = f.read(ch)) != -1) {
			f1.write(ch, 0, len1);
		}
		f.close();
		f1.close();
	}
}

字符缓冲流复制文本1:

public class Test {
	public static void main(String[] args) throws Exception {
		BufferedReader b = new BufferedReader(new FileReader("b.txt"));
		BufferedWriter b1 = new BufferedWriter(new FileWriter("m.txt"));

		char[] ch = new char[1024];
		int len = 0;
		while ((len = b.read(ch)) != -1) {
			b1.write(ch, 0, len);
			b1.flush();
		}
		b.close();
		b1.close();
	}
}

字符缓冲流复制文本2:

public class Test {
	public static void main(String[] args) throws Exception {
		BufferedReader b = new BufferedReader(new FileReader("b.txt"));
		BufferedWriter b1 = new BufferedWriter(new FileWriter("x.txt"));
		// 读写数据
		String s = null;
		while ((s = b.readLine()) != null) {
			b1.write(s);// 写数据
			b1.newLine();// 换行
			b1.flush();// 刷新缓冲区
		}
		// 释放资源
		b.close();
		b1.close();
	}
}

总结:

复制文本5种方式:

public class Test {
	public static void main(String[] args) throws IOException {
		String srcString = "c:\\a.txt";
		String destString = "d:\\b.txt";
		// method1(srcString, destString);
		// method2(srcString, destString);
		// method3(srcString, destString);
		// method4(srcString, destString);
		method5(srcString, destString);
	}

	// 字符缓冲流一次读写一个字符串
	private static void method5(String srcString, String destString) throws IOException {
		BufferedReader br = new BufferedReader(new FileReader(srcString));
		BufferedWriter bw = new BufferedWriter(new FileWriter(destString));

		String line = null;
		while ((line = br.readLine()) != null) {
			bw.write(line);
			bw.newLine();
			bw.flush();
		}

		bw.close();
		br.close();
	}

	// 字符缓冲流一次读写一个字符数组
	private static void method4(String srcString, String destString) throws IOException {
		BufferedReader br = new BufferedReader(new FileReader(srcString));
		BufferedWriter bw = new BufferedWriter(new FileWriter(destString));

		char[] chs = new char[1024];
		int len = 0;
		while ((len = br.read(chs)) != -1) {
			bw.write(chs, 0, len);
		}

		bw.close();
		br.close();
	}

	// 字符缓冲流一次读写一个字符
	private static void method3(String srcString, String destString) throws IOException {
		BufferedReader br = new BufferedReader(new FileReader(srcString));
		BufferedWriter bw = new BufferedWriter(new FileWriter(destString));

		int ch = 0;
		while ((ch = br.read()) != -1) {
			bw.write(ch);
		}

		bw.close();
		br.close();
	}

	// 基本字符流一次读写一个字符数组
	private static void method2(String srcString, String destString) throws IOException {
		FileReader fr = new FileReader(srcString);
		FileWriter fw = new FileWriter(destString);

		char[] chs = new char[1024];
		int len = 0;
		while ((len = fr.read(chs)) != -1) {
			fw.write(chs, 0, len);
		}

		fw.close();
		fr.close();
	}

	// 基本字符流一次读写一个字符
	private static void method1(String srcString, String destString) throws IOException {
		FileReader fr = new FileReader(srcString);
		FileWriter fw = new FileWriter(destString);

		int ch = 0;
		while ((ch = fr.read()) != -1) {
			fw.write(ch);
		}

		fw.close();
		fr.close();
	}
}

复制图片4种方式:

public class Test {
	public static void main(String[] args) throws IOException {
		// 使用字符串作为路径
		// String srcString = "c:\\a.jpg";
		// String destString = "d:\\b.jpg";
		// 使用File对象做为参数
		File srcFile = new File("c:\\a.jpg");
		File destFile = new File("d:\\b.jpg");

		// method1(srcFile, destFile);
		// method2(srcFile, destFile);
		// method3(srcFile, destFile);
		method4(srcFile, destFile);
	}

	// 字节缓冲流一次读写一个字节数组
	private static void method4(File srcFile, File destFile) throws IOException {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));

		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = bis.read(bys)) != -1) {
			bos.write(bys, 0, len);
		}

		bos.close();
		bis.close();
	}

	// 字节缓冲流一次读写一个字节
	private static void method3(File srcFile, File destFile) throws IOException {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));

		int by = 0;
		while ((by = bis.read()) != -1) {
			bos.write(by);
		}

		bos.close();
		bis.close();
	}

	// 基本字节流一次读写一个字节数组
	private static void method2(File srcFile, File destFile) throws IOException {
		FileInputStream fis = new FileInputStream(srcFile);
		FileOutputStream fos = new FileOutputStream(destFile);

		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = fis.read(bys)) != -1) {
			fos.write(bys, 0, len);
		}

		fos.close();
		fis.close();
	}

	// 基本字节流一次读写一个字节
	private static void method1(File srcFile, File destFile) throws IOException {
		FileInputStream fis = new FileInputStream(srcFile);
		FileOutputStream fos = new FileOutputStream(destFile);

		int by = 0;
		while ((by = fis.read()) != -1) {
			fos.write(by);
		}

		fos.close();
		fis.close();
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值