java io 笔记二:FileOutPutStream、FileInPutStream、FileWriter、FileReader类

FileOutPutStream 和FileInPutStream:java

  
  
  
  
  1. package iotest;   
  2.    
  3.    
  4.    
  5. import java.io.*;   
  6.    
  7. public class StreamTest {   
  8.    
  9.     /**  
  10.      * 流的概念:  
  11.      *  一、流是字节顺序的抽象概念。  
  12.      *  二、文件是数据的静态存储形式、而流事是指数据传输时的形态。  
  13.      *  三、流类分为两个大类:节点流和过滤流类(也叫处理流类)。  
  14.   
  15.      *  InputStream类:  
  16.      *  程序能够从中连续读取字节的对象叫输入流,在java中,用InputStream类来描述全部输入流的抽象概念。  
  17.   
  18.      *  OutPutStream类:  
  19.      *  程序能够向其中连续写入字节的对象叫输出流,在java中,用OutPutStream类来描述全部输出流的抽象概念。  
  20.   
  21.      *  FuleInputStream和FileOutPutStream类分别用来建立磁盘文件的输入流个输出流对象、经过他们的构造函数来指点文件路径文件名。  
  22.   
  23.      *  建立FuleInputStream实例对象时,指定的文件应该是存在和可读的,建立FileOutPutStream实例对象时,若是指定的文件已经存在,这个文件中的原来内容将被覆盖清楚。  
  24.   
  25.      *  对同一个磁盘文件建立FuleIntputStream对象的两种方式:  
  26.      *  (1)、FileInPutStream inOne = new FileInPutStream("hello.test");  
  27.      *  (2)、File f = new ("hello.test");  
  28.      *       FileInPutStream inTwo = new FileInPutStream(f);  
  29.        
  30.      *  建立FileOutPutStream实例对象时,能够指定还不存在的文件名,不能指定一个已被其余程序打开了的文件。  
  31.       
  32.       
  33.      * 例题:用FileOutPutStream类向文件中写入一个字符串,而后用FileInPutStream读出写入的内容  
  34.      * @param args  
  35.      * @throws IOException   
  36.      */   
  37.     public static void main(String[] args) throws IOException {   
  38.            
  39.         /* FileOutputStream类:文件输出流是用于将数据写入 File 或 FileDescriptor 的输出流。  
  40.          *   
  41.          *   
  42.          * FileOutputStream(String name)构造:建立一个向具备指定名称的文件中写入数据的输出文件流。  
  43.          *   
  44.          */   
  45.         FileOutputStream fout = new FileOutputStream("hzw//hello.txt");   
  46.            
  47.            
  48.         /*  
  49.          * Writer方法: b.length 个字节从指定 byte 数组写入此文件输出流中。  
  50.          *   
  51.          * String.getBytes()方法用于将字符串转换为字节数组  
  52.          */   
  53.         fout.write("www.hzw.com".getBytes());   
  54.            
  55.            
  56.         //关闭流   
  57.         fout.close();   
  58.            
  59.            
  60.         /*  
  61.          * 建立一个file对象: 文件和目录路径名的抽象表示形式。  
  62.          *   
  63.          * File(String name)构造:经过将给定路径名字符串转换为抽象路径名来建立一个新 File 实例。  
  64.          */   
  65.         File f = new File("hzw//hello.txt");   
  66.            
  67.         /*  
  68.          * FileInputStream类:用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用 FileReader。  
  69.          *   
  70.          * FileInputStream(File file)构造:经过打开一个到实际文件的链接来建立一个 FileInputStream,  
  71.          *                                 该文件经过文件系统中的 File 对象 file 指定。  
  72.          */   
  73.         FileInputStream input = new FileInputStream(f);   
  74.            
  75.         /*  
  76.          * 建立一个byte(字节)类型数组  
  77.          */   
  78.         byte[] by = new byte[1024];   
  79.            
  80.         /*  
  81.          * read方法:今后输入流中读取一个数据字节。  
  82.          *   
  83.          * read(byte[] b)方法:今后输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。  
  84.          *   
  85.          * 返回:读入缓冲区的字节总数,若是由于已经到达文件末尾而没有更多的数据,则返回 -1。  
  86.          */   
  87.         int len = input.read(by);   
  88.            
  89.            
  90.         /*  
  91.          * String(by,0,len)构造: 用于将by数组转换成字符串——从0开始到len(Int变量值)结束  
  92.          */   
  93.         System.out.println(new String(by,0,len));   
  94.            
  95.         //关闭流   
  96.         input.close();       
  97.            
  98.     }   
  99.    
  100. }  

FileWriter、FileReader类:数组

  
  
  
  
  1. package iotest;   
  2.    
  3. import java.io.*;   
  4. public class IoStringTest {   
  5.    
  6.     /**  
  7.      * java 中专门有两个类分别对字符串进行输入跟输出处理。Reader、Writer类  
  8.      *   
  9.      * 使用FileWriter类向文件中写入一个字符串,而后用FileReader类读出写入的内容  
  10.      *   
  11.      * @param args  
  12.      * @throws IOException   
  13.      */   
  14.     public static void main(String[] args) throws IOException {   
  15.         /*  
  16.          * FileWriter(继承OutputStreamWriter类 ):用来写入字符文件的便捷类。  
  17.          *   
  18.          * FileWriter(String name):根据给定的文件名构造一个 FileWriter 对象。  
  19.          */   
  20.         FileWriter wr = new FileWriter("hzw//tiantian.txt");   
  21.            
  22.         /*  
  23.          * write:继承OutputStreamWriter类 write方法——的写入字符串。  
  24.          */   
  25.         wr.write("何祖文");   
  26.            
  27.         //有兴趣的能够把这句代码删掉试试!!!   
  28.         wr.close();   
  29.            
  30.         char[] ch = new char[1024];   
  31.            
  32.         /*  
  33.          * FileReader(继承InputStreamReader类):用来读取字符文件的便捷类。  
  34.          *   
  35.          * FileReader(String fileName):在给定从中读取数据的文件名的状况下建立一个新 FileReader。  
  36.          */   
  37.         FileReader re = new FileReader("hzw//tiantian.txt");   
  38.            
  39.         /*  
  40.          * read:继承InputStreamReader类的read方法——试图将字符读入指定的字符缓冲区。  
  41.          *   
  42.          * 返回:添加到缓冲区的字符数量,若是此字符源位于缓冲区末端,则返回 -1   
  43.          */   
  44.         int len = re.read(ch);   
  45.         System.out.println(new String(ch,0,len));   
  46.         re.close();   
  47.     }   
  48.    
相关文章
相关标签/搜索