package io;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/*
* 字节流多用于传输非文本文件
*/
public class FileOutputStreamDemo {
//写文件,将字符串转换成字节
private static void writeFile() throws IOException{
FileOutputStream fos = new FileOutputStream("E:\\BaiduYunDownload\\3.txt");
fos.write("test:E:\\BaiduYunDownload\\3.txt".getBytes());
fos.close();
}
//读取文件,一个字节一个字节的读取
private static void readFile() throws IOException{
FileInputStream fis = new FileInputStream("E:\\BaiduYunDownload\\3.txt");
int ch = 0;
while((ch=fis.read())!=-1){
System.out.print((char)ch);
}
fis.close();
}
//用字节数组读取文件
private static void readFileBuf() throws IOException{
FileInputStream fis = new FileInputStream("E:\\BaiduYunDownload\\3.txt");
int ch = 0;
byte[] buf = new byte[1024];
while((ch=fis.read(buf))!=-1){
System.out.print(new String(buf,0,ch));
}
fis.close();
}
//复制图片
private static void copyImg() throws IOException{
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("E:\\BaiduYunDownload\\1.jpg");
fos = new FileOutputStream("E:\\BaiduYunDownload\\2.jpg");
int len = 0;
byte[] buf = new byte[1024];
while((len=fis.read(buf))!=-1){
fos.write(buf, 0, len);
}
System.out.println("图片复制成功");
} catch (IOException e) {
throw new IOException("文件复制失败");
}finally{
if(fis!=null){
fis.close();
System.out.println("fis关闭成功");
}
if(fos!=null){
fos.close();
System.out.println("fos关闭成功");
}
}
}
//使用自定义字节缓冲区复制MP3
private static void copyMP3() throws IOException{
MyBufferedInputStream mbis = null;
BufferedOutputStream bos = null;
try {
mbis = new MyBufferedInputStream(new FileInputStream("E:\\BaiduYunDownload\\1.mp3"));
bos = new BufferedOutputStream(new FileOutputStream("E:\\BaiduYunDownload\\2.mp3"));
int len = 0;
byte[] buf = new byte[1024];
while((len=mbis.myRead())!=-1){
bos.write(len);
}
System.out.println("MP3复制成功");
} catch (IOException e) {
throw new IOException("文件复制失败");
}finally{
if(mbis!=null){
mbis.myClose();
System.out.println("bis关闭成功");
}
if(bos!=null){
bos.close();
System.out.println("bos关闭成功");
}
}
}
//测试
public static void main(String[] args) {
try {
long start = System.currentTimeMillis();
// writeFile();
// readFileBuf();
// copyImg();
copyMP3();
long end = System.currentTimeMillis();
System.out.println("成功:拷贝用时"+(end-start)+"ms");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//自定义字节流缓冲区
class MyBufferedInputStream{
private InputStream is;
private byte[] buf = new byte[1024];
private int pos =0,len = 0;
MyBufferedInputStream(InputStream is){
this.is = is;
}
//一次读取一个字节,从缓冲区(字节数组)获取
public int myRead() throws IOException {
//判断是否缓冲区的数据已经获取完
if(len==0){
//如果为空,则需要读取新数据到缓冲区来
len=is.read(buf);
//如果文件读取完毕,则返回-1
if(len<0){
return -1;
}
//并且把(字节数组的)指针还原到(字节数组的)起始位置
pos = 0;
//获取缓冲区中的数据
byte b = buf[pos];
//每获取一个字节,意味着着缓冲区的数据减少一个
len--;
//每获取一个字节,指针就要向后移动一位
pos++;
return b&0xff;
}else if(len>0){//如果缓冲区不为空,只从缓冲区中读取
byte b = buf[pos];
len--;
pos++;
return b&0xff;
}
return -1;
}
public void myClose() throws IOException{
is.close();
}
}
在使用IO输入输出时,要把握好这些流操作的规律,有时还需要用到转换流
package io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
/**
* 1.如上一个示例(InputStreanReaderDemo)中
* 源:键盘录入
* 目的:控制台输出
*
* 2.需求:把键盘录入的数据存储到一个文件中
* 源:键盘
* 目的:文件
*
* 3.需求:将一个文件中的数据打印在控制台上
* 源:文件
* 目的:控制台
*
* 流操作的基本规律
* 1.明确源和目的
* 源:输入流 InputStream Reader
* 目的:输出流 OutputStream Writer
*
* 2.明确操作的数据是否是纯文本
* 是:字符流
* 不是:字节流
*
* 3.当体系明确后,在明确使用哪个具体的对象,通过设备来进行区分
* 源设备:内存,硬盘,键盘
* 目的设备:内存,硬盘(文件),控制台
*/
/*
* 转换流:字符和字节之间转换,通常涉及到字符编码转换时,需要用到转换流
*/
public class InputStreamReaderDemo {
public static void main(String[] args) throws IOException {
//获取键盘录入对象
InputStream is = System.in;
//将字节流对象转换成字符流对象,只用转换流。InputStreamReader
InputStreamReader isr = new InputStreamReader(is);
//为了提高效率,将字符串进行缓冲区技术高效操作,使用BufferedReader
BufferedReader br = new BufferedReader(isr);
//获取控制台输出对象
OutputStream os = System.out;
//将字符流转换成字节流对象
OutputStreamWriter osw = new OutputStreamWriter(os);
//输出缓冲区
BufferedWriter bw = new BufferedWriter(osw);
//向文件输出
FileOutputStream fos = new FileOutputStream("E:\\BaiduYunDownload\\1.txt");
OutputStreamWriter osw2 = new OutputStreamWriter(fos,"UTF-8");
BufferedWriter bw2 = new BufferedWriter(osw2);
String line = null;
try {
while((line=br.readLine())!=null){
if("over".equalsIgnoreCase(line)){
break;
}
//控制台打印
bw.write(line);
bw.newLine();
bw.flush();
//向文件输出
bw2.write(line);
bw2.newLine();
bw2.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(br!=null){
br.close();
}
if(bw!=null){
bw.close();
}
if(bw2!=null){
bw2.close();
}
}
}
}
最后简单说一下打印流
package io;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
/*
* 打印流
* PrintWriter与PrintStream:可以直接操作输入流文件
*
* PrintStream字节打印流
* 构造函数可以接收的参数类型:1.file对象 2.字符串路径 3.字节输出流OutputStream
*
* PrintWriter字符输出流
* 构造函数可以接收的参数类型:1.file对象 2.字符串路径 3.字节输出流OutputStream 4.字符输出流Writer
*/
public class PrintWriterDemo {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
String line = null;
while((line=br.readLine())!=null){
pw.write(line);
}
br.close();
pw.close();
}
}