------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
字节流:
package com.mth.img;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
*
* @ClassName: FileStream
* @Description: 字节流读写操作
* @author mth 75100313@qq.com
* @date 2014-2-18 上午09:03:09
*
*/
public class FileStream {
public static void writeFile() throws IOException {
FileOutputStream fos = new FileOutputStream("mth.txt");
fos.write("abcde".getBytes());
fos.close();
}
// 第一种方式 一个一个的读
public static void readFile_1() throws IOException {
FileInputStream fis = new FileInputStream("mth.txt");
int ch = 0;
while ((ch = fis.read()) != -1) {
System.out.println((char) ch);
}
fis.close();
}
// 第二种方式 读方式(建立缓冲区)
public static void readFile_2() throws IOException {
FileInputStream fis = new FileInputStream("mth.txt");
int ch = 0;
byte[] b = new byte[1024];
while ((ch = fis.read(b)) != -1) {
System.out.println(new String(b, 0, ch));
}
fis.close();
}
// 第三种方式读方式(不用循环)
public static void readFile_3() throws IOException {
FileInputStream fis = new FileInputStream("mth.txt");
// 拿到文件的大小 定义一个刚刚好的缓冲区
int num = fis.available();
byte[] b = new byte[num];
fis.read(b);
System.out.println(new String(b));
fis.close();
}
public static void main(String[] args) throws IOException {
// writeFile();
// readFile_1();
// readFile_2();
readFile_3();
}
}
字节流拷贝图片示例:
package com.mth.img;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
*
* @ClassName: ImgCopy
* @Description: 复制一个图片
* @author mth 75100313@qq.com
* @date 2014-2-18 上午09:30:23
* 步骤:
* 1。用字节读取流对象和图片关联
* 2.用字节写入流对象创建一个图片文件,用于存储获取到的图片数据
* 3.通过循环读写,完成数据的存储
* 4.关闭资源
*/
public class ImgCopy {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("mth.png");
fos = new FileOutputStream("mth_copy.png");
int len = 0;
byte[] b = new byte[1024];
while ((len = fis.read(b)) != -1) {
// 写有效数据
fos.write(b, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 关闭资源
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.mth.mp3;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
*
* @ClassName: CopyMp3
* @Description: 演示mp3的复制 通过缓冲区 BufferedOutputStream BufferedInputStream
* @author mth 75100313@qq.com
* @date 2014-2-18 上午09:41:11
*
*/
public class CopyMp3 {
public static void copyMp3_1() {
FileInputStream fis = null;
FileOutputStream fos = null;
// 定义缓冲区
BufferedInputStream bufis = null;
BufferedOutputStream bufos = null;
try {
fis = new FileInputStream("e:\\mth.mp3");
fos = new FileOutputStream("e:\\mth_copy.mp3");
bufis = new BufferedInputStream(fis);
bufos = new BufferedOutputStream(fos);
int by = 0;
// 边读边写
while ((by = bufis.read()) != -1) {
bufos.write(by);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 关闭资源
try {
if (bufis != null) {
bufis.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (bufos != null) {
bufos.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// 计算拷贝时间
long start = System.currentTimeMillis();
copyMp3_1();
long end = System.currentTimeMillis();
System.out.println(end - start + "毫秒");
}
}