IO 字符流学习
2015-10-01 20:10
357 查看
import java.awt.Frame; import java.io.*; public class filewriter { /** * @param args */ public static void main(String[] args) throws IOException{ // TODO Auto-generated method stub //test1(); test2(); } static void test1()throws IOException{ FileWriter fw = new FileWriter("F:\\fd.txt"); fw.write("sfasf"); fw.flush(); fw.write("zzzzz"); fw.flush(); fw.close(); fw = new FileWriter("F:\\fd.txt", true); fw.write("cccc"); fw.flush(); fw.close(); } static void test2(){ FileWriter fw = null; try { fw = new FileWriter("F:\\fd2.txt"); fw.write("wzz"); } catch (IOException e) { // TODO: handle exception System.out.println(e.toString()); } finally{ try { if(fw!=null) fw.close(); } catch (IOException e2) { // TODO: handle exception System.out.println(e2.toString()); } } } }
import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class filereader { /** * @param args */ public static void main(String[] args)throws IOException { // TODO Auto-generated method stub //test1() //test2(); //test3(); //test4(); test5(); } static void test1()throws IOException{ FileReader fr = new FileReader("F:\\fd.txt"); int c; while((c = fr.read()) != -1){ System.out.println((char)c); } fr.close(); } static void test2()throws IOException{ FileReader fr = new FileReader("F:\\fd.txt"); char[] buf = new char[3]; int x; while((x = fr.read(buf)) != -1){ System.out.println(buf); } fr.close(); } static void test3()throws IOException{ FileReader fReader = new FileReader("F:\\fd.txt"); char[] buf = new char[2]; int x; while((x = fReader.read(buf))!= -1){ System.out.print(new String(buf, 0, x)); } } static void test4()throws IOException{ FileWriter fw = new FileWriter("F:\\fd3.txt"); FileReader fr = new FileReader("F:\\fd2.txt"); int x; while((x = fr.read())!=-1) { fw.write(x); } fw.close(); fr.close(); } static void test5(){ FileWriter fw = null; FileReader fr = null; try { fw = new FileWriter("F:\\fd3.txt"); fr = new FileReader("F:\\fd2.txt"); int len = 0; char[] buf = new char[1024]; while((len = fr.read(buf)) != -1){ fw.write(buf, 0, len); } } catch (Exception e) { // TODO: handle exception System.out.println(e.toString()); } finally{ try { if(fw != null) fw.close(); } catch (IOException e2) { // TODO: handle exception System.out.println(e2.toString()); } try { if(fr!=null) fr.close(); } catch (IOException e2) { // TODO: handle exception System.out.println(e2.toString()); } } } }
FileWriter FileReader 文本的续写 文本的读取 文本的拷贝
BuffWriter Buffreader 缓冲
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class bufferwrier { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub test1(); } static void test1(){ BufferedReader bfr = null; BufferedWriter bfw = null; try { bfr = new BufferedReader(new FileReader("F:\\fd.txt")); bfw = new BufferedWriter(new FileWriter("F:\\wzz.txt")); String s = null; while((s = bfr.readLine())!=null){ bfw.write(s); bfw.newLine(); bfw.flush(); } } catch (IOException e) { // TODO: handle exception throw new RuntimeException("keng"); } finally{ try { if(bfr!=null) bfr.close(); } catch (IOException e2) { // TODO: handle exception throw new RuntimeException("keng"); } try { if(bfw!=null) bfw.close(); } catch (IOException e) { // TODO: handle exception throw new RuntimeException("keng"); } } } }
装饰设计模式:
当想要对已有的对象进行功能增强时。
能够定义类,将已有对象传入。基于已有的功能。并提供加强功能。
那么自己定义的该类称为装饰类
装饰类一般会通过构造方法接受被装饰的对象。
并基于被装饰的对象的功能。提供更强的功能。
装饰模式比继承要灵活,避免了继承体系臃肿。
并且减少了类与类之间的关系。
装饰类由于增强已有对象,具备的功能和已有的是同样的。仅仅只是提供了更强功能。
因此,装饰装修通常属于制度
相关文章推荐
- grep命令
- linux个人学习笔记---ubuntu14.10删除播放器历史记录命令
- Linux程序包管理
- C语言之进制、位运算符、数组
- Linux<一>:Linux启动及登陆
- NOIP快速幂复习
- 拿到对方公钥可信证书过程
- 关于APP中,广告栏实现目视循环播放的效果
- Linux应用总结:自动删除n天前日志
- python的timer带参数传递
- RADI磁盘组织形式、工作方式
- JavaScript跨域总结与解决办法
- cordova 3.0 配置
- CSS轮廓,突出元素
- Request的getParameter和getAttribute方法的区别
- 【错误处理】CentOS命令登录MySQL时,报错ERROR 1045 (28000):
- java gui
- OC基础—多态(超级简单)
- 读取数量不定的输入数据
- 黑马程序员-----网络编程