IO流之文件流的使用

Java IO原理

1. I/O是Input/Output的缩写, I/O技术是非常实用的技术,用于 处理设备之间的数据传输。如读/写文件,网络通讯等。
2. Java程序中,对于数据的输入/输出操作以“流(stream)” 的 方式进行。
3. java.io包下提供了各种“流”类和接口,用以获取不同种类的 数据,并通过标准的方法输入或输出数据。

以下代码块中分别是文件流(也称节点流)的字符输入流、字符输出流、字节输入流、使用字符流复制文本、使用字节流复制图片的演示

package com.Lindom.IO流;
import org.junit.Test;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
/*
    FileReader :字符输入流
        read(): 返回的是读取到的一个字符的int类型
        read(char[] cbuf): 返回的int是数组里元素的个数
 */
public class FileReaderDemo01 {


    //第一种方式
    @Test
    public void test01(){    //这里最好不要用抛出异常,因为如果出现IOException异常,会出现流开启的状态下程序异常终结,而导致流没有关闭!
        FileReader fileReader = null;
        try {
            File file = new File("hello.txt");
            fileReader = new FileReader(file);   //这里可能出现FileNotFoundException异常
            int read;  //记录每次读取字符的个数
            //read()方法:返回一个读入的一个字符,如果到达文件末尾,则返回-1
            while((read = fileReader.read()) != -1){  //这里可能出现IOException异常
                System.out.print((char)read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileReader != null)  //如果上面出现FileNotFoundException异常而导致fileReader流没有开启,
                fileReader.close();      //那么这里直接关闭就会出现错误,所以需要加以判断
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //第二种方式
    @Test
    public void test02(){
        FileReader fileReader = null;
        try {
            File file = new File("hello.txt");
            fileReader = new FileReader(file);
            char[] chars = new char[5];
            int len;
            while((len = fileReader.read(chars))!= -1){
                //方式一:
                for (int i = 0; i < len; i++) {
                    System.out.print(chars[i]);
                }
                //方式二:
//             String str = new String(chars,0,len);
//             System.out.print(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fileReader != null)
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

package com.Lindom.IO流;
import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/*
    FileWriter :字符输出流
       两个构造器
            ----FileWriter(file)
            ----FileWriter(file,true)
       方法
            ----write()
 */
public class FileWriterDeom01 {

    @Test
    public void test01(){
        FileWriter fw = null;
        try {
            //创建File对象  如果writer.txt文件不存在,则会自动为我们创建,不会报错
            File file = new File("writer.txt");
            //创建FileWriter对象   如果想要在原有文本上添加字符而不是覆盖原有字符,需调用FileWriter(file ,true/false)方法
            //调用FileWriter(file) 方法的话默认为false,为覆盖原有文本; 选择true为添加操作
            fw = new FileWriter(file,true);
            //使用write()方法写入字符
            fw.write("I have a dream!\n");
            fw.write("You need a dream!\n");
            //资源关闭
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fw != null)
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

package com.Lindom.IO流;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/*
    字节输入流(FileInputStream)
        操作方式基本与字符流一样
        使用字节流读取文本数据时,可能出现中文乱码,因为一个中文是三个字节
        但是如果使用字节流实现文本的复制,也是可行的!
 */

public class FileInputStreamDemo01 {

    @Test
    public void test01() throws IOException {
        FileInputStream fis = null;
        try {
            File file = new File("inputStream.txt");
            fis = new FileInputStream(file);
            byte[] buffer = new byte[5];
            int len;
            while ((len = fis.read(buffer)) != -1){
                String str = new String(buffer,0,len);
                System.out.print(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null)
            fis.close();
        }

    }

}

package com.Lindom.IO流;
import org.junit.Test;
import java.io.*;
/*
    文本复制:
    这里是结合字符输入流FileReader和字符输出流FileWriter,完成了对文本的复制操作

 */
public class FileCopyDemo {

    @Test
    public void test(){
        FileReader fr = null;
        FileWriter fw = null;
        try {
            File file = new File("copy.txt");
            File file1 = new File("copyTo.txt");
            fr = new FileReader(file);
            fw = new FileWriter(file1,true);
            char[] cbuf = new char[5];
            int len = 0;
            while((len = fr.read(cbuf)) != -1){
                fw.write(cbuf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fw != null)
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fr != null)
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

package com.Lindom.IO流;
import org.junit.Test;
import java.io.*;
/*
    使用FileInputStream(字节输入流)和FileOutputStream(字节输出流)完成图片的复制
 */

public class FileCopyDemo02 {

    @Test
    public void test01(){
        FileInputStream fis = null;
        FileOutputStream fop = null;
        try {
            File file = new File("Lisa01.jpg");
            fis = new FileInputStream(file);
            fop = new FileOutputStream("Lisa.jpg");
            byte[] bytes = new byte[5];
            int len;
            while ((len = fis.read(bytes)) != -1){
                fop.write(bytes,0,len);
            }
            System.out.println("图片复制成功!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fop != null)
                fop.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fis != null)
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章