Thursday, January 19, 2012

File IO ....in Java (!)

  GNU nano 2.2.6                    File: fileIO.java                                            

//all io stuff we need
import java.io.*;

public class fileIO{
public static void main(String[] args){

    //What file will we read?
//    File myFile = new File("./textFile.txt");

    //create a filereader
    FileReader fr = new FileReader("/nfs/student/s/smgonzal/textFile.txt");
   
   //create buffered reader
    BufferedReader br = new BufferedReader(fr);

   int numWords = 0;

   //make string tokenizer
   StringTokenizer st;

//pseudo code...
//      while (not at end of file)
//      {
//        buffered reader gets next line
//        give it to tokenizer
//
//        //what if .,! etc are tokens though... this would be inacccurate
//        numWords = numWords + st.countTokens();
//      }

    System.out.println("number of Words: " + numWords);
}
};


Resources Used:

http://docs.oracle.com/javase/1.4.2/docs/api/java/io/BufferedReader.html
http://docs.oracle.com/javase/1.4.2/docs/api/java/io/File.html#File(java.lang.String)
http://docs.oracle.com/javase/1.4.2/docs/api/java/io/FileReader.html
http://www.abbeyworkshop.com/howto/java/readFile/index.html
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html

looked at (not so much used):
http://www.roseindia.net/java/beginners/java-word-count.shtml
http://stackoverflow.com/questions/5748656/unreported-exception-java-io-filenotfoundexception-must-be-caught-or-declared-t

No comments:

Post a Comment