Simple Coding Question (Java) about .txt files

C++, C#, Java, PHP, ect...
Post Reply
Psycho-Drive
Posts: 15
Joined: Wed Mar 17, 2010 9:47 pm

Simple Coding Question (Java) about .txt files

Post by Psycho-Drive »

Hey guys...
I started playing Minecraft a week or two ago, and right now I'm trying to set up a banking company.

To aid in the running of the bank, I'm trying to design a java program that I will be able to input account data into. However, it's been nearly a year since my last programming class, so I don't quite remember how to do everything.

I need the program to input new account names into a .txt file and then be able to search for the account name and update it when needed.
How do I do this? Also, should I put all the values into one file, or would the programming be smoother/easier if I had the program make a separate file for each new account (can java create new files, or can it only modify ones already created?)?
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Simple Coding Question (Java) about .txt files

Post by Jackolantern »

I thought I had a source file that did something like this, but I must have deleted it. Here is a tutorial about writing to text files in Java. It encompasses a bit too much for me to explain in here, since it involves streams. But you are free to create however many text files you want in a Java program.
The indelible lord of tl;dr
Psycho-Drive
Posts: 15
Joined: Wed Mar 17, 2010 9:47 pm

Re: Simple Coding Question (Java) about .txt files

Post by Psycho-Drive »

Sorry for the delay, I've been a bit busy with finals...


I notice that the file name appears to be predefined. Is there a way I can make it so that the file name the program searches for is based off a string I input, or do these have to be constant?
(So that, if I type in, say, "bob" it will search for "bob.txt")
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Simple Coding Question (Java) about .txt files

Post by Jackolantern »

They just seem predefined because they are being entered as command-line arguments. I am sure they are doing it that way for simplicity's sake, since live input in a Java console application requires the use of streams, which would muddy-up the example code and possibly steal attention away from where it should be. You can see the string variable "fileName" in the example. You can put whatever you want into this variable, from any source or method you can think of (streams, command-line arguments, Applet text box, POST web form info, swing text box, etc.).
The indelible lord of tl;dr
Post Reply

Return to “Coding”