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

The FileWriter has an optional second boolean argument. If you pass it true, it will operate in append mode.

This article has been dead for over six months. Start a new discussion instead.