Hey guys.
So I want to learn how to do a simple I/O to and from a file.
I want the simplest way to do this.
My teacher sent me her program but I don't think I get what she is doing and it's giving me an error. So I would also like to clarify a term - Tokenizer.

Also, how do I read multiple lines? or find out how many lines are there in a file which are not blank?
And if the input is separated by white space, how do I get each one into a separate variable?
For example.
My input: ABC 13 DEF 14 15 LJK

So how would I get ABC in one string, 13 in an int and so on and so forth...

If this makes a difference (and most of the time, it may), I am using Java SDK 1.4.1 (since that is what our school is using -.- )

Any help is appreciated :) thanks

A tokenizer turns a raw input sequence into a sequence of "tokens", which your parser can then accept for further processing. It will often attempt to recognize keywords and return "tokens" of that type, and return unrecognized items as literals - so

if (a == b)
  {
    c = d;
  }

might be tokenized as IF, LPAREN, 'a', ISEQUALTO, 'b', RPAREN, LBRACE, 'c', ASSIGN, 'd', SEMI, RBRACE

(where those capitalized words are symbols recognized by the parser - they might be objects to your parser, or elements in an enumeration, or other sorts of representations)

That is, you're turning an input stream into known symbols which your parser knows how to handle. Presumably, your parser would know to expect a left parenthesis after an instance of "if", and so forth - that's not the tokenizer's job. The tokenizer's job is just to recognize the known symbols and hand them to the parser.

The advantage of this, of course, is that it clusters the similar work in one place, so you're not trying to recognize symbols and process them in the same logical place. This simplifies your work immensely.

Does that help?

Yes. Actually it does!
Now... if only someone could answer HOW to input and output to and from a file.

Thanks for the tokenizer explanation :)

Okay, file io is actually pretty easy to understand, although learning to do it right can be a bit of trial and error - you will make some mistakes, but you should be able to sort yourself out.
I'll start by suggesting that you read this here and see what you can make of it. It may be that it answers all of your questions, in which case, congratulations, you're done. If not, you'll be back with more specific questions, and that'll make everyone's life easier, especially yours.

input:

File ofile = fc.getSelectedFile();
        			file = new File(yourfilepath);
        			FileInputStream fis = null;
        		    BufferedInputStream bis = null;
        		    DataInputStream dis = null;
        		    textArea.setText(null);
        		    try {
        		      fis = new FileInputStream(file);
        		      bis = new BufferedInputStream(fis);
        		      dis = new DataInputStream(bis);

        		      while (dis.available() != 0) {
        		    	  textArea.setText(textArea.getText() + dis.readLine());
        		      }
        		      fis.close();
        		      bis.close();
        		      dis.close();

        		    } catch (Exception ex){}

output:

BufferedWriter out = new BufferedWriter(new FileWriter(file));
    	 out.write(text to write);
    	 out.close();
commented: Do you think the poster couldn't figure this out? Or did you just need to feel clever? +0

Brilliant.
Plus one for rote code. Minus ten for lost learning opportunity.

Brilliant.
Plus one for rote code. Minus ten for lost learning opportunity.

thanks...

lol I'm actually two sided on this one :P I have to write this competition on tuesday (Canadian Computing Competition) and there are two sets of questions that I can choose from.
Junior and Senior. I need to know how to I/O from files for senior :P So I could have taken my time (2 hours or so...) to properly learn it... or ask someone for the direct code to save time for practice :P

Anyhow, thank you to both of you :)
I'll mark this as solved.

lol I'm actually two sided on this one :P I have to write this competition on tuesday (Canadian Computing Competition) and there are two sets of questions that I can choose from.
Junior and Senior. I need to know how to I/O from files for senior :P So I could have taken my time (2 hours or so...) to properly learn it... or ask someone for the direct code to save time for practice :P

Anyhow, thank you to both of you :)
I'll mark this as solved.

No Problem. I had recently made a text editor in java so i just used the code from that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.