java的IO流分两种流
字节流 InputStream OutputStream
字符流 Reader Writer
他们都是抽象类
具体实现
字节流 FileInputStream FileOutputStream
字符流 FileReader FileWriter
字符流处理的单元为2个字节的Unicode字符,分别操作字符、字符数组或字符串,而字节流处理单元为1个字节,操作字节和字节数组。所以字符流是由Java虚拟机将字节转化为2个字节的Unicode字符为单位的字符而成的,所以它对多国语言支持性比较好!如果是音频文件、图片、歌曲,就用字节流好点,如果是关系到中文(文本)的,用字符流好点.
所有文件的储存是都是字节(byte)的储存,在磁盘上保留的并不是文件的字符而是先把字符编码成字节,再储存这些字节到磁盘。在读取文件(特别是文本文件)时,也是一个字节一个字节地读取以形成字节序列.
字节流可用于任何类型的对象,包括二进制对象,而字符流只能处理字符或者字符串; 2. 字节流提供了处理任何类型的IO操作的功能,但它不能直接处理Unicode字符,而字符流就可以。
字节流转换成字符流可以用 InputSteamReader OutputStreamWriter
转换成BufferdReader BufferedWriter 他们具有缓冲区
例如:读取文件 从字节流输入到字符流输入
定义一个字节流:
// 定义一个指向D:/TEXT.TXT 的字节流
FileInputStream fileInputStream = new FileInputStream("d:/text.txt");
//字节流转换成InputStreamReader
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
//InputStreamReader 转换成带缓存的bufferedReader
BufferedReader bufferedReader = new BufferedReader(inputSteamReader);
Java的IO流分为字符流(Reader,Writer)和字节流(InputStream,OutputStream),字节流顾名思义字节流就是将文件的内容读取到字节数组,然后再输出到另一个文件中。而字符流操作的最小单位则是字符。可以先看一下IO流的概述:
动态获取文件长度创建接收数组,系统自动判定转义目录斜杠,讲述字节流、字符流、buffer流三种方式针对text文件的写入、读取、创建、追加等操作
递归创建上层目录
public static void mkDir(File file){
if(file.getParentFile().exists()){
file.mkdir();
}else{
mkDir(file.getParentFile());
file.mkdir();
}
文件工具类如下
import java.io.File;
import java.io.IOException;
public class FileUtils {
public static final String encode = "UTF-8";
/**获取目录下的所有文件和目录列表完整地址输出*/
public static void nowFileLists(String fileName) throws IOException{
File f=new File(fileName);
File[] str=f.listFiles();
for (int i = 0; i < str.length; i++) {
System.out.println(str[i]);
}
}
/**判断文件是否存在*/
public static boolean exitsFile(String fileName) throws IOException{
File file = new File(fileName);
boolean flag = false;
if(file.exists()){
flag = true;
}
return flag;
}
/**判断是否是文件夹*/
public static boolean isDirectory(String fileName) throws IOException{
File file = new File(fileName);
boolean flag = false;
//判断是否是目录 只有当文件夹存在的情况才能判断
if(exitsFile(fileName)){
if(file.isDirectory()){
flag=true;
}
}
return flag;
}
/**创建文件*/
public static void createFile(String fileName) throws IOException{
File file = new File(fileName);
file.createNewFile();
}
}
字节流操作文件
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.cn.utils.FileUtils;
public class ByteFiles {
/**
* 字节流操作文件
*/
public static void main(String [] args){
String fileName = "D:"+File.separator+"byteFile.txt";
try {
if(!FileUtils.exitsFile(fileName)){
FileUtils.createFile(fileName);
}
writeContext(fileName);
redaContext(fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
//写入指定格式的文件内容
public static void writeContext(String fileName) throws IOException{
File file = new File(fileName);
OutputStream outs = new FileOutputStream(file,true);
String content = "\r\n窗前明月光";
outs.write(content.getBytes("UTF-8"));
outs.close();
}
//写入指定格式的文件内容
public static void redaContext(String fileName) throws IOException{
File file = new File(fileName);
InputStream in = new FileInputStream(file);
byte b [] = new byte [(int) file.length()];
//字节流读取文件方式一全部读取
in.read(b);
in.close();
String content = new String(b);
System.out.println("byte方式一全部读取:\r\n"+content);
//字节流读取文件方式二 逐行读取
InputStream inn = new FileInputStream(file);
byte bb [] = new byte [(int) file.length()];
int count =0;
int temp=0;
while((temp=inn.read())!=(-1)){
bb[count++]=(byte)temp;
}
inn.close();
content = new String(bb);
System.out.println("byte方式二全部读取:\r\n"+content);
}
}
字符流操作文件
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import com.cn.utils.FileUtils;
public class CharFiles {
/**
* 字符流操作文件
*/
public static void main(String [] args){
String fileName = "D:"+File.separator+"charFile.txt";
try {
if(!FileUtils.exitsFile(fileName)){
FileUtils.createFile(fileName);
}
writeContext(fileName);
redaContext(fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
//写入指定格式的文件内容
public static void writeContext(String fileName) throws IOException{
File file = new File(fileName);
OutputStream outs = new FileOutputStream(file,true);
OutputStreamWriter out = new OutputStreamWriter(outs,"UTF-8");
String content = "\r\n我是字符流写入";
out.write(content);
out.close();
}
//写入指定格式的文件内容
public static void redaContext(String fileName) throws IOException{
File file = new File(fileName);
InputStreamReader inr = new InputStreamReader(new FileInputStream(file),"UTF-8");
char b [] = new char [(int) file.length()];
//字符流读取文件方式一全部读取
inr.read(b);
inr.close();
String content = new String(b);
System.out.println("char方式一全部读取:\r\n"+content);
//字符流读取文件方式二 逐行读取
InputStreamReader inrr = new InputStreamReader(new FileInputStream(file),"UTF-8");
char bb [] = new char [(int) file.length()];
int count =0;
int temp=0;
while((temp=inrr.read())!=(-1)){
bb[count++]=(char)temp;
}
inrr.close();
content = new String(bb);
System.out.println("char方式二全部读取:\r\n"+content);
}
}
使用buffer流操作文件
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.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import com.cn.utils.FileUtils;
public class BufferReadWriteFile {
public static void main(String[] args) {
String fileName = "D:"+File.separator+"buffer.txt";
try {
if(!FileUtils.exitsFile(fileName)){
FileUtils.createFile(fileName);
}
bufferWite(fileName);
bufferRead(fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
//使用buffer流进入写入文件
public static void bufferWite(String fileName){
OutputStreamWriter outt;
try {
outt = new OutputStreamWriter(new FileOutputStream(fileName,true),"UTF-8");
BufferedWriter wites = new BufferedWriter(outt);
wites.write("\r\n我是buffer流文件操作");
wites.close();
} catch (UnsupportedEncodingException e2) {
e2.printStackTrace();
} catch (FileNotFoundException e2) {
e2.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//使用buffer流进入读取文件
public static void bufferRead(String fileName){
BufferedReader reader = null;
InputStreamReader inputFileReader = null;
String content = "";
List
String tempString = null;
try {
inputFileReader = new InputStreamReader(new FileInputStream(fileName),"UTF-8");
reader = new BufferedReader(inputFileReader);
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
content += "\r\n"+tempString;
readContextList.add(tempString);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
System.out.println(content);
}
}