湖南工业大学论坛
欢迎来到湖南工业大学论坛,工大30000师生的交流平台!

希望你能玩得愉快!

Join the forum, it's quick and easy

湖南工业大学论坛
欢迎来到湖南工业大学论坛,工大30000师生的交流平台!

希望你能玩得愉快!
湖南工业大学论坛
Would you like to react to this message? Create an account in a few clicks or log in to continue.

J***A 私塾第十三章笔记整理

向下

JAVA 私塾第十三章笔记整理 Empty J***A 私塾第十三章笔记整理

帖子 由 luodis 周二 二月 22, 2011 1:45 pm

J***A 私塾第十三章笔记整理

第十三章 I/O流

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

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

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

继承自InputStream的流都是用于向程序中输入数据的,且数据的单位为字节(16bit),上图中深色为节点流,浅色为处理流
【此处有图片,可以到J***A 私塾官网下载完整笔记: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),上图中深色为节点流,浅色为处理流
【此处有图片,可以到J***A 私塾官网下载完整笔记:www.javass.cn】

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

FileInputStream
代码:

import java.io.*;
public class TestFileInputStream {
  public static void main(String[] args) {
      int b = 0;
      FileInputStream in = null;
      try {
        in = new FileInputStream("e:/TestFileInputStream.java");
      } catch (FileNotFoundException e) {
        System.out.println("找不到指定文件");
        System.exit(-1);
      }
   
      try {
        long num = 0;
        while((b=in.read())!=-1){
            System.out.print((char)b);
            num++;
        }
        in.close(); 
        System.out.println();
        System.out.println("共读取了 "+num+" 个字节");
      } catch (IOException e1) {
        System.out.println("文件读取错误"); System.exit(-1);
      }
  }
}

共读取了 700 个字节

FileOutputStream
代码:

import java.io.*;
public class TestFileOutputStream {
  public static void main(String[] args) {
      int b = 0;
      FileInputStream in = null;
      FileOutputStream out = null;
      try {
        in = new FileInputStream("e:/TestFileInputStream.java");
        out = new FileOutputStream("e:/1.java");
        while((b=in.read())!=-1){
            out.write(b);
        }
        in.close();
        out.close();
      } catch (FileNotFoundException e2) {
        System.out.println("找不到指定文件"); System.exit(-1);
      } catch (IOException e1) {
        System.out.println("文件复制错误"); System.exit(-1);
      }
      System.out.println("文件已复制");
  }
}

FileReader
代码:

import java.io.*;
public class TestFileReader {
  public static void main(String[] args) {
      FileReader fr = null;
      int c = 0;
      try {
        fr = new FileReader("e:/TestFileReader.java");
        int ln = 0;
        while ((c = fr.read()) != -1) {
            System.out.print((char)c);
        }
        fr.close();
      } catch (FileNotFoundException e) {
        System.out.println("找不到指定文件");
      } catch (IOException e) {
        System.out.println("文件读取错误");
      }
  }
}

FileWriter
代码:

import java.io.*;
public class TestFileWriter {
  public static void main(String[] args) {
      FileWriter fw = null;
      try {
        fw = new FileWriter("e:/sunicode.dat");
        for(int c=0;c<=50000;c++){
            fw.write(c);
        }
        fw.close();
      } catch (IOException e1) {
       e1.printStackTrace();
        System.out.println("文件写入错误");
        System.exit(-1);
      }
  }
}

BufferedStream
代码:

import java.io.*;
public class TestBufferStream{
  public static void main(String[] args) {
    try {
      BufferedWriter bw = new BufferedWriter(new FileWriter("e:/2.java"));
      BufferedReader br = new BufferedReader(
            new FileReader("e:/1.java"));
      String s = null;
      for(int i=1;i<=100;i++){
        s = String.valueOf(Math.random());
        bw.write(s);
        bw.newLine();
      }
      bw.flush();
      while((s=br.readLine())!=null){
        System.out.println(s);
      }
      bw.close();
      br.close();
    } catch (IOException e) { e.printStackTrace();}
  }
}

简述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();
}

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

import java.io.*;
public class Test{
public static void main(String args[]){
  String currentLine;
  try{
      DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("Test.java")));
      while ((currentLine = in.readLine()) != null)
        System.out.println(currentLine);
      }catch (IOException e) {
        System.err.println("Error: " + e);
      } // End of try/catch structure.
  } // End of method: main
} // End of class
luodis
luodis

帖子数 : 21
注册日期 : 10-12-30

返回页首 向下

返回页首


 
您在这个论坛的权限:
不能在这个论坛回复主题