Results 1 to 1 of 1
Thread: Help opening and reading a file
- 09-21-2010, 07:32 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 16
- Rep Power
- 0
Help opening and reading a file
Hello, I have an assignment where we are to open a file, "tokenize" it, then print each token into an output file. This is easy with the string tokenizer class, but we cannot use it. We have to come up with a way around it. This is what I have so far, I want it to read characters, keep adding them to an empty string, when it reads a whitespace, get out of loop, save that string into an array of strings, and continue again....
There might be an easier way of doing this? Reading chars from a .txt file, seeing if they are a letter or digit?Java Code:String filename = "input.txt"; String[] tokens = new String[20]; try{ // Open the file that is the first // command line parameter StringBuffer buffer = new StringBuffer(); FileInputStream fstream = new FileInputStream(filename); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); int ch; int i=0; while ((ch = in.read()) > -1) { if(Character.isLetter(ch) || Character.isDigit(ch)) { buffer.append((char)ch); } tokens[i] = buffer.toString(); i++; } //print array for (i=0; i < tokens.length; i++) System.out.println (tokens[i]); //Close the input stream in.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage());
Similar Threads
-
Opening mp3 file ( not reading )
By Char in forum New To JavaReplies: 15Last Post: 08-29-2010, 07:09 AM -
Opening .html file
By Prajin in forum New To JavaReplies: 7Last Post: 08-06-2010, 07:20 AM -
Error While Opening A PDF file
By Cluster Storm in forum AWT / SwingReplies: 6Last Post: 06-07-2010, 06:03 PM -
Opening applet with .html file
By Atriamax in forum New To JavaReplies: 12Last Post: 09-13-2009, 06:26 AM -
Problem in opening a file
By Raghav kv in forum New To JavaReplies: 1Last Post: 08-11-2007, 01:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks