码迷,mamicode.com
首页 > 其他好文 > 详细

输入输出流

时间:2016-02-06 22:16:05      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

在文件之后 比较合适出现的 就是 关于 流的操作了。

猜想关于数据的写入和写出操作,本质上是01形式的二进制数,而这些数据的排列方式是不能乱套的。他们是一个有序的整体,这队长长的用于表示一些内容的东西就称作流了。在这里面用Stream 来标识。

 java.io 包中定义了多个流类,用来实现输入/输出功能,以不同的角度可以分类为:

   1、按数据流的方向分为输入和输出。

   2、按数据处理的单位分为字节流和字符流。

   3、按功能不同分为节点流和处理流。

package IOPart;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class IOPart {
    
    public static void main(String[] args) {
        /**
         * 在F:\tryFile目录下,创建一个a.txt 文件,修改里面的内容为:used to try the first demo of inputStream.
         */
        try {
            //如果 在这个 路径找不到 这样一个 文件的话,就会报出,文件未找到异常,所以 要用 FileNotFoundException 包裹
            FileInputStream fileInputStream = new FileInputStream(new File("f:\\tryFile\\a.txt"));
            byte[] contents = new byte[1024];
            fileInputStream.read(contents);//这里会报一个 读写异常,再从文件流往内存中的数组中写入数据的时候,可能会出现问题,需要用IOException包裹一下
            String result = new String(contents);
            System.out.println(result);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行结果:

used to try the first demo of inputStream.

//这个编辑器还真是高级,自动抹掉了我后面茫茫多的空格。事实上因为 我们 创建了一个 长达 1024个 bite位的数组。所以。有多少读多少,没有填空,补足。这样就会有弊端,我们要存储的数据的内容的大小不确定,那么这个 byte给多大合适呢?

方案一:

package IOPart;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class IOPart {
    
    public static void main(String[] args) {
        /**
         * 在F:\tryFile目录下,创建一个a.txt 文件,修改里面的内容为:used to try the first demo of inputStream.
         */
        try {
            //如果 在这个 路径找不到 这样一个 文件的话,就会报出,文件未找到异常,所以 要用 FileNotFoundException 包裹
            FileInputStream fileInputStream = new FileInputStream(new File("f:\\tryFile\\a.txt"));
            byte[] contents = new byte[fileInputStream.available()];
            fileInputStream.read(contents);//这里会报一个 读写异常,再从文件流往内存中的数组中写入数据的时候,可能会出现问题,需要用IOException包裹一下
            String result = new String(contents);
            System.out.println(result);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

流作为资源的一种使用之后是要关闭的。

package IOPart;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class IOPart {
    
    public static void main(String[] args) {
        /**
         * 在F:\tryFile目录下,创建一个a.txt 文件,修改里面的内容为:used to try the first demo of inputStream.
         */
        FileInputStream fileInputStream=null;
        try {
            //如果 在这个 路径找不到 这样一个 文件的话,就会报出,文件未找到异常,所以 要用 FileNotFoundException 包裹
            fileInputStream = new FileInputStream(new File("f:\\tryFile\\a.txt"));
            byte[] contents = new byte[fileInputStream.available()];
            fileInputStream.read(contents);//这里会报一个 读写异常,再从文件流往内存中的数组中写入数据的时候,可能会出现问题,需要用IOException包裹一下
            String result = new String(contents);
            System.out.println(result);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileInputStream!=null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

这种抽取形式,还是比较重要的。

方式二:

package IOPart;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class IOPart {
    
    public static void main(String[] args) {
        IOPart ioPart = new IOPart();
        ioPart.method2();
        
    }

    private void method2() {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream= new FileInputStream(new File("f:\\tryFile\\SendPart.java"));
            byte[] contents = new byte[1024];
            int length = 0;
            while((length=fileInputStream.read(contents))!=-1){
                System.out.println(new String(contents,0,length));
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            if(fileInputStream!=null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    private void method1() {

        /**
         * 在F:\tryFile目录下,创建一个a.txt 文件,修改里面的内容为:used to try the first demo of inputStream.
         */
        FileInputStream fileInputStream=null;
        try {
            //如果 在这个 路径找不到 这样一个 文件的话,就会报出,文件未找到异常,所以 要用 FileNotFoundException 包裹
            fileInputStream = new FileInputStream(new File("f:\\tryFile\\a.txt"));
            byte[] contents = new byte[fileInputStream.available()];
            fileInputStream.read(contents);//这里会报一个 读写异常,再从文件流往内存中的数组中写入数据的时候,可能会出现问题,需要用IOException包裹一下
            String result = new String(contents);
            System.out.println(result);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileInputStream!=null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

写出:

package IOPart;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class OutputDemo1 {

    public static void main(String[] args) {
        
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(new File("F:\\tryFile\\output.txt"));
            String contentString = new String("lifei");
            fileOutputStream.write(contentString.getBytes());
            System.out.println("写出成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(fileOutputStream!=null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

输入输出流

标签:

原文地址:http://www.cnblogs.com/letben/p/5184253.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!