我曾试过将变量newstext清空不过没解决问题,请大家指点下

解决方案 »

  1.   

    用DataInputStream in =new DataInputStream(new BufferedInputStream(new FileInputStream(path))); 试试
      

  2.   

    能烦各位告知怎么初始化吗?我刚接触不久没什么经验,我刚开始以为是newstext没清空的关系就加了if(newstext!=null)
          {
            newstext=new StringBuffer();
          }
    来清空也试过其他办法来清空,虽然能得到正确的i的值但是到newstext=new StringBuffer(new String(buff1))还是在后面追加了前一文件的内容,请大家能否说详细点
      

  3.   

    你这个不是工具类吗?写成这样的不就行了:
     public static String gettext()
      {
    StringBuffer sb = new StringBuffer();
        try
        {
          FileInputStream news = new FileInputStream(path);
          int  i= news.read(buff1);
          if (i != -1)
          {
            //newstext=new StringBuffer(new String(buff1));
    sb=sb.append(new String(buff1));
          }
          news.close();
          //String text = new String(newstext);
          //return text;
          return sb.toString();
        }
        catch (Exception e)
        {
          System.out.println(e);
        }
        return null;
      }
      

  4.   

    可能是int  i= news.read(buff1);这句出了问题,第二次读数据的时候没有清空buff1。buff1可以不要在声明时初始化,可以在函数中使用时初始化。试试看,希望可以成功。
      

  5.   

    你将byte buff1[] = new byte[99999];的定义放在public String gettext()方法里不就每次调用的时候就是一个新的buff1了么?这样来试试,看行不?
      

  6.   

    package com.mdao.emap.test;/**
     * news
     * @author 
     * @version 1.1
     */
    import java.io.*;public class News {
      private String path;  private StringBuffer newstext = new StringBuffer();  byte buff1[];  public void setpath(String path) {
        this.path = path;
      }  public String getpath() {
        return path;
      }  public String gettext() {
        try {
          buff1 = new byte[1000];
          
          FileInputStream news = new FileInputStream(path);
          int i = news.read(buff1);
          if (i != -1) {
            newstext = new StringBuffer(new String(buff1));
          }
          news.close();
          String text = new String(newstext);
          return text;
        } catch (Exception e) {
          System.out.println(e);
        }
        return null;
      }  public static void main(String args[]) {    
        News news = new News();
        news.setpath("c:\\1.txt");
        System.out.println("开始:" + news.gettext());
        news.setpath("c:\\2.txt");
        System.out.println("结束:" + news.gettext());
      }
    }试试看,呵呵,我可以了
      

  7.   

    解决了就好,不客气啊,呵呵。好久不来CSDN了,今天是重返Csdn的第一天,希望能和大家共同学习Java!