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

IO流基本操作

时间:2016-09-14 12:17:34      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

第一种分法:1.输入流2.输出流
第二种分法:1.字节流2.字符流
第三种分法:1.节点流2.处理流
////////////////////////////////////////////////////////////////////////////字节流:

InputStream 的子类是? FileInputStream;
InputStream :int  read(byte[]b,int off,int len)从第几位读,读几个,返回读取的个数
OutputStream 的子类是?FileOutputStream
OutputStream :void  write(byte[]b,int off,int len)
public static void main(String args[]){
FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream("e:/from.txt");//输入流(从文件读数据,即数据输入到程序里)
            out = new FileOutputStream("e:/to.txt");//输出流(写入文件数据,即数据输出到文件里)
            byte[] buffer  =new  byte[100];//字节数组,用来存放读取的数据
            int num = in.read(buffer, 0, buffer.length);//从第0位读取,读from.txt文件的 buffer.length个,放进buffer内
            out.write(buffer, 0, num);//read方法返回读取的个数
            //out.write(buffer, 0, 2);//从0位开始,把buffer数组的2个字节写进to.txt文件里
            for(int i = 0;i<buffer.length;i++){
                System.out.print(buffer[i]);
            }
String s = new String(buffer);//转换成原来的字符
s= s.trim();//trim方法:去掉首尾空格和空字符,中间的空格不去掉
            System.out.print(s);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
finally{
            try {
                in.close();
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }    
        }
}

读取大文件:
public static void main(String args[]) {
        FileInputStream in = null;
        FileOutputStream out = null;
        int a = 0;
        try {
            in = new FileInputStream("e:/from.txt");//输入流(从文件读数据,即数据输入到程序里)
            out = new FileOutputStream("e:/to.txt");//输出流(写入文件数据,即数据输出到文件里)
            byte[] buffer  =new  byte[1024];//字节数组,用来存放读取的数据
            while(true){
                
                a++;
                System.out.print(a);
                int num = in.read(buffer, 0, buffer.length);//从第0位读取,读from.txt文件的 buffer.length个,放进buffer内
                out.write(buffer, 0, num);//read方法返回读取的个数
                if(num == -1){//返回-1,说明读取完毕
                    break;
                }
            }
    
    
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            try {
                in.close();
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }    
        }

////////////////////////////////////////字符流:读写文件时,以字符为基础
字节输入流:Reader 的子类是?FileReader
int read(char [] c,int off,int len)
字节输出流:Writer 的子类是?FileWriter
void write(char [] c,int off,int len)

public static void main(String args[]) {
        
        FileReader reader =null;
        FileWriter writer = null;
        try{
        reader = new FileReader("E:/from.txt");
        writer = new FileWriter("e:/to.txt");
        
        char [] buffer = new char[100];
        int num = reader.read(buffer, 0, buffer.length);
        writer.write(buffer, 0, num);
        for(int i = 0;i<num;i++){
            System.out.print(buffer[i]);
        }
        }
        catch(Exception e){
            System.out.print(e);
        }
        finally{
            try{
            reader.close();
            writer.close();
            }
            catch(Exception e){
                System.out.print("success!!!");
            }
        }
}
/////////////////////////////////////////////////一次性读取一行数据
public static void main(String args[]) {
        
        FileReader fileReader = null;
        BufferedReader  bufferedReader = null;
        try{
            fileReader = new FileReader("e:/from.txt");
            bufferedReader = new BufferedReader(fileReader);
            String line = null;
            while(true){
            line  = bufferedReader.readLine();
            if(line == null){
                break;
            }
            System.out.println(line);
            }
        }
        catch(Exception e){
            System.out.println(e);
            
        }
        finally{
            try{
            bufferedReader.close();
            fileReader.close();
            }
            catch(Exception e){
                System.out.println(e);
            }
            
        }

 

IO流基本操作

标签:

原文地址:http://www.cnblogs.com/wwzyy/p/5871334.html

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