| | |
.txt File Handling in Java
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2009
Posts: 1
Reputation:
Solved Threads: 0
Java Syntax (Toggle Plain Text)
package newuser; import java.io.BufferedReader; import java.io.FileReader; public class Main implements ServerConstants { public static void main(String[] args) { BufferedReader lnr = null; String line = null; int N=1000; int totalRecords = 0; String [][] array = new String[N][]; try { lnr = new BufferedReader (new FileReader("LoginData.txt")); for (line = lnr.readLine(); line!=null; line = lnr.readLine()) { array[totalRecords] = line.split(" , "); totalRecords++; } lnr.close(); } catch (Exception e) { System.out.println("An error has occured: "+e.getMessage()); //e.printStackTrace(); } for (int i=0;i<totalRecords;i++) { for (int j=0;j<array[i].length;j++) { System.out.print(array[i][j]+" "); } System.out.println(); } } }
Ok so I have this code. The code reads a text file, stores the information into an array then displays the contents on the screen when I run the code. So basically what I have is a .txt file with a list of users e.g:
username, password
matt, 123
What I want the code to do is allow me to enter new users into that text file. I have browsed through the i-net and it seems that the .txt file cannot be just edited. The information has to be stored into an array, and then the extra information I wish to add be added to that array, then the array printed into a new .txt file. How exactly can I modify the code to do this? Any help is appreciated. Thanks
•
•
Join Date: Sep 2008
Posts: 1,620
Reputation:
Solved Threads: 205
Putting text at the end of an existing file
^ I'm pretty sure using FileWriter with the constructor that has "append" as one of the arguments will allow you to write to a file without deleting its contents first. To use FileWriter:
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
The example of PrintWriter can be found here.
I'm going to assume you can take it from here, but if you need any more help, feel free to ask.
^ I'm pretty sure using FileWriter with the constructor that has "append" as one of the arguments will allow you to write to a file without deleting its contents first. To use FileWriter:
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
The example of PrintWriter can be found here.
I'm going to assume you can take it from here, but if you need any more help, feel free to ask.
Last edited by BestJewSinceJC; May 5th, 2009 at 10:12 pm.
![]() |
Similar Threads
- EXCEL CSV FILES Handling (Java)
- parent/child (Java)
- handling multiple files (Java)
- Implementing Random Images (Java)
- parse a .txt file (Java)
- Reading from a file to fill an array (Java)
- "cannot resolve symbol"problem (Java)
Other Threads in the Java Forum
- Previous Thread: What's the event when clicking on a tab, in a multi-tab frame?
- Next Thread: NullPointerException with Channels
| Thread Tools | Search this Thread |
6 actuate android api applet application array arrays automation balls binary bluetooth bold business c++ chat class classes client code codesnippet collections component coordinates database defaultmethod doctype dragging draw ebook eclipse educational error event exception file fractal froglogic game givemetehcodez graphics gui hql html ide image ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaprojects jni jpanel jtextarea julia linux list loop looping map method methods mobile mysql netbeans newbie nextline numbers oracle parameter php print problem program programming project recursion recursive scanner screen server set size sms sort sql string sun swing swt threads time tree user websites windows






