21天精通java基础之Day15IO流(二)

26 篇文章 0 订阅

Day15IO流(二):


  使用FileReader、FileWriter可以实现文本文件的复制。

  对于非文本文件(视频文件、音频文件、图片),只能使用字节流。

实例:

@Test
	public void testFileReaderWriter1(){
		//1.输入流对应的文件src一定要存在,否则跑异常。输出流对应的文件dest可以不存在,执行过程会自动创建。
		File file1 = new File("hello.txt");
		File file2 = new File("hello3.txt");
		FileReader fr = null;
		FileWriter fw = null;
		
		try{
			fr = new FileReader(file1);
			fw = new FileWriter(file2);
			
			char[] c = new char[20];
			int len;
			while((len = fr.read(c)) != -1)
			{
				fw.write(c, 0, len);
			}
		}
			catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}finally{
				if(fw != null){
					try {
						fw.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
				if(fr != null){
					try {
						fr.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
					
		}
		

    缓冲流:复制图片

 BufferedInputStream,BufferedOutputStream

 实例:

	@Test
	public void TestBuffered1(){
	
		//1.提供读入写出的文件
		File file1 = new File("1.jpg");
		File file2 = new File("2.jpg");
		
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			//2.创建相应的节点流
			FileInputStream fis = new FileInputStream(file1);
			FileOutputStream fos = new FileOutputStream(file2);
			//3.将创建的节点流的对象作为形参传递给缓冲流的构造器中
			bis = new BufferedInputStream(fis);
			bos = new BufferedOutputStream(fos);
			//4.文件复制
			byte[] b = new byte[1024];
			int len;
			while((len = bis.read(b)) != -1){
				bos.write(b,0,len);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//关闭流
		if(bos != null){
			try {
				bos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}if(bis != null){
			try {
				bis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
		}
	}

缓冲流实例:

BufferedReader、BufferedWriter

	@Test
	public void testBuffered2(){
		File file1 = new File("hello.txt");
		File file2 = new File("hello2.txt");
		
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			FileReader fr = new FileReader(file1);
			FileWriter fw = new FileWriter(file2);
			
			br = new BufferedReader(fr);
			bw = new BufferedWriter(fw);
			
			String str ;
			while((str = br.readLine()) != null){
				bw.write(str + "\n");
				//bw.newLine();
				bw.flush();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
			if(br != null){
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}			
			}			
		}
		
	}

转换流:InputStreamReader OutputStreamWriter

编码:字符串---》字节数组

解码:字节数组---》字符串


标准的输入输出流:

  标准的输入流:System.in

  标准的输出流:System.out

题目:

  从键盘输入字符串,要求将读取到的整行字符串转换成大写输出,然后继续进行输入操作,直到当输入“e”,或者输入“exit”时,退出程序。

@Test
	public void test(){
		BufferedReader br = null;
		try {
			InputStream  is = System.in;
			InputStreamReader isr = new InputStreamReader(is);
			br = new BufferedReader(isr);
			
			while(true){
				System.out.println("请输入字符串:");
				String str;
				str = br.readLine();
				if(str.equalsIgnoreCase("e") || str.equalsIgnoreCase("exit")){
					System.out.println("结束!");
					break;
				}
				String upper = str.toUpperCase();
				System.out.println(upper);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(br != null){
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
			
		}

  数据流:

  用来处理基本的数据类型、String、字节数组的数据:DataInputStream DataOutputStream.

    RandomAccessFile:支持随机访问

  1.既可以充当一个输入流,又可以充当一个输出流。

  2.支持从文件的开头读取、写入。

  3.支持从任意位置的读取、写入(插入)。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值