找回密码
 快速注册

QQ登录

只需一步,快速开始

查看: 1397|回复: 1

JAVA 私塾第十三章笔记整理

[复制链接]
solimh 发表于 2011-2-22 10:33:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?快速注册

×
JAVA 私塾第十三章笔记整理

第十三章  I/O流

    JDK所提供的所有流类型,都位于java.io包内都分别继承自以下四种抽象流类型:
字节流字符流
输入流InputStreamReader
输出流OutputStream Writer


    节点流和处理流
        节点流可以从一个特定的数据源读写数据。
        处理流是连接在已存在的流之上,通过对数据的处理为程序提供更强大的读        
        写功能。
        【此处有图片,可以到JAVA 私塾官网下载完整笔记:w ww.javass.cn】

        继承自InputStream的流都是用于向程序中输入数据的,且数据的单位为字节(8bit),上图中深色为节点流,浅色为处理流

        InputStream的方法:
             三个read方法
                int read():读取一个字节,以整数的形式返回,如返回-1,则表示以到输入流的末尾。
                int read(byte[]):读取一系列字节,返回读取的字节数,如返回-1,则表示以到输入流的末尾。
                int read(byte[], int, int):读取一系列字节,返回读取的字节数,如返回-1,则表示以到输入流的末尾。Int,int用于定义读取字节数组的范围。

                void close():关闭流
                skip(long):跳过n个字节不读,返回实际跳过的字节数。

       继承自OutputStream的流适用于程序输出数据的,且数据的单位为字节(8bit),上图中深色为节点流,浅色为处理流
       【此处有图片,可以到JAVA 私塾官网下载完整笔记:w ww.javass.cn】

        void write(int)
        void write(byte[])
        void write(byte[],int,int)
        void close()
        void flush()将缓冲区的数据全部写到目的地。

        继承自InputStream的流都是用于向程序中输入数据的,且数据的单位为字节(16bit),上图中深色为节点流,浅色为处理流
       【此处有图片,可以到JAVA 私塾官网下载完整笔记:w ww.javass.cn】

        Reader的方法:
            三个read方法
                int read():读取一个字节,以整数的形式返回,如返回-1,则表示以到输入流的末尾。
                int read(byte[]):读取一系列字节,返回读取的字节数,如返回-1,则表示以到输入流的末尾。
                int read(byte[], int, int):读取一系列字节,返回读取的字节数,如返回-1,则表示以到输入流的末尾。Int,int用于定义读取字节数组的范围。

                void close():关闭流
                skip(long):跳过n个字节不读,返回实际跳过的字节数。

       继承自Writer的流适用于程序输出数据的,且数据的单位为字节(16bit),上图中深色为节点流,浅色为处理流
       【此处有图片,可以到JAVA 私塾官网下载完整笔记:ww w.javass.cn】

        void write(int)
        void write(byte[])
        void write(byte[],int,int)
        void close()
        void flush()将缓冲区的数据全部写到目的地。

FileInputStream

  1. import java.io.*;
  2. public class TestFileInputStream {
  3.    public static void main(String[] args) {
  4.       int b = 0;
  5.       FileInputStream in = null;
  6.       try {
  7.          in = new FileInputStream("e:/TestFileInputStream.java");
  8.       } catch (FileNotFoundException e) {
  9.          System.out.println("找不到指定文件");
  10.          System.exit(-1);
  11.       }
  12.    
  13.       try {
  14.          long num = 0;
  15.          while((b=in.read())!=-1){
  16.             System.out.print((char)b);
  17.             num++;
  18.          }
  19.          in.close();  
  20.          System.out.println();
  21.          System.out.println("共读取了 "+num+" 个字节");
  22.       } catch (IOException e1) {
  23.         System.out.println("文件读取错误"); System.exit(-1);
  24.       }
  25.    }
  26. }
复制代码
共读取了 700 个字节

