标签:
javaIO流文件操作涵盖FileInputStream FileOutStream OutStream
package com.org.stream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 读取文件工具类属于字节流,这里采用FileInputStream
*
* @author wangsl
*
*/
public class FileInputTools {
private File file;
private String name;
public FileInputTools() {
}
public FileInputTools(File file) {
this.file = file;
}
public FileInputTools(String name) {
this.name = name;
}
public void readFile() {
// 开始读取文件到流中,创建缓冲区
byte[] b = new byte[1024];
try {
InputStream in = new FileInputStream(file);
OutputStream out=new FileOutputStream("E:\\bb.txt");
int readnum=0;
while ((readnum=in.read(b))!= -1) {
System.out.println("读取文件大小===="+readnum);
out.write(b,0,readnum);
}
//关闭输入流
in.close();
out.close();
} catch (Exception e) {
// TODO: handle exception
}
}
public void readFile(String ftype) {
// 开始读取文件到流中,创建缓冲区
byte[] b = new byte[1024];
try {
//创建输入读取输入流
InputStream in = new FileInputStream(file);
String outPath="E:\\bb"+ftype;
//写入文件
OutputStream out=new FileOutputStream(outPath);
int readnum=0;
while ((readnum=in.read(b))!= -1) {
//System.out.println("读取文件大小===="+readnum);
out.write(b,0,readnum);
}
//关闭输入流
in.close();
//关闭输出流
out.close();
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
File file=new File("E:\\iv_login_cctv.png");
//System.out.println(file.getName());
String ftype=file.getName();
//获取文件类型
if(ftype!=null) ftype=ftype.substring(ftype.lastIndexOf("."), ftype.length());
FileInputTools tools =new FileInputTools(file);
tools.readFile(ftype);
}
}
标签:
原文地址:http://my.oschina.net/u/238082/blog/506151