Java的文件操作在系统开发中是非常平凡的功能,如:文件的上传下载,Excel类文件数据的导入导出等等。
通常的IO操作有两种,一种是字节流的操作,一种是字符流的操作。
字节流有输入流和输出流,字符流也有输入流和输出流。
输入流(从屏幕、硬盘或是其他数据源读取数据放入内存中)和输出流(用来向屏幕、硬盘等目的地输出内容)
字节流就是一个字节一个字节的读取或是输出(字节都是8位,0到255之间的整数)
字符流是按照文本的那种字符来读取和输出,就是直接读取数字、字母或是中文字等这些我们能够直接识别的字符。
关于缓冲流:
缓冲流,是指当输出的时候带 有缓冲功能的流(BufferOutputStream),没有缓冲功能的流当其输出时是直接存入目的地,如果有缓冲功能,则会将输出的内容先放置在内存中,等到有一定数据量的时候,或是流关闭、或调用flush()方法时,才会将相应的内容保存到硬盘或是其它目的地中。
字节流操作时,我们通常用FileInputStream,FileOutputStream。而字符流操作时,我人通常用到FileReader和FileWriter。配合缓冲流使用:BufferedReader和BufferedWriter。BufferedInputStream和BufferedOutputStream。
下面代码是一个文件操作的基础实例:
package com.zyujie.io;
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.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class FileOp {
/*
* 根据路径判断是否为目录
*/
public boolean isDirectory(String path){
File dirName = new File(path);
if(dirName.isDirectory()){
return true;
}else{
return false;
}
}
/*
* 创建目录
*/
public boolean createDirectory(String path){
File dirName = new File(path);
return dirName.mkdir();
}
/*
* 判断文件是否存在
*/
public boolean isFile(String path){
File fileName = new File(path);
if(fileName.isFile()){
return true;
}else{
return false;
}
}
/*
* 创建文件
*/
public boolean createFile(String path) throws IOException{
File fileName = new File(path);
return fileName.createNewFile();
}
/*
* 向文件里写入内容,如果是向文本文件里写内容建议使用字符流的FileWriter,不容易乱码
* 如果是生成图片这类文件,建议使用字节流的FileOutputStream。或者缓冲流
*/
public void writeFile(String path){
try {
//读取控制台输入字符,进缓冲区
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
//指定要写入的目标文件
FileWriter fw = new FileWriter(path);
//缓冲对文件的输出
BufferedWriter bw = new BufferedWriter(fw);
while(true){
//读一行
String tempStr = br.readLine();
//写一行
bw.write(tempStr);
//写入一行分隔符
bw.newLine();
//如果写入字符流中有end,就退出写入操作
if(tempStr.equals("end")){
break;
}
}
br.close();
isr.close();
bw.flush();
bw.close();
fw.close();
} catch (Exception e) {
}
}
/*
* 读文件,同理写文件操作,通过字符流去操作
*/
public void readFile(String path){
try {
//读取文件,指定目标文件
FileReader fr = new FileReader(path);
//将输入流文件写入缓冲区
BufferedReader br = new BufferedReader(fr);
//定义字符串变量存取读出来的值,默认不占内存
String tempStr = "";
//如果此流已经准备好被读取
while(br.ready()){
tempStr += br.readLine();
}
br.close();
fr.close();
System.out.println(tempStr);
} catch (Exception e) {
}
}
/*
* 指定目录生成图片和excel类的文件,采用字节流,文件复制的一个功能
*/
public void copyImage(String path,String newPath){
try {
//读取要复制的目标图片
File file = new File(path);
//将目标图片读成字节流
FileInputStream fis = new FileInputStream(file);
//将字节流缓冲到缓冲区
BufferedInputStream bis = new BufferedInputStream(fis);
//指定生成的目标图片位置
File newFile = new File(newPath);
//准备输出流
FileOutputStream fos = new FileOutputStream(newFile);
//输出流文件缓冲
BufferedOutputStream bos = new BufferedOutputStream(fos);
//字节流长度
int len = 0;
//一直循环到字节流长度末尾
//不使用缓冲流
//while((len = fis.read()) != -1){
// fos.write(len);
//}
//使用缓冲流
while((len = bis.read()) != -1){
bos.write(len);
}
bos.flush();
bos.close();
fos.close();
bis.close();
fis.close();
} catch (Exception e) {
}
}
public static void main(String[] args) throws IOException {
FileOp op = new FileOp();
String path = "D://zhouyujie//log.txt";
String imgPath = "D://zhouyujie//edu.png";
String imgNewPath = "D://zhouyujie//edu附件.png";
String xlsPath = "D://zhouyujie//tcms.xlsx";
String xlsNewPath = "D://zhouyujie//tcms附件.xlsx";
boolean flag = op.isFile(path);
if(flag){
System.out.println("文件存在");
//读文件
op.readFile(path);
//复制图片
op.copyImage(imgPath, imgNewPath);
//复制excel
op.copyImage(xlsPath, xlsNewPath);
}else{
boolean f = op.createFile(path);
if(f){
System.out.println("文件生成,请在控制台写入字符串。");
//指定文件写入内容
op.writeFile(path);
}else{
System.out.println("文件生成失败");
}
}
}
}