1

this is a work in progress, but I was wondering about how to access a specific file. My main method is supposed to construct a new FileInputStream with a new file as its parameter. Then, I am supposed to call getCounts method that accepts a FileInputStream object to return some kind of map. Inside the getCounts method, I am having trouble finishing this as I have to be able to access the file inside it, and FileInputStream doesn't seem to have a accessor for it. In other words, how do I access this file used to construct the FileInputStream object that goes into the getCounts Method as a parameter, inside the getCounts method? Ultimately, I am supposed to use the keys/values of the map to get the most reoccurring character of the text file. Thanks.

Here is my code:

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;
 }
}
Jack L.
  • 405
  • 2
  • 13
  • 31
  • Why do you need access to the file object? The input stream does give you enough to do the counting. – BetaRide Jul 15 '14 at 06:13

2 Answers2

2

If you are going to use FileInputStream, then you will need to loop, doing multiple reads

byte[] fileContent = new byte [1024];

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

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

}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • This might sound like a dumb question, but why 1024 elems for byte[] fileContent? – Jack L. Jul 15 '14 at 06:22
  • it is an arbitrary number, could be 2048, 1234 or 6734, but the inputstream will only read that amount of data before returning. – Scary Wombat Jul 15 '14 at 06:24
  • I plan to use this to read characters of large novels so should I have something like: byte[] filecontent = new byte[Integer.MAX_VALUE]; just to be safe? – Jack L. Jul 15 '14 at 06:26
  • see http://stackoverflow.com/questions/8748960/how-do-you-decide-what-byte-size-to-use-for-inputstream-read – Scary Wombat Jul 15 '14 at 06:27
1

Ideally I would pass the the length as a parameter to getCounts() but since you're not allowed to do it, you can save the file-length into a class-static parameter:

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;
    }
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129