zoukankan      html  css  js  c++  java
  • 字符流和字节流

    字符流

    1.1Reader/Write(字符流的输入和输出流,相对于java文件而言)
     1.2能够用TXT编辑器打开的文件,且不乱码就是字符文件可以用字符流来操作
      而不能打开的文件,则是字节文件。
     2.输入流Reader类(抽象类)
      2.1常用方法

        int read();  //读取单个字符

        int read(byte [] c)  //返回实际读取的字符数

        int read(char[] c,int off,int len)    

        void colse()    //关闭流

      2.2FileReader类(是Reader的子类)

      2.2.1 读取方法:File f=new File("文件路径");

                Reader fr=new FileReader("文件路径"/f);

    Reader fr=null;
          StringBuffer sbf=null;
                try {
               fr=new FileReader("D:\Test\test.txt");
               sbf=new StringBuffer();
               int length;
               while((length=fr.read())!=-1){
                char c=(char)length;
                sbf.append(c);
           }
             System.out.println(sbf.toString()); //输出数据
            } catch (FileNotFoundException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
      }   catch (IOException e) {
           // TODO Auto-generated catch block
             e.printStackTrace();
            }finally{
               if(fr!=null){
                try {
               fr.close();
              } catch (IOException e) {
             // TODO Auto-generated catch block
                 e.printStackTrace();
          }
           }
          }
    示例

    2.3字符输入流(BufferedReader类)

        2.3.1:优势:提高了读取文件的效率。

        2.3.2:创建对象:

          Reader fr=new FileReader("文件路径(相对或绝对路径)");

          BufferedReader br=new BufferedReader(fr);或BufferedReader br=new BufferedReader(new FileReader(“文件路径(相对或绝对路径)”));  

        2.3.3:   br.readerLine();    读取一行数据

    Reader fr=null;
            BufferedReader br=null;
            try {
             //先创建一个FileReader对象
             fr=new FileReader("D:\Test\test.txt");
       
             //在创建一个BufferedReader对象
             br=new BufferedReader(fr);
       
             //读取每一行数据
             String line=br.readLine();
             while(line!=null){
                System.out.println(line);
              line=br.readLine();//再次赋值进行判断
               }
            } catch (FileNotFoundException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
            }finally{
               try {
            br.close(); //先关
            fr.close();
             } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
                 }
                }
    示例

    3.字符输出流Writer类

        3.1常用方法:

          write(String s);      将str字符串里包含的字符输出到指定的输出流中

          write(String s,int off,int len)

          void close()    关闭输出流

          void flush()    刷新输出流

        3.2FilerWrite(是write的子类)

        3.3读取方法:

          Writer fr=new FileWriter("文件路径");

    Writer fw=null;
      
           try {
           //创建一个FileWrite对象
             fw=new FileWriter("D:\Test\简介.txt");
       
           //写入信息
             fw.write("我热爱我的团队!"+"
    ");
             fw.write("张三"+"
    ");  //
    进行换行
             fw.write("18岁");
             fw.flush();  //刷新缓冲区
            } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
              }finally{
               try {
                fw.close();
             } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
           }
            }
    示例

    3.4:字符输出流BufferedWrite类  

          3.4.1:创建对象:

            BufferdWriter bw=new BufferedWriter(new FileWrite(“文件路径”))  

    Writer fw=null;
          BufferedWriter bw=null;
      
            try {
               fw=new FileWriter("D:\Test\hello.txt");
       
                 bw=new BufferedWriter (fw);
       
               //写入信息
             bw.write("大家好");
             bw.write("我正在学习BufferedWriter。");
             bw.newLine();  //换行
             bw.write("请多多指教!");
             bw.flush();  //刷新缓冲区
             bw.close();  //关闭流
             System.out.println("成功插入数据!");
       
             //读取文件内容
             FileReader fr=new FileReader("D:\Test\hello.txt");
             StringBuffer sbf=new StringBuffer();
             int length;
             while((length=fr.read())!=-1){
              char c=(char)length;
              sbf.append(c);
             }
           System.out.println(sbf.toString());
          } catch (IOException e) {
           // TODO Auto-generated catch block
             e.printStackTrace();
            }
    示例

    字节流 

    2.1:字节输入流(InputStream):   

      2.1.1: 常用方法(数据从文件到java代码中)          

         int read(); //读取一个字节          

         int read(byte[]); //读取一串字节           

        long avaliable; //文件长度

       2.1.2:   FileInptstream(字节文件输入流,是InputStream的子类)           

        new FileInptstream(File)           

         new FileInptstream("文件路径+文件名")

    //声明流对象  
    
         FileInputStream fis=null;   
    
        try {    fis =new FileInputStream("D:\Test\hello.txt");   
    
         int data;    System.out.println("文件内容为:");        //循环读取数据        
    
        while((data=fis.read())!=-1){     
    
           System.out.print((char)data+"");  
    
           }  
    
             }catch (FileNotFoundException e) {  
    
          // TODO Auto-generated catch block    
    
            e.printStackTrace();  
    
         }      catch (IOException e) {   
    
           // TODO Auto-generated catch block   
    
               e.printStackTrace();   
    
           } finally {   
    
            if(fis!=null){   
    
             try {    
    
                 fis.close();   
    
               } catch (IOException e) {  
    
                 // TODO Auto-generated catch block     
    
                e.printStackTrace();    }
    
            }
    
             }
    示例

    2.2:字节输出流(OutPutStream):   

        2.2.1: 常用方法:(数据从java代码中,写到文件或其他介质中)          

             void write (字节);//写入一个字节         

             void write(byte[]); //写入字节数组

        2.2.2: FileOutputStream(字节文件输出流,是OutputStream的子类)          

            new FileOutputStream(file)        

             new FileOutputStream("文件路径+文件名")        

             new FileOutputStream("文件路径+文件名",boolean) 

     注意:a.boolean:表示是否向文件末尾追加,如果是true,表示追加,false表示不追加(也就是覆盖),默认值为false

             b.创建FileOutputStream实咧时,如果相应的文件并不存在,则会自动创建一个空的文件。

    //  File file=new File("D:\Test");
    
      //  showInfo(file);   
    
       //非菠萝契数列:1 1 2 3 5 8 ......   
    
      //int [] muns=new int[10]; //定义数组   
    
      Scanner input=new Scanner(System.in);
    
        System.out.println("请输入您要计算的位置:");  
    
       int a=input.nextInt();  
    
       System.out.println(suan(a-1));
    
     }  
    
     
    
     private static void showInfo(File f){  
    
         File [] file=f.listFiles();   
    
        for(int i=0;i<file.length;i++){   
    
           System.out.println(file[i].getName()); //得到一级目录中的文件和文件夹   
    
           if(file[i].isDirectory()){ //判断一级目录中是否含有文件夹    
    
             showInfo(file[i]);//调用自己方法    }
    
            }  
    
        }  
    
         private static  int suan(int i){  
    
           if(i==0 || i==1){   
    
             return i=1;   }else{
    
              return suan(i-2)+suan(i-1);  
    
         }
    递归算法
  • 相关阅读:
    CF 461B Appleman and Tree
    POJ 1821 Fence
    NOIP 2012 开车旅行
    CF 494B Obsessive String
    BZOJ2337 XOR和路径
    CF 24D Broken robot
    POJ 1952 BUY LOW, BUY LOWER
    SPOJ NAPTIME Naptime
    POJ 3585
    CF 453B Little Pony and Harmony Chest
  • 原文地址:https://www.cnblogs.com/xykwh/p/7019740.html
Copyright © 2011-2022 走看看