IO流——字节流输入输出

/*
 字节流:
 读:InputStream  写:OutputStream

 需求:想要操作图片数据,这是就要用到字节流。
 */

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class FileStream{

	public static void main(String[] args) throws IOException {
		writeFile();
		readFile_3();
	}

	public static void<span style="color:#cc33cc;"> readFile_1()</span> throws IOException {
		FileInputStream fis = new FileInputStream("fos.txt");

		int ch = 0;
		while ((ch = fis.read()) != -1) {
			System.out.println((char) ch);
		}
		fis.close();
	}

	public static void <span style="color:#cc33cc;">readFile_2()</span> throws IOException {
		FileInputStream fis = new FileInputStream("fos.txt");

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

		fis.close();
	}

	public static void <span style="color:#cc33cc;">readFile_3()</span> throws IOException {
		FileInputStream fis = new FileInputStream("fos.txt");

		byte[] buf = new byte[<span style="background-color: rgb(51, 204, 0);">fis.available()</span>];// 定义一个刚刚好的缓冲区,不用再循环了。前提数据不能太大

		fis.read(buf);
		System.out.println(new String(buf));

		fis.close();
	}

	public static void writeFile() throws IOException {
		FileOutputStream fos = new FileOutputStream("fos.txt");

		fos.write("sbgfa".getBytes());

		fos.close();
	}
}

/*
 复制一个图片
 思路:
 1.用字节流读取流对象和图片关联
 2.用字节写入流对象创建一个图片文件。用于存储获取到的图片数据
 3.通过循环读写,完成数据的存储
 4.关闭资源
 */

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.management.RuntimeErrorException;

public class CopyPic {

	public static void main(String[] args) throws IOException {
		FileOutputStream fos = null;
		FileInputStream fis = null;
		try{
			fos = new FileOutputStream("c:\\2.bmp");
			fis = new FileInputStream("c:\\1.bmp");
			
			byte[] buf = new byte[1024];
			int len = 0;
			while((len = fis.read())!=-1)
			{
				fos.write(buf,0,len);
			}
		}catch(IOException e){
			throw new RuntimeException("复制文件失败");
		}finally{
			try {
				if(fis != null)
					fis.close();
			} catch (IOException e) {
				throw new RuntimeException("读取关闭失败");
			}
			try {
				if(fos != null)
					fos.close();
			} catch (IOException e) {
				throw new RuntimeException("写入关闭失败");
			}
		}
	}
}


/*
<span style="color:#cc0000;"> 字节流的缓冲区</span>
 BufferedInputStream
 BufferedOutputStream
 
 演示MP3的复制,通过缓冲区
 */


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.management.RuntimeErrorException;

public class CopyMp3 {

	public static void main(String[] args) throws IOException {
		
	}
	public static void copy_1() throws IOException{
		BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("c:\\0.mp3"));
		BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("c:\\1.mp3"));
		
		int by = 0;
		
		while((by = bufis.read())!=-1)
		{
			bufos.write(by);
		}
		bufos.close();
		bufis.close();
	}
}


/*
<span style="color:#cc0000;"> 读取转换流</span>
 
 直接使用readLine方法完成键盘录入的一行数据的读取。
 readLine方法是字符流BufferedReader类中的方法
 而键盘录入的read方法是字节流InputStream的方法
 
 将字节流转成字符流再使用字符流缓冲区的readLine方法
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class TransSstreamDemo 
{

	public static void main(String[] args) throws IOException
	{
		//获取键盘录入对象
		InputStream in = System.in;
		
		//将字节流对象转成字符流对象,使用转换流:InputStreamReader
		InputStreamReader isr = new InputStreamReader(in);
		
		//为了提高效率,将字符串进行缓冲区技术高效操作。使用BufferedReader
		BufferedReader bufr = new BufferedReader(isr);
				
		String line = null;
		
		while((line = bufr.readLine())!=null)
		{
			//定义结束标记
			if("over".equals(line))
				break;
			System.out.println(line.toUpperCase());
		}
		bufr.close();
	}

}


/*
<span style="color:#cc0000;">写入转换流</span>
 
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class TransStreamDemo
{

	public static void main(String[] args) throws IOException
	{
		/*
		//获取键盘录入对象
		InputStream in = System.in;
		
		//将字节流对象转成字符流对象,使用转换流:InputStreamReader
		InputStreamReader isr = new InputStreamReader(in);
		
		//为了提高效率,将字符串进行缓冲区技术高效操作。使用BufferedReader
		BufferedReader bufr = new BufferedReader(isr);
		*/
		//键盘的最常见写法
		<span style="color:#cc0000;">BufferedReader bufr = 
				new BufferedReader(new InputStreamReader(System.in));</span>
			
		/*
		OutputStream out = System.out;
		OutputStreamWriter osw = new OutputStreamWriter(out);
		BufferedWriter bufw = new BufferedWriter(osw);
		*/
		BufferedWriter bufw = 
				new BufferedWriter(new OutputStreamWriter(System.out));
				
		String line = null;
		
		while((line = bufr.readLine())!=null)
		{
			//定义结束标记
			if("over".equals(line))
				break;
			bufw.write(line.toUpperCase());
			bufw.newLine();
			bufw.flush();
		}
		bufr.close();
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值