IO流

1.IO流

2. 1字节流(一切皆为字节)

字节输出流
	public static void main(String[] args) throws IOException{
		//创建一个file output stream对象,构造方法中传递写入数据的目的地
		FileOutputStream fos=new FileOutputStream("E:\\javacode\\111\\src\\io\\a.txt");
		//调用FileOutputStream对象中write方法,把数据写入到文件中
		fos.write(97);//一次只能写一个字节  a
		fos.write(97);//a
		byte[] b1= {65,66,65,67};//多字节写入   ABAC
		fos.write(b1);//aaABAC
		//释放资源
		fos.close();
		
		FileOutputStream fos1=new FileOutputStream(new File("E:\\javacode\\111\\src\\io\\b.txt"));
		fos1.write(97);
		fos1.write(97);
		//写入的第一个字节是负数,那第一个字节会和第二个字节,两个字节组成一个中文显示
	//	byte[] b= { -65, 67,-65, -66, 77}; //烤緾   gbk  两个字节
	//	fos1.write(b);
		fos1.write(b1, 1, 2);//数组中部分写入文件
		
		byte[] b2="你好".getBytes();
		System.out.println(Arrays.toString(b2));//[-28, -67, -96, -27, -91, -67]  utf-8  3个字节
		fos1.write(b2);
		fos1.close();
	}


//true  继续写入
		FileOutputStream fos=new FileOutputStream("E:\\javacode\\111\\src\\io\\a.txt", true);
		for (int i = 0; i < 3; i++) {
			fos.write("你好".getBytes());
			fos.write("\r\n".getBytes());//换行写入
		}
		fos.close();
字节输入流
//一次读取一个字节
public static void main(String[] args) throws IOException {
		// 创建一个FileInputStream对象,构造方法中绑定要读取的数据源
		FileInputStream fis = new FileInputStream("E:\\javacode\\111\\src\\io\\b.txt");
		// 调用FileOutputStream对象中read方法,读取文件
		/*
		 * int len=fis.read(); 
		 * System.out.println(len);//97 
		 * int len1=fis.read();
		 * System.out.println(len1);//66 
		 * int len2=fis.read();
		 * System.out.println(len2);//228 一个中文字符占3个字节 
		 * int len3=fis.read();
		 * System.out.println(len3);//189 
		 * int len4=fis.read();
		 * System.out.println(len4);//160
		 *  int len5=fis.read();
		 * System.out.println(len5);//-1 读取到最后没内容为-1
		 */
		int a=0;
		while ((a=fis.read())!=-1) {
			System.out.println(a);
		}
		// 释放资源
		fis.close();
	}

//一个读取多个字节
	public static void main(String[] args) throws IOException {
		// 创建一个FileInputStream对象,构造方法中绑定要读取的数据源
		FileInputStream fis = new FileInputStream("E:\\javacode\\111\\src\\io\\b.txt");
		// 调用FileOutputStream对象中read方法,读取文件
		/*
		 * byte[] bytes = new byte[2]; 
		 * int len = fis.read(bytes);
		 * System.out.println(len);//2 读取的有效字节个数
		 * System.out.println(Arrays.toString(bytes));// [97, 66] 		
		 * System.out.println(newString(bytes));// aB
		 * len = fis.read(bytes); System.out.println(len);//2 读取的有效字节个数
		 * System.out.println(Arrays.toString(bytes));// [99, 100]
		 * System.out.println(new String(bytes));// cd 
		 * len = fis.read(bytes); 
		 * System.out.println(len);// -1 读取结束标志
		 * System.out.println(Arrays.toString(bytes));// [99, 100]
		 * System.out.println(new String(bytes));// cd 
		 * fis.close();
		 */

		byte[] bytes =new byte[1024];
		int len=0;
		while ((len=fis.read(bytes))!=-1) {
			System.out.println(new String(bytes,0,len));//有效字符串转换,不浪费内存空间
		}
		fis.close();
	}
public static void main(String[] args) throws IOException {
		FileOutputStream fos=new FileOutputStream("E:\\javacode\\111\\src\\io\\aa.txt");
		FileInputStream fis = new FileInputStream("C:\\Users\\THINK\\Desktop\\ps.txt");
		/*
		 * int a=0; 
		 * while ((a=fis.read())!=-1) { 
		 *         fos.write(a);
		 * }
		 */
		//使用数组缓冲流,读写多个字节速度变快
		byte[] bytes =new byte[1024];
		int len=0;//每次读取的有效字节个数
		while ((len=fis.read(bytes))!=-1) {
			fos.write(bytes,0,len);
		}
		fos.close();
		fis.close();
	}

2.2 字符流

字符流读取文件
public static void main(String[] args) throws IOException {
		FileReader fr = new FileReader("E:\\javacode\\111\\src\\io\\a.txt");
		/*
		 * 
		 * int len=0;//每次读取的有效字节个数 
		 * while ((len=fr.read())!=-1) {
		 * System.out.print((char)len); 
		 * }
		 */
		char[] bytes = new char[1024];
		int len = 0; // 每次读取的有效字节个数
		while ((len = fr.read(bytes)) != -1) {
			System.out.println(new String(bytes,0,len));
		}
		fr.close();
	}

字符流写入数据
public static void main(String[] args) throws IOException {
		FileWriter fw = new FileWriter("E:\\javacode\\111\\src\\io\\d.txt");
		// 写入单个字符
		fw.write(99);
		char[] b= {'a','2','3','-','-'};
		fw.write(b);
		
		fw.write("你好!");
		fw.write("你好!",2,1);
		// 把内存缓存区中的数据,刷新到文件中 刷新之后流可以继续使用
		fw.flush();
		fw.close();
	}
	
**//续写在方法后加    true  
//行换   \r\n**


属性集

Properties类表示一个持久层的属性集。Properties可保存在流中或从流中加载
Properties集合是一个唯一和IO流相结合的集合
	可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
	可以使用Properties集合中load,把硬盘中保存的文件(键值对),读到集合中使用
属性列表中每个键及其对应值都是一个字符串
	Properties集合是一个双列集合,key和value默认都是字符串


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值