1.基本概念
按数据类型分:Java有字节流和字符流两种:
字节流:InputStream/OutputStream
字符流:Reader/Writer
字节流读取的时候,读到一个字节就返回一个字节;
字符流使用了字节流读到一个或多个字节(中文对应的字节是两个,UTF-8码表中是3个)时,先去查指定的编码表,将查到的字符返回。
2.常用子类
字节流都是以InputStream/OutputStream/Stream结尾,InputStream/OutputStream前面代表这个流的作用,它们都继承了InputStream/OutputStream,
比如FileInputStream/FileInputStream表示操作文件内容的字节流,PrintStream表示用于格式化打印的字节流;
字符流都是以Reader/Writer结尾,Reader/Writer前面代表这个流的作用,它们都继承了Reader/Writer,
比如FileReader/FileWriter表示操作文件内容的字符流,PrintWriter表示用于格式化打印的字符流。
2.使用场合
字节流可以处理所有类型数据,如图片、MP3等;
字符流只能处理字符数据。
下面是一个使用字节流和字符流实例:
FileIO.java文件:
package com.swsx.io; import java.io.File; /** * 用于文件输入输出操作的接口 * @author Administrator * */ public interface FileIO { String CLASS_PATH = System.getProperty("java.class.path") + File.separator; /** * 换行符 */ String LINE_SEPARATOR = System.getProperty("line.separator"); /** * 建立一个文件 * @param fileName * @return */ boolean createFile(String fileName); /** * 删除一个文件 * @param fileName * @return */ boolean deleteFile(String fileName); /** * 将内容写入到文件中 * @param fileName * @param content */ void writeFile(String fileName, String content); /** * 将内容写入到文件中 * @param fileName * @param content * @param append 内是否追加 */ void writeFile(String fileName, String content, boolean append); /** * 读文件中的内容 * @param fileName * @return */ String readFile(String fileName); }
AbstractFileIO.java文件:
package com.swsx.io; import java.io.File; import java.io.IOException; /** * 实现FileIO的抽象类 * @author Administrator * */ public abstract class AbstractFileIO implements FileIO { public AbstractFileIO() { // TODO Auto-generated constructor stub } @Override public boolean createFile(String fileName) { // TODO Auto-generated method stub if(fileName != null && !fileName.equals("")) { File file = new File(CLASS_PATH + fileName); try { return file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return false; } @Override public boolean deleteFile(String fileName) { // TODO Auto-generated method stub if(fileName != null && !fileName.equals("")) { File file = new File(CLASS_PATH + fileName); return file.delete(); } return false; } @Override public void writeFile(String fileName, String content) { // TODO Auto-generated method stub writeFile(fileName, content, false); } @Override public abstract void writeFile(String fileName, String content, boolean append); @Override public abstract String readFile(String fileName); }
FileStreamIO.java文件:
package com.swsx.io; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; /** * 文件输入输出操作字节流实现 * @author Administrator * */ public class FileStreamIO extends AbstractFileIO { public FileStreamIO() { super(); // TODO Auto-generated constructor stub } @Override public void writeFile(String fileName, String content, boolean append){ if(fileName != null && content != null && !fileName.equals("")) { byte[] b = content.getBytes(); OutputStream output = null; try { output = new FileOutputStream(CLASS_PATH + fileName, append); output.write(b); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(output != null){ try { output.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } //方法一:使用一个事先定好长度的字节数组保存全部文件内容(可能溢出) // @Override // public String readFile(String fileName){ // if(fileName != null && !fileName.equals("")){ // File file = new File(CLASS_PATH + fileName); // InputStream input = null; // byte[] b = new byte[1024];//文件内容全部读到这里 // try { // input = new FileInputStream(file); // int temp = 0; // int count = 0; // while((temp = input.read()) != -1){ // b[count++] = (byte)temp;//文件过大时,可能会溢出 // } // return new String(b); // } catch (FileNotFoundException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // }finally{ // if(input != null){ // try { // input.close(); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } // } // } // return null; // } //方法二:使用ByteArrayOutputStream生成一个存储文件内容的字节数组(可以避免内容溢出) @Override public String readFile(String fileName){ if(fileName != null && !fileName.equals("")){ File file = new File(CLASS_PATH + fileName); InputStream input = null; //只是一个缓冲区 文件全部内容读到ByteArrayOutputStream中 byte[] b = new byte[16]; ByteArrayOutputStream out = new ByteArrayOutputStream(); try { input = new FileInputStream(file); int len = 0; while((len = input.read(b)) != -1){ out.write(b, 0, len);//ByteArrayOutputStream不会溢出 } return new String(out.toByteArray());//将ByteArrayOutputStream中的内容转化为字节数组 } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(input != null){ try { input.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } return null; } /** * 格式化写文件 * @param fileName * @param append * @param format * @param args */ public static void PrintFile(String fileName, boolean append, String format, Object...args){ if(fileName != null && format != null && !fileName.equals("")){ PrintStream pt = null; try { pt =new PrintStream(new FileOutputStream(CLASS_PATH + fileName, append)); pt.printf(format, args); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(pt != null){ pt.close(); } } } } }
FileCharacterIO.java文件:
package com.swsx.io; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; /** * 文件输入输出操作字符流实现 * @author Administrator * */ public class FileCharacterIO extends AbstractFileIO { public FileCharacterIO() { super(); // TODO Auto-generated constructor stub } @Override public void writeFile(String fileName, String content, boolean append) { // TODO Auto-generated method stub if(fileName != null && content != null && !fileName.equals("")) { Writer out = null; try { out = new FileWriter(CLASS_PATH + fileName, append); out.write(content); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(out != null){ try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } //方法一:使用一个事先定好长度的字符数组保存全部文件内容(可能溢出) // @Override // public String readFile(String fileName) { // // TODO Auto-generated method stub // if(fileName != null && !fileName.equals("")){ // File file = new File(CLASS_PATH + fileName); // Reader in = null; // char[] c = new char[1024];//文件内容全部读到这里 // try { // in = new FileReader(file); // int temp = 0; // int count = 0; // while((temp = in.read()) != -1){ // c[count++] = (char)temp;//文件过大时,可能会溢出 // } // // return new String(c); // } catch (FileNotFoundException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // }finally{ // if(in != null){ // try { // in.close(); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } // } // } // return null; // } //方法二:使用StringBuffer或StringBuilder存储文件内容(可以避免内容溢出) @Override public String readFile(String fileName) { // TODO Auto-generated method stub if(fileName != null && !fileName.equals("")){ File file = new File(CLASS_PATH + fileName); Reader in = null; //只是一个缓冲区 文件全部内容读到StringBuffer或StringBuilder中 char[] c = new char[16]; //StringBuffer或StringBuilder存储文件内容 StringBuffer strBuffer = new StringBuffer(); try { in = new FileReader(file); int len = 0; while((len = in.read(c)) != -1){ strBuffer.append(c, 0, len);//不会溢出 } return strBuffer.toString(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(in != null){ try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } return null; } }
TestFileIO.java文件:
package com.swsx.io; public class TestFileIO { public static void main(String[] agrs){ FileIO fileIO = new FileCharacterIO(); String fileName = "test1.txt"; if(fileIO.createFile(fileName)){ System.out.println("success"); } String content = "你好!" + FileIO.LINE_SEPARATOR + "hello" + FileIO.LINE_SEPARATOR; fileIO.writeFile(fileName,content); FileStreamIO.PrintFile(fileName,true, "姓名:%s 年龄:%d%s", "张三", 3, FileIO.LINE_SEPARATOR); System.out.println(fileIO.readFile(fileName)); fileIO.deleteFile(fileName); } } //输出: //success //你好! //hello //姓名:张三 年龄:3