Java中的File,IO流
IO流体系
字节流: InputStream |--FileInputStream |--FilterInputStream |--BufferedInputStream |--DataInputStream |--ByteArrayInputStream |--ObjectInputStream |--SequenceInputStream |--PipedInputStream OutputStream |--FileOutputStream |--FilterOutputStream |--BufferedOutputStream |--DataOutputStream |--ByteArrayOutputStream |--ObjectOutputStream |--PipedOutputStream |--PrintStream 字符流: Reader |--BufferedReader: |--LineNumberReader |--CharArrayReader |--StringReader |--InputStreamReaer |--FileReader Writer |--BufferedWriter |--CharArrayWriter |--StringWriter |--OutputStreamWriter |--FileWriter |--PrintWriter
读写UTF-8文件
1. 使用转换流 InputStreamReader、OutputStreamWriter
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/** 转换流:字节流+编码表。 转换流的子类:FileReader,FileWriter:字节流+本地默认码表(GBK)。 如果操作文本文件使用的本地默认编码表完成编码。可以使用FileReader,或者FileWriter。因为这样写简便。 如果对操作的文本文件需要使用指定编码表进行编解码操作,这时必须使用转换流来完成。 */ /** * 指定字符编码的写文件 */ @Test public void test01() { BufferedWriter bw = null; try { String filePath = "file/utf8.txt"; Writer out = new OutputStreamWriter(new FileOutputStream(filePath),"UTF-8"); bw = new BufferedWriter(out); bw.write("我爱你,中国"); bw.newLine(); bw.write("I love you, China."); // bw.flush(); // bw.close();//一定要flush,或者close } catch (Exception e) { e.printStackTrace(); } finally { // 关闭资源 try { if (bw != null) bw.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 指定字符编码的方式读文件 */ @Test public void test02() { BufferedReader br = null; try { String filePath = "file/utf8.txt"; Reader in = new InputStreamReader(new FileInputStream(filePath),"UTF-8"); br = new BufferedReader(in); String line=null; while((line = br.readLine())!=null) { System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } finally { // 关闭资源 try { if (br != null) br.close(); } catch (IOException e) { e.printStackTrace(); } } }
2. 使用 InputStreamReader 和 打印流 PrintStream
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//字节打印流 PrintStream public class PrintStreamDemo { public static void main(String[] args) throws IOException { String sFilePath = "file/print.txt"; String sCharSetName = "utf-8"; PrintStream ps = new PrintStream(sFilePath, sCharSetName); ps.write("printStream".getBytes()); ps.println(); ps.print(true); ps.println(" test Println"); ps.println("中文测试"); ps.close(); // BufferedReader bis = new BufferedReader(new FileReader(sFilePath)); //读 utf-8的文件 ,中文乱码,因为默认编码是本地编码即GBK Reader bis = new InputStreamReader(new FileInputStream(sFilePath), sCharSetName);//用指定字符集的方式 int len = -1; char[] buf = new char[1024]; while((len = bis.read(buf)) != -1) { System.out.println(new String(buf, 0, len)); } bis.close(); } }
File类
封装文件和文件夹的类
1. 读写文件(正确关闭资源)
InputStream in = null; OutputStream out = null; try { in = new FileInputStream(file); String newName = file.getName().replace(".java", ".txt"); out = new FileOutputStream(new File(destDir, newName)); // 读并写 byte[] buf = new byte[1024];// 一般 1024 int len = -1; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } } catch (Exception e) { e.printStackTrace(); } finally { try { if(in != null) in.close(); } catch (IOException e) { e.printStackTrace(); } try { if(out != null) out.close(); } catch (IOException e) { e.printStackTrace(); } }
2. 获取当前目录下的所有文件名(不包含子目录下)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// 获取path路径下的所有文件名 public static List<String> getFileNameList(String path, String suffixFilter) { List<String> result = new ArrayList<String>(); List<File> files = getFileListInPath(path, suffixFilter); for (File file : files) { result.add(file.getName()); } return result; } // 获取path路径下的所有文件名(带路径) public static List<String> getFilePathList(String path, String suffixFilter) { List<String> result = new ArrayList<String>(); List<File> files = getFileListInPath(path, suffixFilter); for (File file : files) { result.add(file.getAbsolutePath()); } return result; } public static List<File> getFileListInPath(String path, String suffixFilter) { File folder = new File(path); if (!folder.exists()) { return new ArrayList<File>(); } else { // 若后缀为"" 或 null ,返回所有文件。 if (null == suffixFilter || suffixFilter.isEmpty()) return Arrays.asList(folder.listFiles()); return Arrays.asList(folder.listFiles(new FileFilter() { @Override public boolean accept(File file) { if (file.getName().endsWith(suffixFilter)) return true; return false; } })); } }
----------
常记溪亭日暮,沉醉不知归路。兴尽晚回舟,误入藕花深处。争渡,争渡,惊起一滩鸥鹭。
昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,却道海棠依旧。知否?知否?应是绿肥红瘦。