zoukankan      html  css  js  c++  java
  • JAVA笔记24-IO流(2)-节点流举例

    节点流类型

    例1:

    import java.io.*;
    public class TestFileInputStream{
        public static void main(String args[]){
            int b = 0 ;
            FileInputStream in = null ;
            try{
                in = new FileInputStream("TestFileInputStream.java");
            }catch(FileNotFoundException e){
                System.out.println("找不到文件");
                System.exit(-1);//非正常退出
            }
            try{
                long num = 0;
                while ((b=in.read()) != -1){
                    System.out.print((char)b);
                    num++;
                }
                in.close();
                System.out.println();
                System.out.println("共读取了"+num+"个字节");
            }catch(IOException e){
                System.out.println("文件读取错误");
                System.exit(-1);
            }
        }
    }

    注意:输出中文的部分显示为“?”。解决方法:用字符流。

    例2:

    import java.io.*;
    public class TestFileOutputStream{
        public static void main(String args[]){
            int b = 0 ;
            FileInputStream in = null ;
            FileOutputStream out = null ;
            try{
                in = new FileInputStream("TestFile.java");
                out = new FileOutputStream("d:/TestFile.java");
                while((b=in.read()) != -1){
                    out.write(b);
                }
                in.close();
                out.close();
            }catch(FileNotFoundException e){
            System.out.println("找不到指定文件");
            System.exit(-1);
            }catch(IOException e){
                System.out.println("文件复制错误");
                System.exit(-1);
            }
                System.out.println("文件已复制");
        }
    }

    例3

    import java.io.*;
    public class TestFileReader{
        public static void main(String args[]){
            int c = 0 ;
            FileReader fr = null ;
            try{
                fr = new FileReader("TestFile.java");
                while((c = fr.read()) != -1){
                    System.out.print((char)c);
                }
                fr.close();
            }catch(FileNotFoundException e){
            System.out.println("找不到指定文件");
            }catch(IOException e){
                System.out.println("文件读取错误");
            }
        }
    }

    输出中文可以打印出来

    例4:char 2个字节 最大65535.系统只能自动新建文件,但不能自动新建目录

    import java.io.*;
    public class TestFileWriter{
        public static void main(String args[]){
            FileWriter fw = null ;
            try{
                fw = new FileWriter("TestFile.java");
                for(int c=0;c<=50000;c++){
                    fw.write(c);
                }
                fw.close();
            }catch(IOException e){
                e.printStackTrace();
                System.out.println("文件写入错误");
                System.exit(-1);
            }
        }
    }

    例5:复制文件的练习题 使用FileReader FileWriter

    import java.io.*;
    public class TestCopyByMyself{
        public static void main(String args[]){
            FileReader fr = null ;
            FileWriter fw = null ;
            int c = 0;    
            try{
                fr = new FileReader("TestCopyByMyself.java");
                fw = new FileWriter("TestFile.java");//文件不存在的话会自动生成,但目录如果不存在不可以自动生成
                while((c=fr.read()) != -1){
                    fw.write(c);
                }
                fr.close();
                fw.close();
            }catch(FileNotFoundException e){
                System.out.println("找不到文件");
                System.exit(-1);//非正常退出
            }catch(IOException e){
                System.out.println("文件复制错误");
                System.exit(-1);
            }
            System.out.println("文件已复制");
        }
    }
  • 相关阅读:
    JS: 子项可以来回交换的两个下拉列表
    DOM事件
    DOM基础2——元素
    DOM基础1
    JS: 随机点名程序与万年历
    G_S男女匹配算法(算法的第一个程序2016.09.19)
    Java IO流详尽解析(大神之作)
    细讲解JAVA中的IO流
    c++运算符的优先级(收好不谢)
    java程序——输出当月日历表
  • 原文地址:https://www.cnblogs.com/seven7seven/p/3680767.html
Copyright © 2011-2022 走看看