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

Recommended Answers

All 3 Replies

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.

HELLO CAN U HELP ME

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.