黑马程序员 Java基础 IO2
来源:互联网 发布:java打印 编辑:程序博客网 时间:2024/06/06 00:54
一 字节流
1 FileOutputStream的: (BufferedOutputStream 同他)
void write(byte[] b)
将 b.length 个字节从指定 byte 数组写入此文件输出流中。
void write(byte[] b, int off, int len)
将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。
void write(int b)
将指定字节写入此文件输出流
2 FileInputStream的:
int read()
从此输入流中读取一个数据字节。
int read(byte[] b)
从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
(如果不成功,均返回 -1 )
3
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bos.txt"));
二 案例
代码1:
复制文本文件、复制图片、复制视频(4种实现)
public static void method1(String srcString, String destString) throws IOException{FileOutputStream fos = new FileOutputStream(destString);FileInputStream fis = new FileInputStream(srcString); int by = 0;while((by = fis.read()) != -1){fos.write(by);} fis.close();fos.close();} public static void method2(String srcString, String destString) throws IOException{FileInputStream fis = new FileInputStream(srcString);FileOutputStream fos = new FileOutputStream(destString); byte[] bys = new byte[1024];int len = 0;while((len = fis.read(bys)) != -1){fos.write(bys, 0, len);} fis.close();fos.close();} public static void method3(String srcString, String destString) throws IOException{BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcString));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destString)); int by = 0;while((by = bis.read()) != -1){bos.write(by);} bis.close();bos.close();} public static void method4(String srcString, String destString) throws IOException{BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destString));BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcString)); byte[] bys = new byte[1024];int len = 0;while((len = bis.read(bys)) != -1){bos.write(bys, 0, len);} bos.close();bis.close();}
三 字符流
4 FileWriter的: ( OutputStreamWriter 同它 )
还可以
public void write(int c):写一个字符
public void write(char[] cbuf):写一个字符数组
public void write(char[] cbuf,int off,int len):写一个字符数组的一部分
public void write(String str):写一个字符串
public void write(String str,int off,int len):写一个字符串的一部分
这里是char[] 和之前的不一样的。
5 FileReader的: ( InputStreamReader同它 )
int read()
读取单个字符。
int read(char[] cbuf)
将字符读入数组中的某一部分。
6 BufferedReader的:
多了一个:
String readLine()
读取一个文本行
7 BufferedWriter的:
BufferedWriter:
public void newLine():根据系统来决定换行符
四 案例
* 复制文本文件 有5种方式
*
* 分析:
* 复制数据,如果我们知道用记事本打开并能够读懂,就用字符流,否则用字节流。
*/
private static void method1(String srcString, String destString) throws IOException{FileReader fr = new FileReader(srcString);FileWriter fw = new FileWriter(destString); int ch = 0;while((ch = fr.read()) != -1){fw.write(ch);} fw.close();fr.close();} private static void method2(String srcString, String destString) throws IOException{FileWriter fw = new FileWriter(destString);FileReader fr = new FileReader(srcString); char[] chs = new char[1024];int len = 0;while((len = fr.read(chs)) != -1){fw.write(chs, 0, len);} fr.close();fw.close();} private static void method3(String srcString, String destString) throws IOException{BufferedWriter bw = new BufferedWriter(new FileWriter(destString));BufferedReader br = new BufferedReader(new FileReader(srcString)); int ch = 0;while((ch = br.read()) != -1){bw.write(ch);} bw.close();br.close();} private static void method4(String srcString, String destString) throws IOException{BufferedReader br = new BufferedReader(new FileReader(srcString));BufferedWriter bw = new BufferedWriter(new FileWriter(destString)); char[] chs = new char[1024];int len = 0;while((len = br.read(chs)) != -1){bw.write(chs, 0, len);} br.close();bw.close();} private static void method5(String srcString, String destString) throws IOException{BufferedWriter bw = new BufferedWriter(new FileWriter(destString));BufferedReader br = new BufferedReader(new FileReader(srcString)); String line = null;while((line = br.readLine()) != null){bw.write(line);bw.newLine();bw.flush();} bw.close();br.close();}
五 转换流及其他
8 OutputStreamWriter的:
A:OutputStreamWriter
OutputStreamWriter(OutputStream os)
B:InputStreamReader
InputStreamReader(InputStream is)
Reader
|--InputStreamReader
|--FileReader
|--BufferedReader
Writer
|--OutputStreamWriter
|--FileWriter
|--BufferedWriter
Day21总结中提到的案例没有都练完,以后可以继续看
字符流的由来:
其实就是:字节流读取文字字节数据后,不直接操作而是先查指定的编码表,获取对应的文字。再对这个文字进行操作。简单说:字节流+编码表。
六 装饰设计模式(懂了这个就好理解上面的转换流等为什么那么用了)
装饰设计模式:
当想要对已有的对象进行功能增强时,可以定义类,将已有对象传入,基于已有的功能,并提供加强功能。
代码1:示例
public interface Phone {public abstract void call();} public abstract class PhoneDecorate implements Phone {private Phone p; public PhoneDecorate(Phone p) {this.p = p;} public void call() {this.p.call();}} public class MusicPhoneDecorate extends PhoneDecorate {public MusicPhoneDecorate(Phone p) {super(p);} public void call() {super.call();System.out.println("手机可以听音乐");}} class RingPhoneDecorate extends PhoneDecorate {public RingPhoneDecorate(Phone p) {super(p);} public void call() {System.out.println("手机可以听彩铃");super.call();}} public static void main(String[] args) {// 需求:我想在接电话前,听彩铃PhoneDecorate pd = new RingPhoneDecorate(p);pd.call(); // 需求:我想在接电话后,听音乐pd = new MusicPhoneDecorate(p);pd.call(); // 需求:我要想手机在接前听彩铃,接后听音乐pd = new RingPhoneDecorate(new MusicPhoneDecorate(p));pd.call();// 这个思路真的很好 // 可以想到有我们曾经有过类似的用法BufferedReader br = new BufferedReader(new InputStreamReader(System.in));BufferedWriter bw = new BufferedWriter((new OutputStreamWriter(System.out))); Scanner sc = new Scanner(System.in);}
可以想到有我们曾经有过类似的用法,比如:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));BufferedWriter bw = new BufferedWriter((new OutputStreamWriter(System.out)));Scanner sc = new Scanner(System.in);
感觉以后也可以返回来继续体悟这个设计模式,因为感觉真的很棒!
七 Properties
1 是一个集合类,Hashtable的子类
2 特有功能
A:public Object setProperty(String key,String value)
B:public String getProperty(String key)
C:public Set<String> stringPropertyNames()
3 案例
代码1: 同一般的map的使用
public static void main(String[] args) {Properties prop = new Properties(); prop.put("it002", "hello");prop.put("it001", "world");prop.put("it003", "java"); Set<Object> set = prop.keySet();for(Object key: set){Object value = prop.get(key);System.out.println(key + "---" + value);}}
代码2:
public static void main(String[] args) {Properties prop = new Properties(); prop.setProperty("张三", "30");prop.setProperty("李四", "40");prop.setProperty("王五", "50"); Set<String> set = prop.stringPropertyNames();for(String key: set){String value = prop.getProperty(key);System.out.println(key + " " + value);}}
* setProperty() 调用了 put()函数
且setProperty()只能存(String, String)的对
代码3:
public static void main(String[] args) throws IOException {// myStore();myLoad();} private static void myStore() throws IOException{Properties prop = new Properties(); prop.setProperty("林青霞", "27");prop.setProperty("武鑫", "30");prop.setProperty("刘晓曲", "18"); Writer w = new FileWriter("e:\\a.txt", true);prop.store(w, "helloworld");w.close();} private static void myLoad() throws IOException{Properties prop = new Properties(); Reader r = new FileReader("e:\\b.txt");prop.load(r); // 注意:这个文件的数据必须是键值对形式r.close(); System.out.println("prop:" + prop);}
* Properties集合 本质上是一个特殊的集合,但其有特殊的用法
4 和IO流结合的方法
public void load(Reader reader)
public void load(InputStream inStream)
public void store(Writer writer,String comments)
public void store(OutputStream out,String comments)
代码示例如上面的代码3所示
- 黑马程序员 Java基础 IO2
- 黑马程序员—Java基础—IO2
- 黑马程序员---Java学习笔记之io2
- 黑马程序员———IO2
- java io2
- Java IO2
- 黑马程序员---IO2(缓冲区、装饰设计模式、转换流)
- 黑马程序员------java基础
- 黑马程序员-Java基础
- 黑马程序员----JAVA基础
- 黑马程序员--Java基础
- 黑马程序员-Java基础
- 黑马程序员-------Java基础
- 黑马程序员--java基础
- 黑马程序员-java-基础
- 黑马程序员--java基础
- 黑马程序员-Java基础
- 黑马程序员-java基础
- 连接池简介
- 模拟音乐播放器-plist的使用-懒加载-NSBundle全路径的使用
- swift -类的定义及使用
- 离开中心村进国企——北漂18年(21)
- Hibernate学习笔记(八) — 懒加载与抓取策略
- 黑马程序员 Java基础 IO2
- swift -UIView的使用
- PL/SQL 如何导出INSERT语句
- Spring学习笔记之六(数据源的配置)
- swift -NavigationController,代理传值
- 程序逻辑抽取器-linux之内核源码逆向设计
- int long long __int64的位数
- 黑马程序员 Java基础 多线程
- 20150814Oracle学习笔记