文件字符输入流,只能读取普通文本。
读取文本内容时,比较方便,快捷。
FileReader reader = null; try { //创建文件字符输入流 reader = new FileReader("temp.txt"); //开始读 char[] chars = new char[4]; int readCount = 0; while ((readCount = reader.read(chars)) != -1){ System.out.print(new String(chars,0,readCount)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } }skip方法用法和之前相同
文件字符输出流。写
只能输出普通文本文件 能用记事本打开编辑的都是普通文本文件
FileWriter writer = null; try { writer = new FileWriter("temp.txt", true); char[] chars = {'/','/','看','我','扎','不','扎','你','就','完','事','了', '昂'}; //可以传入char数组,char数组一部分也可以,也可以传入字符串 writer.write(chars); writer.flush(); } catch (IOException e) { e.printStackTrace(); }finally { if (writer != null) { try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } }