现在的位置: 首页 > 综合 > 正文

java IO流

2013年10月18日 ⁄ 综合 ⁄ 共 5311字 ⁄ 字号 评论关闭

***********************IO流介绍**************************

1.IO流是干什么的?

     IO流指 的是输入输出流,用来处理设备上的数据。这里的设备指硬盘,内存,键盘录入,网络传输等。

2.IO流的分类?

     按处理数据类型来分:字节流和字符流

     按流的方向来分:输入流和输入流。

     PS:初学流的时候,新手很容易搞不清什么时候用输入流,什么时候用输出流。简单来说,当需要读数据的时候,需要使用输入流,当需要写数据的时候,需要使用输出流。我以前是这么记忆的:“读入写出”,要读的话就用输入流,要写的话,就用输出流。经常想起这4个字,慢慢就记熟下来了。

3.什么时候使用字节流?什么时候使用字符流?

    首先需要知道的是,任何数据存在硬盘上时,都是以二进制的形式存储的。而通过使用字节流,可以读取任意文件。字节流一次读取一个字节,而字符流使用了字节流读到一个或者多个字节时,去查找指定的编码表,返回对应的编码。所以字符流只能处理纯文本字符数据,而字节流可以处理更多类型的数据,比如图片,视频,音频文件等。因此,只要是纯文本数据处理,优先考虑使用字符流。其他情况就使用字节流。

4.IO流类的关系(列举了其中一部分)

***********************使用IO流读取存储文件**********************

1.使用字符流,读取和存储纯文本文件

      存储文件,也就是像一个文件里写内容,既然是写,那就需要使用输出流。而且我们写的是纯文本文件,所以这里使用字符流来操作,java api提供给我们FileWriter这么一个类,我们来试试:(读取文件同理使用FileReader类)

package org.example.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class TestFileWriter {

    public static void main(String[] args) throws Exception {
        writeToFile();
        readFromFile();
    }

    /**
     * DOC 从文件里读取数据.
     * 
     * @throws FileNotFoundException
     * @throws IOException
     */
    private static void readFromFile() throws FileNotFoundException, IOException {
        File file = new File("E:\\helloworld.txt");// 指定要读取的文件
        FileReader reader = new FileReader(file);// 获取该文件的输入流
        char[] bb = new char[1024];// 用来保存每次读取到的字符
        String str = "";// 用来将每次读取到的字符拼接,当然使用StringBuffer类更好
        int n;// 每次读取到的字符长度
        while ((n = reader.read(bb)) != -1) {
            str += new String(bb, 0, n);
        }
        reader.close();// 关闭输入流,释放连接
        System.out.println(str);
    }

    /**
     * DOC 往文件里写入数据.
     * 
     * @throws IOException
     */
    private static void writeToFile() throws IOException {
        String writerContent = "hello world,你好世界";// 要写入的文本
        File file = new File("E:\\helloworld.txt");// 要写入的文本文件
        if (!file.exists()) {// 如果文件不存在,则创建该文件
            file.createNewFile();
        }
        FileWriter writer = new FileWriter(file);// 获取该文件的输出流
        writer.write(writerContent);// 写内容
        writer.flush();// 清空缓冲区,立即将输出流里的内容写到文件里
        writer.close();// 关闭输出流,施放资源
    }

}

测试结果:

hello world,你好世界

2.使用字节流,读取和存储图片

首先使用输入流读取图片信息,然后通过输出流写入图片信息:

package org.example.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class TestIOStream {

    /**
     * 
     * DOC 将F盘下的test.jpg文件,读取后,再存到E盘下面.
     * 
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        FileInputStream in = new FileInputStream(new File("F:\\test.jpg"));// 指定要读取的图片
        File file = new File("E:\\test.jpg");
        if (!file.exists()) {// 如果文件不存在,则创建该文件
            file.createNewFile();
        }
        FileOutputStream out = new FileOutputStream(new File("E:\\test.jpg"));// 指定要写入的图片
        int n = 0;// 每次读取的字节长度
        byte[] bb = new byte[1024];// 存储每次读取的内容
        while ((n = in.read(bb)) != -1) {
            out.write(bb, 0, n);// 将读取的内容,写入到输出流当中
        }
        out.close();// 关闭输入输出流
        in.close();
    }

}

*********************使用缓冲流来读写文件*************************

通过BufferedReader和BufferedWriter来读写文件

     使用缓冲流的好处是,能够更高效的读写信息,原理是将数据先缓冲起来,然后一起写入或者读取出来。经常使用的是readLine()方法,表示一次读取一行数据。

package org.example.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class TestBufferedWriter {

    public static void main(String[] args) throws Exception {
        write();
        read();
    }

    /**
     * DOC 读取信息.
     * 
     * @throws FileNotFoundException
     * @throws IOException
     */
    private static void read() throws FileNotFoundException, IOException {
        File file = new File("E:\\a.txt");// 指定要读取的文件
        // 获得该文件的缓冲输入流
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String line = "";// 用来保存每次读取一行的内容
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        bufferedReader.close();// 关闭输入流
    }

    /**
     * DOC 写入信息.
     * 
     * @throws IOException
     */
    private static void write() throws IOException {
        File file = new File("E:\\a.txt");// 指定要写入的文件
        if (!file.exists()) {// 如果文件不存在则创建
            file.createNewFile();
        }
        // 获取该文件的缓冲输出流
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
        // 写入信息
        bufferedWriter.write("你好世界");
        bufferedWriter.newLine();// 表示换行
        bufferedWriter.write("hello world");
        bufferedWriter.flush();// 清空缓冲区
        bufferedWriter.close();// 关闭输出流
    }

}

使用BufferedInputStream和BufferedOuputStream读写图片

使用方式和FileInputStrem和FileOutputStream基本一致:

package org.example.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class TestBufferedString {

    public static void main(String[] args) throws Exception {
        // 指定要读取文件的缓冲输入字节流
        BufferedInputStream in = new BufferedInputStream(new FileInputStream("F:\\test.jpg"));
        File file = new File("E:\\test.jpg");
        if (file != null) {
            file.createNewFile();
        }
        // 指定要写入文件的缓冲输出字节流
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
        byte[] bb = new byte[1024];// 用来存储每次读取到的字节数组
        int n;// 每次读取到的字节数组的长度
        while ((n = in.read(bb)) != -1) {
            out.write(bb, 0, n);// 写入到输出流
        }
        out.close();// 关闭流
        in.close();
    }

}

当字节流和字符流之间需要转化的时候,或者要对字节数据进行编码转换的时候,就需要使用转换流

package org.example.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class TestStreamReader {

    public static void main(String[] args) throws Exception {
        File file = new File("E:\\b.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file), "GBK");
        out.write("hello world,你好世界");
        out.close();

        InputStreamReader in = new InputStreamReader(new FileInputStream(file), "gbk");
        char[] cc = new char[1024];
        int n = 0;
        String str = "";
        while ((n = in.read(cc)) != -1) {
            str += new String(cc, 0, n);
        }
        in.close();
        System.out.println(str);
    }

}

针对java IO,标记之:http://blog.csdn.net/a107494639/article/category/1151283

抱歉!评论已关闭.