编程开发 > JSP > 文章内容

jsp复习资料(13)

2017-1-18编辑:ljnbset

 字符流
    1. FileReader和FileWriter
     FileReader(String filePath);
 FileReader(File fileObj);

     FileWriter(String filePath);
     FileWriter(String filePath, boolean append);
 FileWriter(File fileObj);
  
 
 例子:(以下例子是做备份,将autoexec.bat复制到autoexec.bak) 
 String file1="autoexec.bat";
 String file2="autoexec.bak";
 File inputfile=new File(file1);
 File outputfile=new File(file2);
 FileReader in=new FileReader(inputfile);
 FileWriter out=new FileWriter(outputfile);

 while ((c=in.read())!=-1) out.write((char) c);

    2. CharArrayReader和CharArrayWriter
        CharArrayReader(char array[]);
        CharArrayReader(char array[], int start, int numChars);

        CharArrayWriter();  //创建默认长度的缓冲器,
        CharArrayWriter(int numChars); //创建numChars长度的缓冲器

 例子:(以下例子过滤掉'!')
 String stmt="we are the world! we are the future!"
 byte b[]=stmt.getBytes();
 CharArrayReader in=new CharArrayReader(b);
 CharArrayWriter out=new CharArrayWriter();
 
 while ((c=in.read())!=-1) {
  if (c!='!') out.write(c);
 }
 byte c[]=new byte(out.count);
 out.toCharArray(c);
 System.out.println(c);

    3. BufferedReader和BufferedWriter
 BufferedReader(Reader inputStream);
 BufferedReader(Reader inputStream, int bufSize);

 BufferedWriter(Writer outputStream);
 BufferedWriter(Writer outputStream, int bufSize);

 例子:(将英文每句话的首字母改为大写,每句话结束标志是".!;?"符号之一) 

 String file1="chapter1.txt"
 String file2="chapter2.txt"
 FileReader inputfile=new FileReader(file1);
 BufferedReader in=new BufferedReader(inputfile);  
 FileWriter outputfile=new FileWriter(file2);
 BufferedWriter out=new BufferedWriter(outputfile);  
 boolean headflg=true;  //headflg==true表示未读入的文本处于行首位置
 while ((c=in.read())!=-1) {
  if (headflg==true) {
   if (c>='a' && c<='z') out.write((char)(c-32)); //变成大写字母并写入文件
   else out.write(c);
  }
  else out.write((char)c);

  if (c=='.' || c=='!' || c==';' || c=='?') headflg=true;
  else if (c!=' ') headflg=false;  
 }

    4. PushbackReader
 参见PushbackInputStream,两者区别在于字节流与字符流

jsp复习资料(10)

热点推荐

登录注册
触屏版电脑版网站地图