设为首页收藏本站

山东农业大学论坛

 找回密码
 注册会员

QQ登录

只需一步,快速开始

查看: 135|回复: 1
打印 上一主题 下一主题

JAVA 私塾第十三章笔记整理 [复制链接]

Rank: 1

UID
2905
金币
52 枚
威望
0 点
经验
5 点
体力
9 点
在线时间
1 小时
跳转到指定楼层
楼主
发表于 2011-2-21 12:01:38 |只看该作者 |倒序浏览
JAVA 私塾第十三章笔记整理

第十三章  I/O流

    JDK所提供的所有流类型,都位于java.io包内都分别继承自以下四种抽象流类型:

字节流字符流
输入流InputStreamReader
输出流OutputStream Writer



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

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

        继承自InputStream的流都是用于向程序中输入数据的,且数据的单位为字节(16bit),上图中深色为节点流,浅色为处理流
       【此处有图片,可以到JAVA 私塾官网下载完整笔记:www.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 私塾官网下载完整笔记:www.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
复制代码
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
分享分享0 收藏收藏0 顶0 踩0

Rank: 3Rank: 3Rank: 3

UID
34
金币
939 枚
威望
0 点
经验
20 点
体力
115 点
在线时间
29 小时

新手勋章 寻找爱情勋章 国庆节勋章 爱MYADSU勋章 风流GG勋章 七夕纪念勋章

沙发
发表于 2011-2-21 14:55:46 |只看该作者
不错,支持一下,虽然我不学java

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

关闭

山东农业大学论坛-你永远的家

社区首页| 家园首页| 群组首页|手机版|山东农业大学论坛   

GMT+8, 2025-4-20 05:00

Powered by Discuz! Templates yeei! © 2001-2010 Comsenz Inc.

回顶部