6 Replies Latest reply: Feb 15, 2013 3:11 PM by 939520 RSS

    Newbie - Why doesn't this read or write me a file?

    990724
      I am a complete newbie and am going through the java tutorials. I use Netbeans as my IDE which makes things easier, but why doesn't this code do anything?
      import java.io.*;
      import java.util.Vector;
      
      
      @SuppressWarnings("empty-statement")
      public class ListDictionary {
      
          /**
           * @param args the command line arguments
           */
          private Vector<String> list;
          private static final int INITIAL_SIZE = 200000;
          private static final int INCREMENT = 10000;
          
          public void ListDictionary() {
          
              list = new Vector<>(INITIAL_SIZE,INCREMENT);
                             
              this.readFile("english-words-lowercase.txt");
              this.readFile("engish-upper.txt");
              this.TrimList();
              this.writeFile();
          }
          
          public void readFile(String fileName) {
              String line;
              try {
                  RandomAccessFile raf = new RandomAccessFile(fileName,"r");
                  while ((line = raf.readLine())!= null) {
                      list.add(line);
                  }
              }    
              catch (IOException e){
                  System.out.println("dictionary not found" + e);
              };
              int listSize = list.size();
              System.out.println(listSize + "words added");    
          }
          
          public void writeFile() {
              PrintWriter out = null;
              
              try {
                  out = new PrintWriter(new FileWriter("dictionary1.txt"));
                  
                  for (int i=0; i<list.size();i++){
                  out.println(list.get(i));
                  }
              }
              catch (IOException e) {
                  System.out.println(e.getMessage());
              }
              finally {
                  if (out != null) {
                      System.out.println("Seems to have worked!");
                  }
                  else {
                      System.out.println("Not this time");
                  }
              }
          }
          
          public void TrimList() {
              list.trimToSize();
          }    
          
          public static void main(String[] args) {
              ListDictionary listDictionary = new ListDictionary();
             
          }
      }
      Can anyone explain? i have spent hours trying different options.

      Edited by: EJP on 13/02/2013 15:19: added {noformat}
      {noformat} tags: please use them.