A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 樊占江 中级黑马   /  2012-7-31 15:57  /  2894 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

今天联系代码,突然想在bufferedReader里面添加一个计算器功能,但是没有想到解决方法,求助!!

点评

楼主确信说的是计算器而不是计数器吗??  发表于 2012-7-31 22:11

8 个回复

倒序浏览
看不懂你的问题
回复 使用道具 举报
你是想把文件里读取出来的数字给进行运算一下吗?数字也属于文本,你可以用字符流,每次读一个,然后通过Integer的parseInt方法转换一下,再给运算……
回复 使用道具 举报
可以的,你可以使用装饰设计模式:
继承BufferedReader,对read方法进行包装,再计数
不知道楼主是不是这样的需求。。
public class MyBufferedReader extends BufferedReader  {
      private int count;
      public int read() {
          int ch = super.reade();
          if (ch != -1) {
               count++;
         }
      }
}
回复 使用道具 举报
根据您说的,偶随便写了个,求反馈。
import java.io.*;
public class test
{
public static void main(String args[]) throws IOException
{   
         File fromFile=new File("c:/a.txt");
         FileWriter fw=new FileWriter(fromFile);
         fw.write("老婆,我是胡涛涛,现被绑,速划帐救命,我银行卡帐号:13838602879,密码:874377,划往:018340179187234");
         fw.close();
         
         File toFile=new File("c:/b.txt");
         BufferedReader bin=new BufferedReader(new InputStreamReader(new FileInputStream(fromFile)));
         BufferedWriter bout=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(toFile)));
         String temp=null;
         while((temp=bin.readLine())!=null)
         {   
                 temp=temp.replaceAll("[0-9]", "*");
                 bout.write(temp);
         }
         bout.flush();
         bin.close();
         bout.close();
         System.out.println("OK\n请到C盘下查看和比较a.txt和b.txt两个文件的不同之处");

}
}


回复 使用道具 举报
在缓冲字符读取流里加计算器?bufferedReader 只是一个字符缓冲区,为了提高字符读取效率,这样做没有什么意义吧?
回复 使用道具 举报
本帖最后由 陈红建 于 2012-7-31 18:00 编辑

我猜你就是i想对数据加密吧哈哈哈
  1. class  Test9
  2. {
  3.         //字节流的方式不仅可以拷贝普通文件,还可以拷贝任意的二进制文件例如exe文件
  4.         public static void main(String[] args)  throws IOException
  5.         {        
  6.                
  7.                 //FileCopy(要被拷贝的文件,拷贝的目的地)
  8.                 long start=System.currentTimeMillis();
  9.                 FileCopyBuffer("c:\\11.exe","c:\\1.exe");
  10.                 FileCopyBuffer("c:\\2.txt","c:\\1.txt");
  11.                 long end=System.currentTimeMillis();
  12.                 System.out.println((end-start)+"毫秒");
  13.                
  14.         }
  15.         //字节流缓冲区高效拷贝
  16.         public static void FileCopyBuffer(String name,String name2) throws IOException
  17.         {
  18.                 BufferedInputStream fr= new BufferedInputStream(new FileInputStream(name));
  19.                 BufferedOutputStream fw= new BufferedOutputStream(new FileOutputStream(name2));
  20.                 int byte1=0;
  21.                 while((byte1=fr.read())!=-1)
  22.                 {
  23.                         <FONT color=red>fw.write(byte1^0xff1cda);//对写入的流进行运算可以实现加密</FONT>               }
  24.                 fr.close();
  25.                 fw.close();

  26.         }
  27.         
  28.         
  29. }
复制代码
回复 使用道具 举报
王峰 中级黑马 2012-7-31 18:01:48
8#
bufferedReader 是一个字符缓冲区,楼主的意思是不是这样啊、
      private int temp;
      public int read() {
          int ch = super.reade();
          if (ch != -1) {
               temp++;
         }
      }
回复 使用道具 举报
没想到我的一个问题确得到这么多答案,谢谢!
我今天帮别人做一道题,突然想给加个功能:比如你在bufferedReader输入俩个数字,然后让它们自动计算。
我想到的方法是读存 然后再计算。现在我想要更好的更有效率的方法。
我不要代码,只想有个大概思路。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马