【问题标题】:How to access the parameter of a parameter?如何访问参数的参数?
【发布时间】:2014-09-05 05:13:48
【问题描述】:

这是一项正在进行的工作,但我想知道如何访问特定文件。 我的主要方法应该构造一个新的 FileInputStream ,并以一个新文件作为其参数。然后,我应该调用接受 FileInputStream 对象的 getCounts 方法来返回某种地图。在 getCounts 方法中,我无法完成此操作,因为我必须能够访问其中的文件,而 FileInputStream 似乎没有它的访问器。换句话说,如何在 getCounts 方法内部访问该文件,该文件用于构造作为参数进入 getCounts 方法的 FileInputStream 对象?最终,我应该使用地图的键/值来获取文本文件中最常出现的字符。谢谢。

这是我的代码:

import java.util.*;
import java.io.*;


public class test2 {
public static void main(String[] q) throws FileNotFoundException {

    // This is for the character mapping
    Scanner console = new Scanner(System.in);
    System.out.println("Count characters of which file? ");
    FileInputStream output = new FileInputStream(new File(console.nextLine()));
    Map<Character, Integer> results = getCounts(output); 
    // do stuff with this map later on...
}

 // character counting method (WIP)
 public static Map<Character, Integer> getCounts(FileInputStream input) {
     Map<Character, Integer> output = new TreeMap<Character, Integer>(); // treemap keeps keys in sorted order (chars alphabetized)
     // Problem here: need to access the file object that was instiantiated
     byte[] fileContent = new byte[(int) file.length()]; // puts all the bytes from file into byte[] to process
     input.read(fileContent); 
     String test = new String(fileContent);

     // missing stuff here; use map to map keys as characters and occurrences as values.

     return output;
 }
}

【问题讨论】:

  • 为什么需要访问文件对象?输入流确实足以让您进行计数。

标签: java map character fileinputstream


【解决方案1】:

如果你要使用 FileInputStream,那么你需要循环,做多次读取

byte[] fileContent = new byte [1024];

while ((input.read (fileContent) != -1) {

     // save fileContent somewhere
     // e.g.
     arrlist.add (new String (fileContent));

}

【讨论】:

  • 这听起来像是一个愚蠢的问题,但为什么 byte[] fileContent 需要 1024 个元素?
  • 它是一个任意数字,可以是 2048、1234 或 6734,但输入流只会在返回之前读取该数量的数据。
  • 我打算用它来阅读大型小说的人物,所以我应该有类似的东西:byte[] filecontent = new byte[Integer.MAX_VALUE];只是为了安全?
【解决方案2】:

理想情况下,我会将length 作为参数传递给getCounts(),但由于不允许这样做,因此可以将文件长度保存到类静态参数中:

private static long length;

public static void main(String[] q) throws IOException {

        // This is for the character mapping
        Scanner console = new Scanner(System.in);
        System.out.println("Count characters of which file? ");
        File file = new File(console.nextLine());
        FileInputStream output = new FileInputStream(file);
        length = file.length();
        Map<Character, Integer> results = getCounts(output);
        // do stuff with this map later on...
    }

    // character counting method (WIP)
    public static Map<Character, Integer> getCounts(FileInputStream input) throws IOException {
        Map<Character, Integer> output = new TreeMap<Character, Integer>(); // treemap keeps keys in sorted order (chars alphabetized)
        // Problem here: need to access the file object that was instantiated
        byte[] fileContent = new byte[(int) length]; // puts all the bytes from file into byte[] to process
        input.read(fileContent);
        String test = new String(fileContent);
        System.out.println("test = " + test);

        // missing stuff here; use map to map keys as characters and occurrences as values.

        return output;
    }

【讨论】:

  • 由于某种原因,赋值要求 getCounts 接受 FileInputStream 并返回一个映射。
  • 我相信他只是想要file 对象,以便他可以做file.length
  • 好的,谢谢。由于我只允许将 FileInputStream 作为参数,因此我必须像 Scary Wombat 建议的那样手动定义数组大小。
  • @ScaryWombat 3 个原因:1)它正在工作。 2)这是一个很好的解决方案。 3) 最少的代码更改。
  • 因为他正在阅读整本大型小说,我不确定在一个块中阅读那么多数据是一个好的解决方案,甚至可能
猜你喜欢
  • 2015-04-18
  • 2017-06-12
  • 2022-12-18
  • 1970-01-01
  • 2016-06-30
  • 1970-01-01
  • 2021-03-26
  • 1970-01-01
  • 2020-01-02
相关资源
最近更新 更多