最基础的读取文件
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileIOTest {
public static void main(String[] args) {
File file = new File("E:/abc.txt");
FileInputStream is = null;
try {
is = new FileInputStream(file);
byte[] bytes = new byte[3]; //缓冲容器
int len = -1; //接收长度
while ((len = is.read(bytes)) != -1) {
//字符数组-->字符串,解码
String s = new String(bytes, 0, len);
System.out.println(s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(is != null){
is.clise();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
从文件读取到另一个文件
packagecom.svs;import java.io.*;public classIOTest {public static voidmain(String[] args) {
File file= new File("E:/abc.txt");
FileInputStream is= null;
FileOutputStream os= null;try{
is= newFileInputStream(file);
os= new FileOutputStream("F:/abc.txt");byte[] bytes = new byte[3];int len = -1;while ((len=is.read(bytes)) != -1){
os.write(bytes,0,len);
os.flush();
}
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally{try{//先打开的后关闭if(os != null){
os.close();
}if(is != null){
is.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
IO流的工具类,抽离出读取、写入、关闭
import java.io.*;public classFileUtil {public static voidmain(String[] args) {
copy("D:/123441.jpg", "E:/123441.jpg");
}/*** 流的读取与写入
*
*@paramsrcPath
*@paramdestPath*/
public static voidcopy(String srcPath, String destPath) {
File src= newFile(srcPath);
File dest= newFile(destPath);try (InputStream is = newFileInputStream(src);
OutputStream os= newFileOutputStream(dest)) {byte[] flush = new byte[1024 * 2]; //缓冲容器
int len = -1; //接收长度
while ((len = is.read(flush)) != -1) {
os.write(flush,0, len);
os.flush();//刷新缓存
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
使用字节数组流读取数据
补充:流的来源或目的地并不一定是文件,也可以是内存中的一块空间,例如一个字节数组。就需使用字节数组流处理,且字节数组流不用关闭。
packagecom.svs;import java.io.*;public classByteArrayStreamFileUtil {public static voidmain(String[] args) {//使用字节数组流读取,写入数据,字节数组流是不用关闭的
try{//从硬盘中读取文件,存储到内存中
InputStream is = new FileInputStream("E:/aa.jpg");
ByteArrayOutputStream baos= newByteArrayOutputStream();
copy(is, baos);//从内存中读取数据,存储到硬盘中
InputStream bais = newByteArrayInputStream(baos.toByteArray());
OutputStream os= new FileOutputStream("F:/aa.jpg");
copy(bais, os);
closeIO(is, os);
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}/*** 流的读取与写入
*
*@paramis
*@paramos*/
public static voidcopy(InputStream is, OutputStream os) {try{byte[] flush = new byte[1024 * 2]; //缓冲容器
int len = -1; //接收长度
while ((len = is.read(flush)) != -1) {
os.write(flush,0, len);
os.flush();//刷新缓存
}
}catch(IOException e) {
e.printStackTrace();
}finally{try{
is.close();
os.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}/*** 关闭流
*
*@paramis
*@paramos*/
public static voidcloseIO(InputStream is, OutputStream os) {try { //先打开的先关闭
if (os != null) {
os.close();
}if (is != null) {
is.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
总结:ByteArrayOutputStream或ByteArrayInputStream是内存读写流,不同于指向硬盘的流,它内部是使用字节数组读内存的,这个字节数组是它的成员变量,当这个数组不再使用变成垃圾的时候,Java的垃圾回收机制会将它回收,所以不需要关流。也就是说,指向内存的流可以不用关闭,指向存储卡/硬盘的流一定要关闭。
使用字节数组流读取数据-升级版
JDK1.7之后引入
try( ){
}catch(){
}
方法,省去finally,进行自动关闭。(这是使用 try-with-resources 资源自动释放特性)
packagecom.svs;import java.io.*;public classByteArrayStreamFileUtil2 {public static voidmain(String[] args) {//JDK1.7之后,引入try(){}方法进行关闭
try (InputStream is = new FileInputStream("E:/aa.jpg");
OutputStream os= new FileOutputStream("F:/aa.jpg");) {
ByteArrayOutputStream baos= newByteArrayOutputStream();
copy(is, baos);
InputStream bais= newByteArrayInputStream(baos.toByteArray());
copy(bais, os);
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}/*** 流的读取与写入
*
*@paramis
*@paramos*/
public static voidcopy(InputStream is, OutputStream os) {try{byte[] flush = new byte[1024 * 2]; //缓冲容器
int len = -1; //接收长度
while ((len = is.read(flush)) != -1) {
os.write(flush,0, len);
os.flush();//刷新缓存
}
}catch(IOException e) {
e.printStackTrace();
}finally{try{
is.close();
os.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
字节缓冲流
BufferedInputStream,BufferedOutputStream。字节缓冲流是对读取【性能】的提升,使用字节流时建议加上字节缓冲流。
import java.io.*;/*** @Description: 字节缓冲流 BufferedInputStream BufferedOutputStream
* @Description: 字节缓冲流是对读取【性能】的提升,使用字节流时建议加上字节缓冲流*/
public classBufferedStream {public static voidmain(String[] args) {//long l1= System.currentTimeMillis();
copy("D:/123441.jpg", "E:/123441.jpg");//long l2= System.currentTimeMillis();//System.out.println(l2-l1);
}/*** 流的读取与写入
*
*@paramsrcPath
*@paramdestPath*/
public static voidcopy(String srcPath, String destPath) {
File src= newFile(srcPath);
File dest= newFile(destPath);try (InputStream is = new BufferedInputStream(newFileInputStream(src));
OutputStream os= new BufferedOutputStream(newFileOutputStream(dest))) {byte[] flush = new byte[1024 * 2]; //缓冲容器
int len = -1; //接收长度
while ((len = is.read(flush)) != -1) {
os.write(flush,0, len);
os.flush();//刷新缓存
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
字符缓冲流
import java.io.*;/*** @Description: 字符缓冲流 BufferedInputStream BufferedOutputStream
* @Description: 字符缓冲流是对读取【性能】的提升,使用字符流时建议加上字符缓冲流*/
public classBufferedRWStream {public static voidmain(String[] args) {
copy("D:/123441.txt", "E:/123441.txt");
}/*** 流的读取与写入
*
*@paramsrcPath
*@paramdestPath*/
public static voidcopy(String srcPath, String destPath) {
File src= newFile(srcPath);
File dest= newFile(destPath);try (BufferedReader br = new BufferedReader(newFileReader(src));
BufferedWriter bw= new BufferedWriter(newFileWriter(dest))) {
String line= null;while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();//刷新缓存
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
转换流
InputStreamReader OutputSttreamWriter。
1、以字符流的形式操作字节流(纯文本);2、指定字符集(API手册查看)
import java.io.*;/*** @Description: 转换流 InputStreamReader OutputSttreamWriter
* @Description: 1、以字符流的形式操作字节流(纯文本)
* @Description: 2、指定字符集(API手册查看)*/
public classConvertTest {public static voidmain(String[] args) {//操作System.in和System.out,均是字节流
try(BufferedReader reader=new BufferedReader(newInputStreamReader(System.in));
BufferedWriter writer=new BufferedWriter(newOutputStreamWriter(System.out))){//循环获取键盘的输入(exit退出),输出此内容
String msg="";while (!"exit".equals(msg)){
msg=reader.readLine(); //循环读取
writer.write(msg); //循环写出
writer.newLine();
writer.flush();//强制刷新
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
commons-io
Apache下的commonsIO,其中有封装好的FileUtil工具类,实际开发中无需写原生代码,拿来即用即可。