IO流对象的继承关系
IO流
按操作数据单位不同分为:字节流(8bit),字符流(16bit)
字节流:处理声音或者图片等二进制的数据的流,以Stream结尾的类
字符流:处理文本数据(如txt文件)的流,以Reader和Writer结尾的类按数据流的流向不同分为:输入流,输出流
按流的角色的不同分为:节点流,处理流
文件流Demo:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.Test;
public class TestFileInputOutputStream {
@Test
public void testCopyFile() {
long start = System.currentTimeMillis();
// String src = "C:\\Users\\shkstart\\Desktop\\1.avi";
// String dest = "C:\\Users\\shkstart\\Desktop\\2.avi";
String src = "dbcp.txt";
String dest = "dbcp2.txt";
copyFile(src, dest);
long end = System.currentTimeMillis();
System.out.println("消耗时间为" + (end - start));
}
public void copyFile(String src, String dest) {
File file1 = new File(src);
File file2 = new File(dest);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
byte[] b = new byte[1024];
int len;
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testFileInputOutputStream() {
File file1 = new File("1.jpg");
File file2 = new File("2.jpg");
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
byte[] b = new byte[1024];
int len;
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// FileOutputStream
@Test
public void testFileOutputStream() {
FileOutputStream fos = null;
try {
fos = new FileOutputStream("hello2.txt");
//写到文件中的方法
fos.write(new String("I love China,热爱祖国").getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Test
public void testFileInputStream2() { // abcdefgcde
FileInputStream fis = null;
try {
fis = new FileInputStream("hello.txt");
byte[] b = new byte[5];
int len;
//读取字节数组,返回长度,失败返回-1
while ((len = fis.read(b)) != -1) {
// for (int i = 0; i < len; i++) {
// System.out.print((char) b[i]);
// }
String str = new String(b, 0, len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Test
public void testFileInputStream1(){
FileInputStream fis = null;
try {
fis = new FileInputStream("hello.txt");
int b;
//一个字节一个字节的读取字符,返回值为the next byte of data,
while ((b = fis.read()) != -1) {
System.out.print((char) b);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
if(fis!=null)
{
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
缓冲流Demo:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.junit.Test;
public class TestBuffered {
@Test
public void testBufferedReader() {
BufferedReader br = null;
BufferedWriter bw = null;
try {
File file = new File("dbcp.txt");
File file1 = new File("dbcp3.txt");
FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter(file1);
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
// char[] c = new char[1024];
// int len;
// while((len = br.read(c))!= -1){
// String str = new String(c, 0, len);
// System.out.print(str);
// }
String str;
while ((str = br.readLine()) != null) {
// System.out.println(str);
bw.write(str + "\n");
// bw.newLine();
bw.flush();
}
} 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();
}
}
}
}
@Test
public void testCopyFile() {
long start = System.currentTimeMillis();
String src = "D:\\software\\ubuntu12_04.iso";
String dest = "D:\\software\\ubuntu12_04_b.iso";
// String src = "D:\\software\\eclipse-jee-mars-R-win32.zip";
// String dest = "D:\\software\\eclipse-jee-mars-R-win32_b.zip";
copyFile(src, dest);
long end = System.currentTimeMillis();
System.out.println("花费时间为" + (end - start) / 1000 + "秒");
}
public void copyFile(String src,String dest) {
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
fis = new FileInputStream(src);
fos = new FileOutputStream(dest);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] b = new byte[1024];
int length;
while ((length = bis.read(b)) != -1) {
bos.write(b, 0, length);
}
} 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();
}
}
}
}
@Test
public void BufferedInputOutputStreamDemo() {
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
fis = new FileInputStream("1.jpg");
fos = new FileOutputStream("2.jpg");
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] b = new byte[1024];
int length;
while ((length = bis.read(b)) != -1) {
bos.write(b, 0, length);
}
} 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();
}
}
}
}
}
RandomAccessFile使用:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import org.junit.Test;
/*
* RandomAccessFile:支持随机访问
* 1.既可以充当一个输入流,有可以充当一个输出流
* 2.支持从文件的开头读取、写入
* 3.支持从任意位置的读取、写入(插入)
*/
public class TestRandomAccessFile {
//相较于test3,更通用
@Test
public void test4(){
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(new File("hello1.txt"),"rw");
raf.seek(4);
byte[] b = new byte[10];
int len;
StringBuffer sb = new StringBuffer();
while((len = raf.read(b)) != -1){
sb.append(new String(b,0,len));
}
raf.seek(4);
raf.write("xy".getBytes());
raf.write(sb.toString().getBytes());
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(raf != null){
try {
raf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//实现插入的效果:在d字符后面插入“xy”
@Test
public void test3(){
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(new File("hello1.txt"),"rw");
raf.seek(4);
String str = raf.readLine();//efg123456
// long l = raf.getFilePointer();
// System.out.println(l);
raf.seek(4);
raf.write("xy".getBytes());
raf.write(str.getBytes());
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(raf != null){
try {
raf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//实现的实际上是覆盖的效果
@Test
public void test2(){
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(new File("hello1.txt"),"rw");
raf.seek(4);
raf.write("xy".getBytes());
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(raf != null){
try {
raf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//进行文件的读、写
@Test
public void test1(){
RandomAccessFile raf1 = null;
RandomAccessFile raf2 = null;
try {
raf1 = new RandomAccessFile(new File("hello.txt"), "r");
raf2 = new RandomAccessFile(new File("hello1.txt"),"rw");
byte[] b = new byte[20];
int len;
while((len = raf1.read(b)) != -1){
raf2.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(raf2 != null){
try {
raf2.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(raf1 != null){
try {
raf1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}