Text file I/O

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amonita
    New Member
    • Nov 2007
    • 2

    Text file I/O

    Hi, I'm pretty new to Java, and I'm really struggling with getting my code to work. The files compile with no errors, but they are supposed to input/output to text files, and I can't get it to output anything. There are two files in the program, the point of it being to count the frequency of words in a text file, and I just wondered if you could tell me where I need to insert the file names to get the code to work correctly. Thank you

    FreqListCreator .java

    Code:
    import java.io.*;
    
    public class FreqListCreator {
    
    public static void
    main(String args[])
    throws IOException {
    	if(args.length != 1) {
    		System.err.println("usage: FreqListCreator filename");
    	} else {
    		FileTokeniser ft = new FileTokeniser(args[0]);
    		FreqList flist = new FreqList();
    		while(ft.hasMoreTokens()) {
    			flist.add(ft.getNextToken());
    		}
    		ft.close();
    		PrintWriter pw = new PrintWriter(new FileWriter(args[0]+".frq"));
    		flist.save(pw);
    		pw.close();
    	}
    }
    An excerpt from FreqList:

    Code:
    public void
    save(PrintWriter pw) {
    	Iterator it = storage.keySet().iterator();
    	while(it.hasNext()) {
    		String word = (String)it.next();
    		int freq = getFreq(word);
    		pw.println(word+" "+freq);
    	}
    }
    
    
    public static void
    main (String args[])
    throws IOException {
        FreqList flist = new FreqList();
        BufferedReader br= new BufferedReader(new FileReader(args[0]));
        flist.load(br);
        br.close();
        Iterator it = flist.iterator();
        while(it.hasNext());
              String word = (String) it.next();
              int freq = flist.getFreq(word);
              System.out.println(word+": "+freq);
    }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    What command did you use to run the program? It's expectiong a filename as argument to the main method.

    Comment

    • samido
      New Member
      • Oct 2007
      • 52

      #3
      Originally posted by amonita
      Hi, I'm pretty new to Java, and I'm really struggling with getting my code to work. The files compile with no errors, but they are supposed to input/output to text files, and I can't get it to output anything. There are two files in the program, the point of it being to count the frequency of words in a text file, and I just wondered if you could tell me where I need to insert the file names to get the code to work correctly. Thank you

      FreqListCreator .java

      Code:
      import java.io.*;
      
      public class FreqListCreator {
      
      public static void
      main(String args[])
      throws IOException {
      	if(args.length != 1) {
      		System.err.println("usage: FreqListCreator filename");
      	} else {
      		FileTokeniser ft = new FileTokeniser(args[0]);
      		FreqList flist = new FreqList();
      		while(ft.hasMoreTokens()) {
      			flist.add(ft.getNextToken());
      		}
      		ft.close();
      		PrintWriter pw = new PrintWriter(new FileWriter(args[0]+".frq"));
      		flist.save(pw);
      		pw.close();
      	}
      }
      An excerpt from FreqList:

      Code:
      public void
      save(PrintWriter pw) {
      	Iterator it = storage.keySet().iterator();
      	while(it.hasNext()) {
      		String word = (String)it.next();
      		int freq = getFreq(word);
      		pw.println(word+" "+freq);
      	}
      }
      
      
      public static void
      main (String args[])
      throws IOException {
          FreqList flist = new FreqList();
          BufferedReader br= new BufferedReader(new FileReader(args[0]));
          flist.load(br);
          br.close();
          Iterator it = flist.iterator();
          while(it.hasNext());
                String word = (String) it.next();
                int freq = flist.getFreq(word);
                System.out.println(word+": "+freq);
      }
      }


      Sorry man!! but what are this object suppose to do: FileTokeniser ft = new FileTokeniser(a rgs[0]);
      FreqList ...

      Comment

      • amonita
        New Member
        • Nov 2007
        • 2

        #4
        Originally posted by r035198x
        What command did you use to run the program? It's expectiong a filename as argument to the main method.
        I built and ran it in NetBeans, I'm not sure where i would put a command line argument?

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by amonita
          I built and ran it in NetBeans, I'm not sure where i would put a command line argument?
          After buiding it in Netbeans, it tells you the commands to use for running it from the command prompt.
          When you wrote the code you expected that the program would be given a file name as an argument so you have to pass that to main.

          Comment

          Working...