Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

File Input Stream

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eric Tan
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok. sure. thanks alot. i will look into it
 
Eric Tan
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringTokenizer is indeed suitable for simple parsing jobs like what you have in mind with your program.
 
Eric Tan
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot for the info. I will look on it and give it a thought. Thanks
 
Eric Tan
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not use a for loop. See how that works out.

Moving to Beginners Forum.
 
Eric Tan
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. thanks alot. i understand what you mean. i will look into it.
 
Eric Tan
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Eric Tan
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read the file line by line and then use string split with delimeter '|' and then write those to DB.
 
Eric Tan
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the idea. Will look into it
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Thanks alot. I will look on it now
 
Climb the rope! CLIMB THE ROPE! You too tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic