A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 田策 中级黑马   /  2015-10-15 22:27  /  265 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package com.heima.stream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;

public class wode2 {

        /**
         * @param args
         * @throws IOException
         * @throws InterruptedException
         */
        public static void main(String[] args) throws IOException, InterruptedException {
                //demo();
                //demo2();
                //demo3();
                //demo4();
                //demo5();
                //demo6();
                //demo8();
                //demo9();
               
       
                }

        private static void demo9() throws FileNotFoundException, IOException {
                BufferedInputStream bis = null;
                BufferedOutputStream bos = null;
                try{
                 bis = new BufferedInputStream(new FileInputStream("963.txt"));
             bos = new BufferedOutputStream(new FileOutputStream("jjj.txt",true));
                int b ;
                while((b = bis.read()) != -1){
                        bos.write(b);
                }
               
                }finally{
                        try{
                                if(bis != null)
                                        bis.close();
                        }finally{
                                if(bos != null)
                        bos.close();
                        }
                }
        }

        private static void demo8() throws FileNotFoundException, IOException {
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("963.txt"));
                bos.write("你好吗,我很好!".getBytes());
                bos.write("\r\n".getBytes());
                bos.close();
        }

        private static void demo7() throws FileNotFoundException, IOException {
                //BufferedInputStream bis = new BufferedInputStream(new FileInputStream("双元.jpg"));
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("copy.jpg"));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy1.jpg"));
                int b ;
                while((b = bis.read()) != -1){
                        bos.write(b^3);
                }
                bis.close();
                bos.close();
        }
        private static void demo6() throws FileNotFoundException, IOException {
                File file = getFile();
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName()));
                int b ;
                while((b = bis.read() ) != -1){
                        bos.write(b);
                }       
                bis.close();
                bos.close();
                BufferedInputStream s = new BufferedInputStream(new FileInputStream("计算机网络.doc"));
                BufferedOutputStream t = new BufferedOutputStream(new FileOutputStream("t.doc"));
                int c ;
                while((c = s.read() ) != -1){
                        t.write(c);
                }       
                s.close();
                t.close();
        }
    public static File getFile(){
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入一个文件路径:");
            while(true){
                    String s = sc.nextLine();
                    File file = new File(s);
                    if(!file.exists()){
                            System.out.println("文件路径不存在");
                    }else if(file.isDirectory()){
                            System.out.println("您录入的文件路径不存在,请重新录入:");
                    }else{
                            return file;
                    }
            }
    }
        private static void demo5() throws FileNotFoundException, IOException {
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("copy.jpg"));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy1.jpg"));
                int b;
                //Random r = new Random(456);
                //int a= r.nextInt();
                while((b = bis.read()) != -1){
                        bos.write(b^123);
                }
                bis.close();
                bos.close();
        }

        private static void demo4() throws FileNotFoundException, IOException {
                FileInputStream fis = new FileInputStream("963.txt");
                FileOutputStream fos = new FileOutputStream("jjj.txt");
                int n ;
                while((n = fis.read()) != -1){
                        fos.write(n);
                }
                fis.close();
                fos.close();
        }

        private static void demo3() throws FileNotFoundException, IOException {
                FileInputStream fis = new FileInputStream("jjj.txt");
                byte[] arr = new byte[3];
                int len;
                while((len = fis.read(arr)) != -1){
                        System.out.println(new String(arr,0,len));
                }
                fis.close();
        }

        private static void demo2() throws FileNotFoundException, IOException {
                FileInputStream fis = new FileInputStream("text.txt");
                FileOutputStream fos = new FileOutputStream("963.txt");
                BufferedInputStream bis = new BufferedInputStream(fis);
                BufferedOutputStream bos = new BufferedOutputStream(fos);
                byte[] arr = new byte[1024];
                int len;
                while((len = bis.read(arr)) != -1){
                        bos.write(arr,0,len);
                }
                bis.close();
                bos.close();
        }

        private static void demo() throws FileNotFoundException, IOException {
                FileInputStream fis = new FileInputStream("yyy.txt");
                FileOutputStream fos = new FileOutputStream("123.txt");
                byte[] arr = new byte[1024];
                int b;
                while((b = fis.read(arr)) != -1){
                        fos.write(arr,0,b);
                }
                fis.close();
                fos.close();
        }

}

1 个回复

倒序浏览
求大神指点,多谢!!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马