黑马程序员_IO流
来源:互联网 发布:阿里云 腾讯云 aws 编辑:程序博客网 时间:2024/06/07 23:42
一、基本概念
1、流:Java中对数据的操作都是通过流的方式。
2、流的分类:
(1)、按操作数据分为两种:字节流和字符流。
(2)、按流向分为:输入流和输出流。
3、IO流:用来处理设备之间的数据传输。
二、IO流常用基类:
1、字节流的抽象基类:InputStream,OutputStream。
InputStream常用子类:FileInputStream。
OutputStream常用子类:FileOutputStreamPrintStream, PrintStream。
2、字符流的抽象基类:Reader,Writer。
Reader常用子类:FileReader,LineNumberReader,InputStreamReader,BufferedReader。
Writer常用子类:FileWriter,BufferedWriter,OutputStreamWriter。
三、IO流中需注意的地方:
1、flush和close的区别
flush():仅将缓冲中的数据刷新到目的地。流对象可以继续使用。可以使用多次。close():将缓冲中的数据刷到目的地后,直接关闭流资源,流无法继续使用。只能使用一次,在close()方法当中其实在关闭之前都会调用一次flush()。
2、IO异常的处理规范
(1)、创建流对象。
(2)、在try外创建流对象的引用。
(3)、在try内对流对象进行初始化。
(4)、把close()放在finally{}中,并用try{}catch{}环绕,因为close存在IO异常,在catch中写入相关的代码处理。
(5)、如果在运行时出现了两个异常,问题在于如果流对象没有创建成功,就为null,但finally一定要被执行,就要调用close(),空不能调用close(),就出现了空指 针异常。解决方法是:在finally中的try前加入判断。
3、流的操作规律
(1)、明确源和目的。
源:Reader,InputStream:一定是被读取的
目的:Writer,OutputStream:一定是被写入的
( 2)、明确是否为纯文本。
纯文本:用字符流:Reader,Writer。
非纯文本:用字节流:InputStream,OutputStream。
(3)、明确数据所在的设备
源设备:键盘:System.in
硬盘:文件流File
内存:数组流ArrayStream
目的设备:键盘:System.out
硬盘:文件流File
内存:数组流ArrayStream
(4)、是否需要额外功能。
是否需要高效:buffered:缓冲区
是否需要转换流:InputStreamReader,OutputStreamWriter。
是否操作对象:objectInputstream,objectOutputstream。
四、File类
1、作用:专门用于描述系统中文件或者文件夹的对象,可以用于操作文件或者文件夹的属性信息。
2、File类构造函数
File(String pathname):通过将给定路径名字符串创建一个文件对象。
File(Stringparent, String child):通过父目录和子目录字符串创建文件对象。
File(Fileparent, String child):通过父目录文件对象和子目录字符串创建文件对象。
3、递归
定义:递归就是函数自身调用自身(直接或者间接)
注意:
(1·)、一定要定义条件。 否则就是StackOverflowError
(2)、一定要递归次数。
什么时候用递归呢?
当一个功能被复用,每次这个功能需要的参数都是根据上一次该功能的元素得出的。
五、其它
1、Properties:
(1)、Map接口中Hashtable的子类。
(2)、 该类上没有泛型定义,因为它的键值都是固定的字符串类型。
(3)、 因为存储都是字符串数据,通常都作为属性信息存在。
(4)、该集合最大的特点就是可以和IO技术相结合。也就是,该集合中的数据可以来自于流。也可以将集合中的数据写入到流中。
2、打印流:
(1)、 PrintStream:
1、使它们能够方便地打印各种数据值表示形式。
2、提供了一系列的打印功能.可以打印任何数据。
3、它的特有的方法不抛出异常。
构造方法:该流是一个处理目的的流对象。
目的:
1、 File对象。 可以指定字符集
2、字符串路径。可以指定字符集
3、 字节输出流。可以对println方法进行自动刷新。
(2)、PrintWriter:字符打印流。
构造函数:
1,File对象。
2,字符串路径。
3,字节输出流。
4,字符输出流。
3、RandomAccessFile:随机访问文件
1,不是字节流或者字符流体系中的成员。
2,该类是用于操作File的类。
3,该对象既可以读取又可以写入。
4,该对象中封装中了一个byte类型的数组。
5,其实它内部就是封装了字节输入流和字节输出流。
6,通过seek方法设置数组的指针就可以实现对文件数据的随机读写。
注意:被操作的数据一定要规律。
六、练习代码:
1、字符输出流练习------Writer;
import java.io.FileWriter;import java.io.IOException;public class day22test1 {public static void main(String[] args) {FileWriter fw=null;try {fw=new FileWriter("test2.txt");fw.write("ww");fw.flush();} catch (Exception e) {throw new RuntimeException("cuowu");}finally{try {if(fw!=null)fw.close();} catch (IOException e) {e.toString();}}}}
2、复制练习
import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class day22test3 {public static void main(String[] args) {FileReader fr= null;FileWriter fw=null;try { fr=new FileReader("test2.txt");fw=new FileWriter("copy_test2.txt");char[] ch=new char[1024];int len=0;while((len=fr.read(ch))!=-1){fw.write(ch);fw.flush();} } catch (Exception e) {}finally{try {fw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {fr.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
3、读取键盘录入
import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class day22test3 {public static void main(String[] args) {FileReader fr= null;FileWriter fw=null;try { fr=new FileReader("test2.txt");fw=new FileWriter("copy_test2.txt");char[] ch=new char[1024];int len=0;while((len=fr.read(ch))!=-1){fw.write(ch);fw.flush();} } catch (Exception e) {}finally{try {fw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {fr.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
4、练习:删除一个带内容的文件夹。
import java.io.File;public class day24test3 {public static void main(String[] args) {File file=new File("C:\\Users\\WY-BRYAN\\Desktop\\wy");deletethefile(file);}public static void deletethefile(File file) {File[] ff=file.listFiles();if(ff!=null){for(File f:ff){if(f.isDirectory()){deletethefile(f);}elsef.delete();}file.delete();}}}
5、序列流
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.SequenceInputStream;import java.util.ArrayList;import java.util.Collections;import java.util.Enumeration;public class daytest25_SequenceInputStream {public static void main(String[] args) throws IOException {// SequenceInputStream: 序列流。FileInputStream fis1=new FileInputStream("11.txt");FileInputStream fis2=new FileInputStream("22.txt");FileInputStream fis3=new FileInputStream("33.txt");ArrayList<FileInputStream> v=new ArrayList<FileInputStream>();v.add(fis1);v.add(fis2);v.add(fis3);Enumeration<FileInputStream> en=Collections.enumeration(v);SequenceInputStream si=new SequenceInputStream(en);FileOutputStream fos=new FileOutputStream("44.txt");byte[] buf=new byte[1024];int len=0;while((len=si.read(buf))!=-1){fos.write(buf, 0, len);}fos.close();si.close();}}
6、Properties练习
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Properties;import java.util.Set;public class day244 {public static void main(String[] args) throws IOException {// Properties练习// method1();// method2();method3();}public static void method3() throws IOException {// TODO Auto-generated method stubProperties pro=new Properties();FileInputStream fis=new FileInputStream("C:\\Users\\WY-BRYAN\\Desktop\\wy.txt");pro.load(fis);pro.setProperty("Bryan1", "11");FileOutputStream fos=new FileOutputStream("C:\\Users\\WY-BRYAN\\Desktop\\wy.txt");pro.store(fos, "change");pro.list(System.out);}public static void method2() {// TODO Auto-generated method stubProperties pro=new Properties();pro.setProperty("Bryan1", "1");pro.setProperty("Bryan2", "2");pro.setProperty("Bryan3", "3");pro.setProperty("Bryan4", "4");pro.list(System.out);}public static void method1() {// TODO Auto-generated method stubProperties pro=new Properties();pro.setProperty("Bryan1", "1");pro.setProperty("Bryan2", "2");pro.setProperty("Bryan3", "3");pro.setProperty("Bryan4", "4");Set<String> set=pro.stringPropertyNames();for(String s:set){String value=pro.getProperty(s);System.out.println(s+"---"+value);}}}
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- 黑马程序员_IO流
- Xcode实用快捷键及插件整理
- .h头文件 .lib动态链接库文件 .dll 动态链接库
- matlab gui中的变量传递
- curl编译静态库收集
- acm_起点
- 黑马程序员_IO流
- C++ - 删除文本的最后一行 代码(C++)
- Subgradient
- cocos2d-x-2.2.3和vs2012环境配置
- Linux 多线程编程的高效开发经验
- Java内存分配
- OpenJDK 源代码阅读之 Collections
- 吸血鬼数字算法
- 2014年6月2日 4G来了