缓冲流,转换流练习

缓冲流,转换流练习

练习一:高效字节输出流写出字节数据

描述:利用高效字节输出流往C盘下的d.txt文件输出一个字节数。

  1. 创建字节输出流对象关联文件路径

  2. 利用字节输出流对象创建高效字节输出流对象

  3. 调用高效字节输出流对象的write方法写出一个字节

  4. 关闭高效流,释放资源。

public class text1 {
	public static void main(String[] args) throws IOException {
		FileOutputStream fos = new FileOutputStream("C:\\d.txt");
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		bos.write(97);
		bos.flush();
		bos.close();
		fos.close();

	}
}

练习二:高效字节输出流写出字节数组数据

描述:利用高效字节输出流往C盘下的e.txt文件写出一个字节数组数据,如写出:”i love java”

  1. 创建字节输出流对象关联文件路径

  2. 利用字节输出流对象创建高效字节输出流对象

  3. 定义字符串存放要输出的数据,然后将字符串转换为字节数组。

  4. 调用高效字节输出流对象的write方法将字节数组输出。

    5.关闭高效流

public class text2 {
	public static void main(String[] args) throws IOException {
	FileOutputStream fos = new FileOutputStream("D:\\e.txt");
	BufferedOutputStream bos = new BufferedOutputStream(fos);
	bos.write("i love java".getBytes());
	bos.flush();
	bos.close();
	fos.close();
	
	}
}

练习三:高效流文件复制

描述:利用高效字节输入流和高效字节输出流完成文件的复制。

1.将C盘下的c.png文件复制到D盘下

2.一次读写一个字节数组方式复制

public class text3 {
	public static void main(String[] args) throws IOException {
		FileInputStream fis = new FileInputStream("D:\\e.txt");
		FileOutputStream fos = new FileOutputStream("E:\\b.txt");
		BufferedInputStream bis = new BufferedInputStream(fis);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		int len;
		while((len=bis.read())!=-1){
			bos.write(len);
		}
		bos.flush();
		bos.close();
		bis.close();
		
				
	}
}

练习四:高效字符流和集合的综合使用

描述:

分析以下需求,并用代码实现

​ 实现一个验证码小程序,要求如下:

​ 1. 在项目根目录下新建一个文件:data.txt,键盘录入3个字符串验证码,并存入data.txt中,要求一个验证码占一行;

​ 2. 键盘录入一个需要被校验的验证码,如果输入的验证码在data.txt中存在:在控制台提示验证成功,如果不存在控制台提示验证失败

public class text817 {
	public static void main(String[] args) throws IOException {
		WriterCode();
		yanzhengCode();
		
	}
	public static void  WriterCode() throws IOException{
		FileWriter fos  = new FileWriter("D:\\图\\data.txt");
		
		BufferedWriter bw = new BufferedWriter(fos);
		for(int i =0 ; i<3 ; i++){
			System.out.println("请输入你的第" + (i+1)+"个字符串");
			Scanner sc = new Scanner(System.in);
			String str = sc.next();
			bw.write(str);
			bw.newLine();
			bw.flush();
		}
		bw.close();
		
	}
	public static void yanzhengCode() throws IOException{
		FileReader fis = new FileReader("D:\\图\\data.txt");
		ArrayList<String> list = new ArrayList<>();
		BufferedReader br =  new BufferedReader(fis);
		String str=null;
		while((str=br.readLine())!=null){
			list.add(str);
		}
		System.out.println("请输入你的验证码:");
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		System.out.println(list);
		if(list.contains(s)){
			System.out.println("验证成功");
		}
		else{
			System.out.println("验证失败");
		}
		br.close();
	}
}
//自己第一次敲得
public class text4 {
	public static void main(String[] args) throws IOException {
		FileOutputStream fos = new FileOutputStream("D:\\图\\data.txt");
		FileInputStream fis = new FileInputStream("D:\\图\\data.txt");
		System.out.println("请输入要三个字符串:");
		Scanner sc = new Scanner(System.in);
		String str = sc.next();
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		BufferedInputStream bis = new BufferedInputStream(fis);
		fos.write(str.getBytes());
		System.out.println("请输入要校验的验证码");
		Scanner s = new Scanner(System.in);
		String sts = s.next();
		
		int len;
		byte[] by = new byte[1024];
		while((len = bis.read(by))!=-1){
			String q = new String(by);
			if(q.contains(sts)){
				System.out.println("验证成功");
				}
			else{
					System.out.println("验证失败");
				}
			}
		bos.close();
		bis.close();
		fis.close();
		fos.close();
		}
		

练习五:转换输出流的使用

描述:现有一字符串:”我爱Java”。将该字符串保存到当前项目根目录下的a.txt文件中。

要求:使用gbk编码保存。

注意:idea的默认编码是utf-8,所以可以通过fileàsettingsàfile encodings设置为gbk格式,否则打开a.txt文件看到的将会是乱码。

public class text {
	public static void main(String[] args) throws IOException {
		FileOutputStream fos = new FileOutputStream("D:\\图\\a.txt");
		OutputStreamWriter ops = new OutputStreamWriter(fos,"GBK");
		ops.write("我爱Java");
		ops.flush();
		ops.close();
	}
}

练习六:转换输入流的使用

描述:利用转换输入流将当前项目根目录下使用gbk编码的a.txt文件的内容读取出来,并打印在控制台上。

要求:不能出现乱码的情况。

public class text6 {
	public static void main(String[] args) throws IOException {
		FileInputStream fis = new FileInputStream("D:\\图\\a.txt");
		InputStreamReader isr = new InputStreamReader(fis,"GBK");
		int len;
		char[] by = new char[1024];
		while((len = isr.read(by))!=-1){
			System.out.println(new String(by,0,len));
		}
		isr.close();
		
		
	}
}

小结:

​ 多练习

​ 有人吗

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值