字符流与字节流练习

import java.io.*;
import java.io.IOException;
class IODemo
{
    public static void main(String[] args)throws IOException
    {
        method_14();
    }
    public static void method_15()throws IOException
    {
        FileReader fr=new FileReader("a.txt");        
        BufferedReader bufr=new BufferedReader(fr);
        String line=null;
        while((line=bufr.readLine())!=null)
        {
            System.out.println(line);
        }
        
//        char[] ch=new char[1024];
//        int num=0;
//        while((num=fr.read(ch))!=-1)
//        {
//            System.out.println(new String(ch,0,num));
//        }
        
        
        
//        char[] ch=new char[1024];
//        int len=fr.read(ch);
//        String str=new String(ch,0,len);
//        System.out.println(str);
        fr.close();
    }
    public static void method_14()throws IOException
    {
        InputStreamReader osr=new InputStreamReader(new FileInputStream("a.txt"),"utf-8");
        char[] ch=new char[1024];
        int len=osr.read(ch);
        String str=new String(ch,0,len);
        System.out.println(str);
        osr.close();
    }

    public static void method_13()throws IOException
    {
        OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("a.txt"),"gbk");
        osw.write("你好");
        osw.close();
    }
    public static void method_12()throws IOException
    {
        FileWriter fw=new FileWriter("a.txt");
        fw.write("你好");
        fw.flush();
        fw.close();
    }        
    //将一个文件复制到另一个文件中
    public static void method_11()throws IOException
    {
        BufferedReader bufr=new BufferedReader(new InputStreamReader(new FileInputStream("a.txt")));
        BufferedWriter bufw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("copy_b.txt")));
        String line=null;
        while((line=bufr.readLine())!=null)
        {
            if("over".equals(line))
                break;
            bufw.write(line.toUpperCase());
            bufw.newLine();
            bufw.flush();
        }
        bufr.close();
        bufr.close();
    }        
    //将文本数据打印到控制台
    public static void method_10()throws IOException
    {
        BufferedReader bufr=new BufferedReader(new InputStreamReader(new FileInputStream("a.txt")));
        BufferedWriter bufw=new BufferedWriter(new OutputStreamWriter(System.out));
        String line=null;
        while((line=bufr.readLine())!=null)
        {
            if("over".equals(line))
                break;
            bufw.write(line.toUpperCase());
            bufw.newLine();
            bufw.flush();
        }
        bufr.close();
        bufw.close();
    }
    //将键盘录入的数据写入到文本        
    public static void method_9()throws IOException
    {
        BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("a.txt")));
        String line=null;
        while((line=bufr.readLine())!=null)
        {
            if("over".equals(line))
                break;
            bufw.write(line.toUpperCase());
            bufw.newLine();
            bufw.flush();
        }
        bufr.close();
        bufw.close();
    }        
    public static void method_8()throws IOException
    {
        BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufw=new BufferedWriter(new OutputStreamWriter(System.out));
        String line=null;
        while((line=bufr.readLine())!=null)
        {
            if("over".equals(line))
                break;
            bufw.write(line.toUpperCase());
            bufw.newLine();
            bufw.flush();
        }
        bufr.close();
        bufw.close();
    }
    public static void method_7()throws IOException
    {
        InputStream in=System.in;
        OutputStream out=System.out;
        BufferedReader bufr=new BufferedReader(new InputStreamReader(in));
    
        OutputStreamWriter osw=new OutputStreamWriter(out);
        BufferedWriter bufw=new BufferedWriter(osw);
        String line=null;
        while((line=bufr.readLine())!=null)
        {
            if("over".equals(line))
                break;
//            bufw.write(line.toUpperCase()+"\r\n");
//            bufw.flush();
            bufw.write(line.toUpperCase());
            bufw.newLine();
            bufw.flush();
        }
    }
    public static void method_6()throws IOException
    {
        InputStream in=System.in;
    
        BufferedReader bufr=new BufferedReader(new InputStreamReader(in));
        String line=null;
        while((line=bufr.readLine())!=null)
        {
            if("over".equals(line))
                break;
            System.out.println(line.toUpperCase());
        }
    }
    public static void method_5()throws IOException
    {
        StringBuilder sb=new StringBuilder();
        InputStream in=System.in;
        int ch=0;
        while((ch=in.read())!=-1)
        {
            if(ch=='\r')
                continue;
            if(ch=='\n'){
        
                String temp=sb.toString();
                if("over".equals(temp))break;
                System.out.println(temp.toUpperCase());
                sb.delete(0,sb.length());
            }
            else
                sb.append((char)ch);
            
        }
        
        
    }
    public static void method_4()throws IOException
    {
        FileInputStream in=new FileInputStream("a.txt");
        BufferedInputStream bis=new BufferedInputStream(in);
        
        FileOutputStream out=new FileOutputStream("c.txt");
        BufferedOutputStream bos=new BufferedOutputStream(out);
        byte[] by=new byte[1024];
        int num=0;
        while((num=bis.read(by))!=-1)
        {
            System.out.println(new String(by,0,num));
            bos.write(by,0,num);
            bos.flush();
        }
        bis.close();
        bos.close();
    }
    public static void method_3()throws IOException
    {
        FileReader fir=new FileReader("a.txt");
        BufferedReader bufr=new BufferedReader(fir);
        
        FileWriter fiw=new FileWriter("copy_a.txt");
        BufferedWriter bufw=new BufferedWriter(fiw);
        char[] ch=new char[1024];
        int num=0;
        while((num=bufr.read(ch))!=-1)
        {
            System.out.println(new String(ch,0,num));
            bufw.write(ch,0,num);
        }
        bufr.close();
        bufw.close();
    }

    public static void method_2()throws IOException
    {
        FileReader fir=new FileReader("a.txt");
        BufferedReader bufr=new BufferedReader(fir);
    
        int num=0;
        while((num=bufr.read())!=-1)
        {
            System.out.println((char)num);
        }
        bufr.close();
    }
    public static void method_1()throws IOException
    {
        FileReader fir=new FileReader("a.txt");
        BufferedReader bufr=new BufferedReader(fir);
        char[] ch=new char[1024];
        int num=0;
        while((num=bufr.read(ch))!=-1)
        {
            System.out.println(new String(ch,0,num));
        }
        bufr.close();
    }
}

posted on 2012-10-17 17:03  蝌蚪的精神  阅读(162)  评论(0编辑  收藏  举报

导航