.txt File Handling in Java

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2009
Posts: 1
Reputation: LooN_iE is an unknown quantity at this point 
Solved Threads: 0
LooN_iE LooN_iE is offline Offline
Newbie Poster

.txt File Handling in Java

 
0
  #1
May 5th, 2009
  1. package newuser;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5.  
  6.  
  7. public class Main implements ServerConstants {
  8.  
  9. public static void main(String[] args) {
  10. BufferedReader lnr = null;
  11. String line = null;
  12.  
  13. int N=1000;
  14. int totalRecords = 0;
  15.  
  16. String [][] array = new String[N][];
  17.  
  18. try {
  19. lnr = new BufferedReader (new FileReader("LoginData.txt"));
  20.  
  21. for (line = lnr.readLine(); line!=null; line = lnr.readLine()) {
  22.  
  23. array[totalRecords] = line.split(" , ");
  24. totalRecords++;
  25. }
  26.  
  27. lnr.close();
  28. } catch (Exception e) {
  29. System.out.println("An error has occured: "+e.getMessage());
  30. //e.printStackTrace();
  31. }
  32. for (int i=0;i<totalRecords;i++) {
  33. for (int j=0;j<array[i].length;j++) {
  34. System.out.print(array[i][j]+" ");
  35. }
  36. System.out.println();
  37. }
  38. }
  39. }

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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,620
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: .txt File Handling in Java

 
0
  #2
May 5th, 2009
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.
Last edited by BestJewSinceJC; May 5th, 2009 at 10:12 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC