zoukankan      html  css  js  c++  java
  • JAVA--IO

      经过一天的学习,基本已经对JAVA--IO有了一定的认识,现在就和大家分享一下。

      JAVA.IO包中定义了多种流的类型(类或抽象类)来实现输入和输出功能,可以从不同的角度对其进行分类:

      按数据流的方向不同可以分为输入流和输出流

      按处理数据单位的不同可以分为字节流和字符流

      按照功能的不同可以分为节点流和处理流

    上图就是JAVA--io中流的分类;看到上图你一定想问输入流和输出流是以什么来区分的,输入流和输出流均是以程序的角度来度量的。

    闲话少聊,现在直奔主题:

    一、读取指定文件中的内容(以字节流的方式读取)

    public class testFileInputstream {
    
        /**文件输入流(字节流输入)
         * @param args
         */
        public static void main(String[] args) {
            FileInputStream input = null;
            int b = 0;
            try {
                input = new FileInputStream("E:\JSP\jsp_demo001\index.jsp");
            } catch (FileNotFoundException e) {
                System.out.println("文件不存在");
                System.exit(0);
            }
            long num = 0;
            try {
                while((b=input.read())!=-1){
                    System.out.print((char)b);
                    num++;
                }
                input.close();
                System.out.println();
                System.out.println("共读取:"+num+"个字节");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    代码不难,大家如有疑问请留言。

    二、读取指定文件中的内容(以字符流的方式读取)

    public class Filereader {
    
        /**文件输入流(字符流输入)
         * @param args
         */
        public static void main(String[] args) {
            FileReader input = null;
            int b = 0;
            try {
                input = new FileReader("E:\JSP\jsp_demo001\index.jsp");
            } catch (FileNotFoundException e) {
                System.out.println("文件不存在");
                System.exit(0);
            }
            long num = 0;
            try {
                while((b=input.read())!=-1){
                    System.out.print((char)b);
                    num++;
                }
                input.close();
                System.out.println();
                System.out.println("共读取:"+num+"个字节");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    在这里给大家说明一点,字节流和字符流的区别,字节流就是一个字符一个字符的来读;字符流就不同的,它是一个字符一个字符的来读;

    三、文件输出流(以字节的方式)

    public class testFileOutputStream {
    
        /**文件输出流(字节流)
         * @param args
         */
        public static void main(String[] args) {
            int b = 0;
            FileInputStream input = null;
            FileOutputStream output = null;
            try {
                input = new FileInputStream("E:\JSP\jsp_demo001\index.jsp");
                //如果指定路径下不存在该文件,系统会自动为我们创建一个文件
                output = new FileOutputStream("E:\JSP\jsp_demo001\index1.jsp");
                //设置为true的话,会在指定文件下进行复制,不删除原文件
                //output = new FileOutputStream("E:\JSP\jsp_demo001\index1.jsp", true);
            } catch (FileNotFoundException e) {
                System.out.println("指定文件不存在");
                System.exit(0);
            }
            try {
                while((b=input.read())!=-1){
                    output.write((char)b);
                }
                input.close();
                output.close();
            } catch (IOException e) {
                System.out.println("文件复制错误");
                System.exit(0);
            }
            System.out.println("文件复制完成");
        }
    }

    四、文件输出流(以字符的方式)

    public class Filewriter {
    
        /**文件输入流(字符流输出)
         * @param args
         */
        public static void main(String[] args) {
            FileWriter output = null;
                try {
                    output = new FileWriter("E:\JSP\jsp_demo001\cn.txt");
                } catch (IOException e) {
                    System.out.println("文件路径有问题");
                    System.exit(0);
                }
            try {
                    for(int i=0 ; i<50000; i++){
                        output.write(i);
                    }
                output.close();
                System.out.println("保存完成");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    最后给大家介绍一个关于视频文件的读取和保存:

    public class FileOutputStream_media {
    
        /**文件输出流(字节流)
         * @param args
         */
        public static void main(String[] args) {
            int b = 0;
            FileInputStream input = null;
            FileOutputStream output = null;
            try {
                input = new FileInputStream("E:\JSP\jsp_demo001\move.avi");
                //如果指定路径下不存在该文件,系统会自动为我们创建一个文件
                output = new FileOutputStream("E:\JSP\jsp_demo001\move1.avi");
            } catch (FileNotFoundException e) {
                System.out.println("指定文件不存在");
                System.exit(0);
            }
            try {
                while((b=input.read())!=-1){
                    output.write((char)b);
                }
                input.close();
                output.close();
            } catch (IOException e) {
                System.out.println("文件复制错误");
                System.exit(0);
            }
            System.out.println("文件复制完成");
        }
    }

    我不喜欢长篇大论的文字,就喜欢这种直接完整的代码,当然上面的每个地方我都做了注释,如果在你的学习过程中有什么疑问,请留言,我一定会尽快给你答复。

    在此声明:以上内容如有不当,请及时留言,谢谢。

  • 相关阅读:
    html5-1 网页结构描述
    [搜索] hdu 4016 Magic Bitwise And Operation
    备忘录模式设计模式入门Memento
    编译的依赖不能vs的release工程
    【web开发学习笔记】Structs2 Action学习笔记(两)
    ios学习网络------4 UIWebView以三种方式中的本地数据
    坑爹BUG,没有详细的看还真看不出问题
    《Effective C++》:规定44-规定45
    [ACM] poj 1088 滑雪 (内存搜索DFS)
    Balanced Binary Tree(Java代码没有结束,是什么原因???)
  • 原文地址:https://www.cnblogs.com/AndroidJotting/p/3938250.html
Copyright © 2011-2022 走看看