File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes File Input Stream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "File Input Stream" Watch "File Input Stream" New topic
Author

File Input Stream

Eric Tan
Ranch Hand

Joined: Oct 11, 2006
Posts: 72
i have a text file that i want to read to be my input into my database in db4o. i was looking through the websites. like http://java.sun.com/javase/6/docs/api/java/io/FileInputStream.html
and
http://www.java2s.com/Code/JavaAPI/java.io/FileInputStreamavailable.htm


there were different types too. like FileReader, FileInputStream. can anyone explain to me the differences between this two?
below is an example on how inside my text file is going to look like


Jack | China | 23
Helen | Holland | 34
Jenny | US | 34
Micheal | Brazil | 23


different attributes is separated by "|". i need to capture all this from a text file and store it in my db4o database. maybe someone could guide me on this? i am very new in java so forgive me if i am wrong. Thanks alot
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The standard form for reading a file is something like:

Look at the BufferedReader and FileReader APIs and see if you can put them together. If you haven't seen an expression like the one in the "while" loop it can be a bit confusing. It reads a line, assigns the results into a string variable called "line", and checks the variable for null all in one line. Null means the reader has reached end of file, so that ends the loop.

See if you can make some code work with something really simple inside the loop, like just print each line as you read it. Then we'll figure out how to break each line into separate fields and get it into your database.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

The important thing to understand is that for text I/O you should use Readers and Writers in Java, for example FileReader and FileWriter.

For binary I/O, you use streams (for example, FileInputStream and FileOutputStream).

As Stan shows you can use BufferedReader to easily read a text file line by line.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Eric Tan
Ranch Hand

Joined: Oct 11, 2006
Posts: 72
ok. sure. thanks alot. i will look into it
Eric Tan
Ranch Hand

Joined: Oct 11, 2006
Posts: 72
Thanks alot for the information. I would like to ask something because i came across on StringTokenizer where it is able to break my whole bunch of String and break them into smaller parts by differentiate it with a symbol that is declared inside the code.

i see that this characteristics of this method match the problem that i am having whereby i need to break my data in the text file by using the "|"

which method among all do you think is best suitable for my case??
thanks a lot everything for your help!!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

StringTokenizer is indeed suitable for simple parsing jobs like what you have in mind with your program.
Eric Tan
Ranch Hand

Joined: Oct 11, 2006
Posts: 72
thanks alot for yr suggestion. i will look into it. i just want to ask something. in order for me to store the data from the text file, after i have the String Tokenizer to retrieve all the data, i was thinking that i should have a nested loop and array to temporarily store it before i actually save it inside my object. is this correct? thanks.

thanks alot for help.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Do you mean a loop over the tokens from StringTokenizer? If your record is in a fixed format you don't need the loop, just pull tokens in straight sequential code:

If you have to accept the occasional partial record - some of the ending fields are missing - you might check hasNext() before getting the next. If you don't check you'll get an exception on a short record, which may be exactly the right thing.

After you get this working and thoroughly understand what you have, look at a couple cool alternatives. String.split (new in 1.4) will split the string into an array. Scanner (new in 5.0) can read a file one token at a time.

BTW: I often put the delimiter first in delimited strings. That way the parser or tokenizer can peek at the first character to find out what the delimiter is rather than making everybody pick a delimiter ahead of time. You can even nest a structure with one delimiter inside a structure with another. Good clean fun.

Let us know how this all works out!
[ November 03, 2006: Message edited by: Stan James ]
Eric Tan
Ranch Hand

Joined: Oct 11, 2006
Posts: 72
Thanks alot for the info. I will look on it and give it a thought. Thanks
Eric Tan
Ranch Hand

Joined: Oct 11, 2006
Posts: 72
Actually i was thinking, the main purpose that i want to put it in loop is because i want to store the data that is being read into the database. If i want to do so, besides displaying, should i have include the loop or it's not necessary? Thanks alot
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
Do not use a for loop. See how that works out.

Moving to Beginners Forum.
Eric Tan
Ranch Hand

Joined: Oct 11, 2006
Posts: 72
Ok. I understand what you mean. I will work it out from the beginning stage and see. Thanks alot for your help
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Just to be annoyingly clear, there is a loop for each line of input. Once you have a line you don't need another loop to extract fields and do a database insert.

Here's something I have seen more than once and it makes me scream every time

Your question made me think of this and YOU DO NOT NEED THIS.
Eric Tan
Ranch Hand

Joined: Oct 11, 2006
Posts: 72
Ok. thanks alot. i understand what you mean. i will look into it.
Eric Tan
Ranch Hand

Joined: Oct 11, 2006
Posts: 72
If i want to insert my data into the database, must i use an array to do that? because i have to display out the data stored in eclipse and insert into the database at the same time. or i do not have to create array in order to perform this method? thanks alot!
Eric Tan
Ranch Hand

Joined: Oct 11, 2006
Posts: 72
i have tried this codes but it still got some errors on it. may anyone help me on this? the error message are illegal start of type and <identifier> expected .




Thanks alot
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Originally posted by Eric Tan:
i have tried this codes but it still got some errors on it. may anyone help me on this? the error message are illegal start of type and <identifier> expected .

Thanks alot


You can't just place code anywhere in a class. I am assuming that you want to declare a main() method to place those while loops in.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Eric Tan
Ranch Hand

Joined: Oct 11, 2006
Posts: 72
I got your point. I already modified on the codings i have done. There is still abit hiccups. Could you have a look on it please? Thanks

The error was "cannot find symbol method readLine()".





Thanks alot for your help
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
You can read the file line by line and then use string split with delimeter '|' and then write those to DB.


Rahul Bhattacharjee
LinkedIn - Blog
Eric Tan
Ranch Hand

Joined: Oct 11, 2006
Posts: 72
Thanks for the idea. Will look into it
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
To the error ... the variable that holds a reader object is called "br". The variable called "reader" holds a String. Use "br" to read a line.

The "while(tokenizer.hasMoreTokens())" loop is not necessary. The block of code inside the curly braces is correct without the loop. The only caveat is that it will throw an exception if you have a short line that doesn't have all the tokens. We might decide that's the right thing to do.

See if that helps.
Eric Tan
Ranch Hand

Joined: Oct 11, 2006
Posts: 72
Ok. Thanks alot. I will look on it now
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File Input Stream
 
Similar Threads
file upload in servlet
Automatic Generate Text File from mySQL
Problem using DataInputStream Class
Obtaining the contents of a binary file
plz delete