IO流基础

IO流的基本API使用

public class Test {
	public static void main(String[] args) {
		// test1();//FileInputStream文件读取流练习
		// test2();//FileOutputStrea文件写出流练习
		// test3();// BufferedInputStream缓冲的文件流练习
		// test4();//BufferedOutputStream缓冲流的练习
		// test5();//综合练习
		// test6();//FileReader练习
		// test7();//FileWriter练习
		// test8();//BufferedReader练习
		// test9();//BuffereWriter的练习
		// test10();// 综合练习
		// test11();//InputStreamReader 和 OutputStreamWriter
		// 转换流的使用,把字节流转换成字符流,主要用于解决编码乱的问题
		//test12();// DataInputStream和DataOutoutStream的练习
		test13();// ObjectOutputStream;
		//test14();
	}
	
	public static void test14() {
		try (FileInputStream fis = new FileInputStream("src/l.dat");
				ObjectInputStream ois = new ObjectInputStream(fis);
		) {
			Person p = (Person) ois.readObject();
			System.out.println(p);
		} catch (Exception e) {
			e.printStackTrace();
		}
	} 

	public static void test13() {
		try (FileOutputStream fos = new FileOutputStream("src/l.dat");
				ObjectOutputStream oos = new ObjectOutputStream(fos);
				FileInputStream fis = new FileInputStream("src/l.dat");
				ObjectInputStream ois = new ObjectInputStream(fis);
				) {
			Person p = new Person("张三","男",18);
			oos.writeObject(p);
			Person p1 = (Person)ois.readObject();
			System.out.println(p1);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void test12() {
		try (FileInputStream fis = new FileInputStream("src/a.txt"); DataInputStream dis = new DataInputStream(fis);) {
			byte[] b = new byte[1024];
			int length = 0;
			int num = dis.readInt();
			System.out.println(num);
			while ((length = dis.read(b)) != -1) {
				String str = new String(b, 0, length);

				System.out.println(str);
			}

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

	public static void test11() {
		try (FileInputStream fis = new FileInputStream("src/a.txt");
				InputStreamReader isr = new InputStreamReader(fis);
				BufferedReader br = new BufferedReader(isr);
				FileOutputStream fos = new FileOutputStream("src/k.txt");
				OutputStreamWriter osw = new OutputStreamWriter(fos);
				BufferedWriter bw = new BufferedWriter(osw);) {
			String str = null;
			while ((str = br.readLine()) != null) {
				bw.write(str);
				bw.newLine();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void test10() {
		try (FileReader fr = new FileReader("src/a.txt");
				BufferedReader br = new BufferedReader(fr);
				FileWriter fw = new FileWriter("src/j.txt", true);
				BufferedWriter bw = new BufferedWriter(fw);) {
			String str = null;
			while ((str = br.readLine()) != null) {
				bw.write(str);
				bw.newLine();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void test9() {
		try (FileWriter fw = new FileWriter("src/i.txt"); BufferedWriter bw = new BufferedWriter(fw);) {
			String str = "对于你我无话可说,可是你不知道什么是满足!";
			bw.write(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void test8() {
		try (FileReader fr = new FileReader("src/a.txt"); BufferedReader br = new BufferedReader(fr);) {
			// br.read();//读取单个字节
			char[] ch = new char[1024];
			int length = 0;
			while ((length = br.read(ch)) != -1) {
				String str = new String(ch, 0, length);
				System.out.println(str);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void test7() {
		try (FileWriter fw = new FileWriter("src/h.txt");) {
			String str = "无论什么时候都要坚强,因为你是家里的男子汉,未来的生活不要把自己陷入被动,无论什么时候你都要招架得住生活任何打击,没有原因,因为你是一个男人,你是家里的希望,你是家里的未来,你不能垮掉,你只能更加的坚强!";
			fw.write(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void test6() {
		try (FileReader fr = new FileReader("src/g.txt");) {
			char[] ch = new char[1024];
			int length = 0;
			while ((length = fr.read(ch)) != -1) {
				String str = new String(ch, 0, length);
				System.out.println(str);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void test5() {
		try (FileInputStream fis = new FileInputStream("C:\\Users\\BW\\Desktop\\初始化.txt");
				BufferedInputStream bis = new BufferedInputStream(fis);
				FileOutputStream fos = new FileOutputStream("C:\\Users\\BW\\Desktop\\b.txt");
				BufferedOutputStream bos = new BufferedOutputStream(fos);) {
			byte[] b = new byte[1024];
			int length = 0;
			while ((length = bis.read(b)) != -1) {
				bos.write(b, 0, length);
			}

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

	public static void test4() {
		try (FileOutputStream fos = new FileOutputStream("src/g.txt");
				BufferedOutputStream bos = new BufferedOutputStream(fos);) {
			String str = "老爸我想你了,我们已经好长时间没见面了.";
			bos.write(str.getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void test3() {
		try (FileInputStream fis = new FileInputStream("src/a.txt");
				BufferedInputStream bis = new BufferedInputStream(fis);) {
			byte[] b = new byte[1024];
			int length = 0;
			while ((length = bis.read(b)) != -1) {
				String str = new String(b, 0, length);
				System.out.println(str);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void test2() {
		try (FileOutputStream fos = new FileOutputStream("src/f.txt");) {
			String str = "这个世界没有谁对不起谁,只是自己不够强大而已,对自己好点," + "   对别人再好最终还是是被他妈的当成理所应当.";
			fos.write(str.getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void test1() {
		try (FileInputStream fis = new FileInputStream("src/a.txt");) {
			byte[] b = new byte[1024];
			int length = 0;
			while ((length = fis.read(b)) != -1) {
				String str = new String(b, 0, length);
				System.out.println(str);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值