黑马程序员---IO流
2014-02-19 20:19
274 查看
----------------------
ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------
String
StringBuffer 同步安全,效率比之StringBuilder低,可变字符序列
StringBuilder 不同步,效率高,可变字符序列。
String str=”1”+”2”+”3”+”4”;
会分别在字符串池中创建“12””123” “1234”多出2个不需要的字符串。而StringBuilder与StringBuffer对象通过append(String)添加字符串只会在字符串中创建一个字符串。
IO流
4000
流向分为:输出流(写入),输入流(读取) 流操作数据分为:字符流,字节流
字符流抽象基类:Reader Writer 字节流抽象基类:InputStream OutputStream
流对象一旦创建,必须close()流。关闭流后,流就不能进行任何操作了。好比男女朋友一样,一旦创建恋爱关系,当不爱时也得说一句分手,得告诉对方咱们不是男女朋友关系了,彼此自由了。但你我就不要再有任何联系了。
字节输入流中的available()判断读取文件的大小
flush():当输出流使用到缓冲区的时候需要在write()后flush()
//字符输入流
//复制文件1
//复制文件2(自定义缓冲区)
缓冲:BufferedReader (子类 LineNumberReader跟踪行号的缓冲字符输入流) BufferedWriter BufferedOutputStream BufferedInputStream 装饰设计模式
装饰设计模式
需对某对象的功能进行扩展,可以使用装饰设计模式而非使用继承。因为只对某功能进行扩展,使用继承的话,子类会多出没必要进行扩展的功能会使得整个类体系杂乱臃肿。装饰比继承灵活。装饰类和被装饰类都必须实现同一接口或所属同一父类。
默认的输入设备和输出设备的流不需要关,因为只有一个流。InputStream in=System.in;in.read();in.close();
转换流:InputStreamReader 字节流转换为字符流 此过程称为解码
OutputStreamWriter 字符流转换为字节流 此过程称为编码
//遍历目录
//删除文件夹及文件
//将properties数据存储到文件中
//文件合并
//文件切割
//文件过滤及删除
//对象流 ObjectOutputStream ObjectInputStream
//编码
中文在GBK中2个字节,UTF—8中3个字节
----------------------
ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net
ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------
String
StringBuffer 同步安全,效率比之StringBuilder低,可变字符序列
StringBuilder 不同步,效率高,可变字符序列。
String str=”1”+”2”+”3”+”4”;
会分别在字符串池中创建“12””123” “1234”多出2个不需要的字符串。而StringBuilder与StringBuffer对象通过append(String)添加字符串只会在字符串中创建一个字符串。
IO流
4000
流向分为:输出流(写入),输入流(读取) 流操作数据分为:字符流,字节流
字符流抽象基类:Reader Writer 字节流抽象基类:InputStream OutputStream
流对象一旦创建,必须close()流。关闭流后,流就不能进行任何操作了。好比男女朋友一样,一旦创建恋爱关系,当不爱时也得说一句分手,得告诉对方咱们不是男女朋友关系了,彼此自由了。但你我就不要再有任何联系了。
字节输入流中的available()判断读取文件的大小
flush():当输出流使用到缓冲区的时候需要在write()后flush()
//字符输入流
FileReader fileReader=null; String str=""; BufferedReader bufr=null; try { fileReader=new FileReader("abc.txt"); bufr=new BufferedReader(fileReader); while((str=bufr.readLine())!=null){ System.out.println(str); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if (bufr!= null) { try { bufr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }//字符输出流
FileWriter fileWriter = null; BufferedWriter bufw = null; String str = ""; try { fileWriter = new FileWriter("abc.txt"); bufw= new BufferedWriter(fileWriter); bufw.write("234"); bufw.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (bufw != null) { try { bufw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
//复制文件1
private static void copyFile1() throws IOException { FileInputStream fileInputStream = new FileInputStream("123.mp4"); BufferedInputStream bufInputStream = new BufferedInputStream( fileInputStream); FileOutputStream fileOutputStream = new FileOutputStream("321.mp4"); BufferedOutputStream bufOutputStream = new BufferedOutputStream( fileOutputStream); //缓冲区中再次使用缓冲区 int len = 0; byte []by=new byte[1024]; while ((len = bufInputStream.read(by)) != -1) { bufOutputStream.write(by,0,len); bufOutputStream.flush(); } //不在自定义缓冲区了(经过简单测试此比有缓冲区慢) /*int ch=0; while ((ch=bufInputStream.read())!=-1) { bufOutputStream.write(ch); }*/ bufInputStream.close(); bufOutputStream.close(); }
//复制文件2(自定义缓冲区)
private static void copyFile2() throws IOException { FileInputStream fileInputStream=new FileInputStream("123.mp4"); FileOutputStream fileOutputStream=new FileOutputStream("321.mp4"); int len=0; byte[]by=new byte[1024]; while((len=fileInputStream.read(by))!=-1){ fileOutputStream.write(by, 0, len); fileOutputStream.flush(); } fileInputStream.close(); fileOutputStream.close(); }
缓冲:BufferedReader (子类 LineNumberReader跟踪行号的缓冲字符输入流) BufferedWriter BufferedOutputStream BufferedInputStream 装饰设计模式
装饰设计模式
需对某对象的功能进行扩展,可以使用装饰设计模式而非使用继承。因为只对某功能进行扩展,使用继承的话,子类会多出没必要进行扩展的功能会使得整个类体系杂乱臃肿。装饰比继承灵活。装饰类和被装饰类都必须实现同一接口或所属同一父类。
默认的输入设备和输出设备的流不需要关,因为只有一个流。InputStream in=System.in;in.read();in.close();
转换流:InputStreamReader 字节流转换为字符流 此过程称为解码
OutputStreamWriter 字符流转换为字节流 此过程称为编码
//遍历目录
private static void fileList(File file) throws IOException { if (file.isDirectory()) { File[] strings = file.listFiles(); for (File string1 : strings) { if (string1.isDirectory()) { fileList(string1); } else { System.out.println(string1.getName()); } } } }
//删除文件夹及文件
private static void fileList(File file) throws IOException { if (file.isDirectory()) { File[] strings = file.listFiles(); for (File string1 : strings) { /*if (string1.isDirectory()) { fileList(string1); } else { System.out.println(string1.getName()); }*/ if(string1.isDirectory()){ fileList(string1); }else{ string1.delete(); } } file.delete(); } }
//将properties数据存储到文件中
Properties properties=new Properties(); properties.setProperty("123", "abc"); properties.setProperty("345", "efg"); properties.setProperty("567", "hjk"); OutputStream outputStream=new FileOutputStream("info.txt"); properties.store(outputStream, ""); outputStream.close();
//文件合并
ArrayList<FileInputStream> alArrayList=new ArrayList<FileInputStream>(); alArrayList.add(new FileInputStream("1.txt")); alArrayList.add(new FileInputStream("2.txt")); alArrayList.add(new FileInputStream("3.txt")); Enumeration<FileInputStream> en=Collections.enumeration(alArrayList); SequenceInputStream sequenceInputStream=new SequenceInputStream(en); FileOutputStream fileOutputStream=new FileOutputStream("4.txt"); byte []by=new byte[1024]; int len=0; while((len=sequenceInputStream.read(by))!=-1){ fileOutputStream.write(by,0,len); } sequenceInputStream.close(); fileOutputStream.close();
//文件切割
FileInputStream fileInputStream=new FileInputStream("38.mp4"); FileOutputStream fileOutputStream=null; int len=0; byte[]by=new byte[1024*1000];//1M int count=0; while((len=fileInputStream.read(by))!=-1){ count++; fileOutputStream=new FileOutputStream(count+".part"); fileOutputStream.write(by,0, len); } fileInputStream.close(); fileOutputStream.close();
//文件过滤及删除
File file = new File("E:\\Eclipse\\exam");//此处得是目录,否则异常 得先判断
if(file.isDirectory()){
File[] f = file.listFiles(new MyFileFilter());//此处可以使用内部类避免单独创建一个过滤类 for (File file2 : f) { System.out.println(file2.toString()); file2.delete(); }
}
//过滤器
public class MyFileFilter implements FilenameFilter { @Override public boolean accept(File dir, String name) { // TODO Auto-generated method stub return name.endsWith(".part"); } }
//对象流 ObjectOutputStream ObjectInputStream
ObjectOutputStream objOutputStream = new ObjectOutputStream( //写入 new FileOutputStream("123.txt")); objOutputStream.writeObject(new Person(12, "jeck")); objOutputStream.flush(); objOutputStream.close(); //读取 ObjectInputStream objectInputStream = new ObjectInputStream( new FileInputStream("123.txt")); Person person = (Person) objectInputStream.readObject(); System.out.println(person.getAge() + ":" + person.getName()); objectInputStream.close();
//编码
中文在GBK中2个字节,UTF—8中3个字节
String string="123中午";//编码 byte[]b=string.getBytes("GBK"); for (byte c : b) { System.out.println(c); }
----------------------
ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net
相关文章推荐
- 黑马程序员——IO流(二)
- 黑马程序员-Java基础学习-IO流
- 黑马程序员-JAVA基础-IO流其他类
- 黑马程序员学习日记--IO流技术(二)
- 黑马程序员_IO流总结
- 黑马程序员---java基础---IO流总结
- 黑马程序员——IO流字符流
- 黑马程序员_IO流
- 黑马程序员————Java的IO流的详解
- 黑马程序员-基础IO流(其他流)
- 黑马程序员-Java基础总结11——IO流(二)
- 黑马程序员——IO流
- 黑马程序员-Java中IO流知识点总结
- 黑马程序员-基础-IO流1
- 黑马程序员__JAVA基础__IO流(二)
- 黑马程序员——io流2(其它流)
- 黑马程序员--Java学习日记之复习总结(递归以及IO流概述)
- 黑马程序员-java_基础加强_IO流
- 黑马程序员—IO流之Properties小练习
- 黑马程序员 IO流