FileOutputStream

  1. import java.io.*;
  2. public class TestFileOutputStream {
  3.    public static void main(String[] args) {
  4.       int b = 0;
  5.       FileInputStream in = null;
  6.       FileOutputStream out = null;
  7.       try {
  8.          in = new FileInputStream("e:/TestFileInputStream.java");
  9.          out = new FileOutputStream("e:/1.java");
  10.          while((b=in.read())!=-1){
  11.             out.write(b);
  12.          }
  13.          in.close();
  14.          out.close();
  15.       } catch (FileNotFoundException e2) {
  16.          System.out.println("找不到指定文件"); System.exit(-1);
  17.       } catch (IOException e1) {
  18.          System.out.println("文件复制错误"); System.exit(-1);
  19.       }
  20.       System.out.println("文件已复制");
  21.    }
  22. }
复制代码
FileReader

  1. import java.io.*;
  2. public class TestFileReader {
  3.    public static void main(String[] args) {
  4.       FileReader fr = null;
  5.       int c = 0;
  6.       try {
  7.          fr = new FileReader("e:/TestFileReader.java");
  8.          int ln = 0;
  9.          while ((c = fr.read()) != -1) {
  10.             System.out.print((char)c);
  11.          }
  12.          fr.close();
  13.       } catch (FileNotFoundException e) {
  14.          System.out.println("找不到指定文件");
  15.       } catch (IOException e) {
  16.          System.out.println("文件读取错误");
  17.       }
  18.    }
  19. }
复制代码
FileWriter

  1. import java.io.*;
  2. public class TestFileWriter {
  3.    public static void main(String[] args) {
  4.       FileWriter fw = null;
  5.       try {
  6.          fw = new FileWriter("e:/sunicode.dat");
  7.          for(int c=0;c<=50000;c++){
  8.             fw.write(c);
  9.          }
  10.          fw.close();
  11.       } catch (IOException e1) {
  12.             e1.printStackTrace();
  13.         System.out.println("文件写入错误");
  14.         System.exit(-1);
  15.       }
  16.    }
  17. }
复制代码
BufferedStream

  1. import java.io.*;
  2. public class TestBufferStream{
  3.   public static void main(String[] args) {
  4.     try {
  5.       BufferedWriter bw = new BufferedWriter(new FileWriter("e:/2.java"));
  6.       BufferedReader br = new BufferedReader(
  7.              new FileReader("e:/1.java"));
  8.       String s = null;
  9.       for(int i=1;i<=100;i++){
  10.         s = String.valueOf(Math.random());
  11.         bw.write(s);
  12.         bw.newLine();
  13.       }
  14.       bw.flush();
  15.       while((s=br.readLine())!=null){
  16.         System.out.println(s);
  17.       }
  18.       bw.close();
  19.       br.close();
  20.     } catch (IOException e) { e.printStackTrace();}
  21.   }
  22. }
复制代码
简述File类的基本功能
        处理文件和获取文件信息,文件或文件夹的管理
        除了读写文件内容其他的都可以做

代码示例:如何使用随机文件读写类来读写文件内容
    RW表示文件时可读写的
       读:
  try{
    RandomAccessFile f = new RandomAccessFile("test.txt", "rw");
    long len = 0L;
    long allLen = f.length();
    int i = 0;
    while (len < allLen) {
      String s = f.readLine();
      if (i > 0) {
          col.add(s);
      }
      i++;
      //游标
      len = f.getFilePointer();
    }
  }catch(Exception err){
    err.printStackTrace();
  }
  
  写:
  
  try{
    RandomAccessFile f = new RandomAccessFile("test.txt", "rw");
    StringBuffer buffer = new StringBuffer("\n");
    Iterator it = col.iterator();
    while (it.hasNext()) {
      buffer.append(it.next() + "\n");
    }
    f.writeUTF(buffer.toString());
  }catch(Exception err){
     err.printStackTrace();
  }

代码示例:如何使用流的基本接口来读写文件内容

  1. import java.io.*;
  2. public class Test{
  3. public static void main(String args[]){
  4.    String currentLine;
  5.    try{
  6.       DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("Test.java")));
  7.       while ((currentLine = in.readLine()) != null)
  8.          System.out.println(currentLine);
  9.       }catch (IOException e) {
  10.          System.err.println("Error: " + e);
  11.       } // End of try/catch structure.
  12.    } // End of method: main
  13. } // End of class
复制代码
宋中 发表于 2011-2-22 18:57:41 | 显示全部楼层
感谢楼主分享!

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 快速注册

本版积分规则

快速回复 返回顶部 返回列表