字节输入流FileInputStream代码举例
FileInputStream fis = null;
try {
File file = new File("Hello.txt");
fis = new FileInputStream(file);
byte[] b = new byte[5];
int len;
while((len=fis.read(b)) != -1) {
String str = new String(b, 0, len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
FileInputStream fis = new FileInputStream(f1);
字节输出流FileOutputStream代码举例
FileOutputStream fos = null;
try {
File file = new File("hello2.txt");
if(!file.exists()) {
file.createNewFile();
}
fos = new FileOutputStream(file);
String str = "I love you forever and yibainian";
fos.write(str.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
字节输入流、输出流的混合使用
public void CopyFile(String src,String dest) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File f1 = new File(src);
File f2 = new File(dest);
fis = new FileInputStream(f1);
fos = new FileOutputStream(f2);
byte[] bs = new byte[24];
int len;
while((len = fis.read(bs))!= -1) {
fos.write(bs, 0, len);
}
} catch (IOException 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 testCopyFile() {
long start = System.currentTimeMillis();
String src = "one.jpg";
String dest = "two.jpg";
CopyFile(src, dest);
long end = System.currentTimeMillis();
System.out.println("花费的时间" + (end - start));
}
缓冲流的使用案例
package com.hpe.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.Test;
public class TestBuffered {
@Test
public void testBufferesInputOutputStream() {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
File f1 = new File("one.jpg");
File f2 = new File("1.jpg");
FileInputStream fis = new FileInputStream(f1);
FileOutputStream fos = new FileOutputStream(f2);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] b = new byte[1024];
int len;
while((len = bis.read(b)) != -1) {
bos.write(b,0,len);
bos.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void copyFile(String src,String dest) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
File f1 = new File(src);
File f2 = new File(dest);
FileInputStream fis = new FileInputStream(f1);
FileOutputStream fos = new FileOutputStream(f2);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] b = new byte[1024];
int len;
while((len = bis.read(b)) != -1) {
bos.write(b,0,len);
bos.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testCopyFile() {
long start = System.currentTimeMillis();
String src = "one.jpg";
String dest = "three.jpg";
copyFile(src, dest);
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}