Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-29-2008, 01:10 AM
Member
 
Join Date: Jan 2008
Posts: 3
Oasis13 is on a distinguished road
Accessing Data from a .txt file
Hi guys,
I hope you are able to help me

I'm very new to Java, and have so far just spent a lot of hours playing around and using google when i get stuck with something.

Anyway, in a little program I'm currently doing I'd like to set an int variable to a value that is stored in a .txt file.

For example, in pseudo code it would work something like this:
Code:
int a; a = the value of the int stored in this text file(hello.txt);
The main purpose of doing this is so that every time the program is run, int a takes its value from the text file. Whilst running the program I might like to change the value of a, in which case the txt file will need changing so that the changes are reflected upon the next run of the programm. Essentially its like using a GET SQL command with a MySQL database, but far simpler!!! (Hopefully )

Any help with this would be greatly appreciated!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-29-2008, 08:09 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
You can use the FileReader class for this.

Code:
FileReader reader = new FileReader("hello.txt"); Scanner console = new Scanner(reader); //Use a scanner to read the info int x = console.nextInt();
See basically, Scanner has a constructor for a FileReader, so we can pass the FileReader object to the scanner, thus giving us the ability to use the Scanner methods on it. In this case, we will use the nextInt() method to obtain the int value in the .txt file.

If you have any more questions, let me know, i enjoy file reading and writing.
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-30-2008, 01:42 AM
Member
 
Join Date: Jan 2008
Posts: 3
Oasis13 is on a distinguished road
Thanks, that worked like a charm!!!

Just one question though - how can i edit the contents of the text file from within the programm e.g. change the value of the int x.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-30-2008, 02:23 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
Well, with this, we will need to use FileWriter. We can overwrite the original file with the new text we want.

Code:
FileWriter out = new FileWriter("hello.txt"); out.write(//new value of x); out.close();
By the way, both FileWriter and Reader constructors throw IOException, so you have to handle it. (try, catch)
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-31-2008, 09:42 PM
Member
 
Join Date: Jan 2008
Posts: 3
Oasis13 is on a distinguished road
Thanks again, I've got it all working now
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 02-01-2008, 01:16 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
K awesome man!
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Axis Client accessing data on .Net webservice cfacile666 Web Frameworks 2 06-24-2008 12:09 PM
Accessing Resources in a JAR File JavaForums Java Blogs 0 04-15-2008 06:40 PM
Accessing a file randomly Java Tip Java Tips 0 11-10-2007 09:15 PM
Accessing client file system revathi17 New To Java 1 10-17-2007 11:17 AM
Packaging and accessing data files todd Advanced Java 1 08-01-2007 01:27 AM


All times are GMT +3. The time now is 09:27 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org