IO流之输入输出

1、input
1、InputStream input=new FileInputStream(path);
2、input.read(b)) != -1

package com.eduask.iodemo;

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

public class FileInputStreamDemo {


    public static void testInputStream2(String path) {
        // 来个流
        InputStream in = null;
        try {
            in = new FileInputStream(path);
            int i = 0;
            StringBuilder sb = new StringBuilder();
            // 读流
            while ((i = in.read()) != -1) {
                sb.append((char) i);
            }
            System.out.print(sb);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关流
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    in = null;
                }
            }
        }
    }

    public static void testInputStream3(String path) {
        //新建一个流
        InputStream input = null;
        try {
            input = new FileInputStream(path);
            // 读流
            int i = 0;
            byte[] b = new byte[1024];
            StringBuilder sb = new StringBuilder();
            while ((i = input.read(b)) != -1) {
                sb.append(new String(b));
            }
            System.out.println(sb);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            // 关闭流
            try {
                if (input != null) {
                    input.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                input=null;
            }
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        testInputStream3("E:\\0302\\workspace\\Day08\\src\\com\\eduask\\iodemo\\FileInputStreamDemo.java");
    }

}

2、Ouput
“context”.getBytes()将字符串转成字节

package com.eduask.iodemo;

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

public class FileOutputStreamDemo {

    public static void testFileOut(String path){
            OutputStream output=null;
            try {
                output=new FileOutputStream(path);
                String context="世界你好";
                try {
                    output.write(context.getBytes());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                if(output!=null){
                try {
                    output.flush();
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    output=null;
                }
                }

            }

    }   








    public static void main(String args[]){
        testFileOut("E:\\0302\\filedemo2\\testnihao.txt");
    }

}

3、复制

package com.eduask.iodemo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CopyDemo {
    /**
     * 
     * 要读 要写
     * 
     * @param path
     */
    public static void testReanAndWriter(String sourcePath, String toPath) {
        // 先来个读(输入)的流
        InputStream input = null;
        // 在来个写的(输出)流
        OutputStream output = null;
        try {
            input = new FileInputStream(sourcePath);
            output = new FileOutputStream(toPath);
            // 开始读
            byte[] b = new byte[1024];
            int i = 0;      
            // 开始写
            while ((i = input.read(b)) != -1);{
                output.write(b,0,i);

            } 
            output.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (output != null) {
                try {
                    output.flush();//强制输出
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    output = null;
                }
            }
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    input = null;
                }
            }
        }

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        testReanAndWriter(
                "E:\\0302\\workspace\\Day08\\src\\com\\eduask\\iodemo\\CopyDemo.java",
                "E:\\CopyDemo3.java");

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值