Java输入/输出流体系

在用java的io流读写文件时,总是被它的各种流能得很混乱,有40多个类,理清啦,过一段时间又混乱啦,决定整理一下!以防再忘

Java输入/输出流体系

1.字节流和字符流

字节流:按字节读取。字符流:按字符读取。

字符流读取方便,字节流功能强大,当不能用字符流时,可以用字节流。

字节流基类:InputStream、OutputStream

 InputStream方法:

 

OutputStream方法:

 

字符流基类:Reader、Writer

Reader方法:

 

Writer方法:

 

2.输入输出流体系

 

3.转换流

InputStreamReader:字节输入流->字符输入流的转换

OutputStreamWriter:字节输出流->字符输出流的转换

例如:字节输入流->字符输入流的转换

字节流使用不方便,而普通的Reader读取也不太方便,转换成BufferReader,利用BufferReader的readLine(),一次读取一行。

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                String result = "";
                String line = "";
               while((line = reader.readLine())!= null){
                   result = result+ line;
               }
               System.out.println(result);

InputStream to String:

private static String changeInputeStream(InputStream inputStream,String encode) {
       //通常叫做内存流,写在内存中的
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] data = new byte[1024];
        int len = 0;
        String result = "";
        if(inputStream != null){
            try {
                while((len = inputStream.read(data))!=-1){
                    outputStream.write(data, 0, len);
                }
                //result是在服务器端设置的doPost函数中的
                result = new String(outputStream.toByteArray(),encode);
                outputStream.flush();
                outputStream.close();
                inputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return result;
    }

 

InputStream->StringBuffer:

StringBuffer buffer = new StringBuffer();
        InputStreamReader reader =new InputStreamReader(request.getInputStream(),"utf-8");
        char[] requestchar = new char[5];
        if(reader != null){
            System.out.println("默认编码:"+reader.getEncoding());//默认编码:UTF8
        }
        //buffer reader.readLine()
        while((temp = reader.read(requestchar))!=-1){
            buffer.append(requestchar, 0, temp);
            System.out.println(buffer.toString());
        }
        String result = buffer.toString();

4.文件读写:

class Kaoshi
{public static void main(String[] args){
  Scanner in=new Scanner(System.in);
  int score=0;                            //分数;
  StringBuffer answer=new StringBuffer();//存放用户答案,动态字符串;
  String result="ACDD";                  //正确答案;
try{                                       
  FileReader file=new FileReader("Test.txt");     //打开文件;
  BufferedReader intw=new BufferedReader(file);   //缓冲流,按行读;
  String s=null;                        
  while((s=intw.readLine())!=null){              //判断不为空;
    if(!s.startsWith("*")){                      
      System.out.println(s);  
    }
    else{
        System.out.println("请输入正确答案:");
         String str=in.next();
         char c= str.charAt(0);//收集答案;
         answer.append(c);
    }
  }
            
  }catch(IOException e){
     e.printStackTrace();
  } 
  for(int i=0;i<result.length();i++){
     if(result.charAt(i)==answer.charAt(i)||
         result.charAt(i)==answer.charAt(i)-32){
        score+=25;
     }

  }
  System.out.println(score);
} 
}

 

posted @ 2016-03-21 11:32  超超boy  阅读(2349)  评论(0编辑  收藏  举报