| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 472 人关注过本帖
标题:关于使用 FileInputStream 的问题, 如何实现打开文件一次之后所有 method ...
只看楼主 加入收藏
sarahman
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-11-13
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
关于使用 FileInputStream 的问题, 如何实现打开文件一次之后所有 method 都无须再打开文件就能用。
新手求助
要求只能打开文件1次。我不理解的是为什么如果我不在每个method里打开一次文件,那个method就不能正常运行?
假设我在getVowelCount()那个method里不再次打开文件,这个method就不能正确运作?
相反,在这个method里打开一次文件,就能正确执行命令。
求解释和解决方法。
万分感谢!🙏
程序代码:
import java.util.Scanner;
import import public class TextFileAnalyzer {

    private String file = null;
    private int charCount = 0;
    private int lnCount = 0;
    private int stnCount = 0;
    private int volCount = 0;
    private int i = 0;
    private char ctr;
    private int wrdCount = 0;
    private String wrd = null;
    private FileInputStream input;
    private Scanner Input;


    public TextFileAnalyzer(String filePath) throws Exception {
      this.file = filePath;
      input = new FileInputStream(file);
      Scanner Input = new Scanner(input);
    }

    public int getCharCount() throws IOException {
      for (int c = input.read(); c != -1; c = input.read()){
        charCount++;
      }
      return charCount;
    }

    public int getVowelCount() throws IOException {
      input = new FileInputStream(file);
      byte[] b = new byte[input.available()];
      input.read(b);
      for (i = 0; i < b.length; i++) {
        if(b[i] == 'A' || b[i] == 'a' || b[i] == 'E' ||
          b[i] == 'e' || b[i] == 'I' || b[i] == 'i' ||
            b[i] == 'O' || b[i] == 'o' || b[i] == 'U' ||
              b[i] == 'u') {
        volCount ++;
        }
      }
      return volCount;
    }
    
        public int getWordCount() throws IOException {
      input = new FileInputStream(file);
      Scanner Input = new Scanner(input);
      while (Input.hasNextLine()) {
        wrd = Input.next();
        wrdCount++;
      }
      Input.close();
      return wrdCount;
    }

    public int getLineCount() throws IOException {
      input = new FileInputStream(file);
      byte[] b = new byte[input.available()];
      input.read(b);
      for (i = 0; i < b.length; i++) {
        if(b[i] == '\n') {
          lnCount++;
        }
      }
      return lnCount;
    }

    public int getSentenceCount() throws IOException {
      input = new FileInputStream(file);
      byte[] b = new byte[input.available()];
      input.read(b);
      for (i = 0; i < b.length; i++) {
        if(b[i] == '.' && b[i] != b[ i - 1]) {
          stnCount++;
        }
      }
      return stnCount;
    }

    public String getFilePath() {
      return file;
    }


    public String toString() {
      return file + ": " + charCount + "c " + volCount + "v " 
        + wrdCount + "w " + lnCount + "l " + stnCount + "s ";
    }
          public static void main(String[] args) throws Exception {
          String path = "Desktop/TextFile.txt";
      TextFileAnalyzer analysis = new TextFileAnalyzer(path);
      if (analysis.getCharCount() == 523099) {
        System.out.println("getCharCount seems OK.");
      } else {
        System.out.println("getCharCount has problems.");
      }
      if (analysis.getVowelCount() == 159206) {
        System.out.println("getVowelCount seems OK.");
      } else {
        System.out.println("getVowelCount has problems.");
      }
      if (analysis.getWordCount() == 88147) {
        System.out.println("getWordCount seems OK.");
      } else {
        System.out.println("getWordCount has problems.");
      }
      if (analysis.getLineCount() == 8425) {
        System.out.println("getLineCount seems OK.");
      } else {
        System.out.println("getLineCount has problems.");
      }
      if (analysis.getSentenceCount() == 3677) {
        System.out.println("getSentenceCount seems OK.");
      } else {
        System.out.println("getSentenceCount has problems.");
      }
      if (analysis.getFilePath().equals(path)) {
        System.out.println("getFilePath seems OK.");
      } else {
        System.out.println("getFilePath has problems.");
      }
      System.out.println(analysis);
      }
  }


[ 本帖最后由 sarahman 于 2014-11-14 11:14 编辑 ]
搜索更多相关主题的帖子: method color 如何 
2014-11-13 16:15
hhwz
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:87
帖 子:687
专家分:4502
注 册:2011-5-22
收藏
得分:20 
没有编译成CLASS
2014-11-14 09:32
sarahman
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-11-13
收藏
得分:0 
回复 2 楼 hhwz
不太理解,可以解释一下吗?

人生也许就像编程。写的过程很难,写完了便很有满足感。
2014-11-14 11:15
快速回复:关于使用 FileInputStream 的问题, 如何实现打开文件一次之后所有 me ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.067790 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved