I have a program that reads from a database, and then displays the results in a TextArea. Then when the user hists upload the file will be saved to a .txt file, and then uploaded to the database. But currently I am getting the original which looks like this in the TextArea

1, Header,i
2, Item 1,n
3, Item 2,n
4, Item 3,n

to look like this in the file

1, Header,i2, Item 1,n3, Item 2,n4, Item 3,n

How can I resolve this issue_

Thanks for your help.

Here is my code that writes

try {
				FileWriter w = new FileWriter ("upload.txt"); // save file to upload.txt
				BufferedWriter out = new BufferedWriter (w);
				out.write(ta.getText()); // ta is my textArea
				out.close();
				JOptionPane.showMessageDialog(null, "Kész!"); // tells the user that the save / upload has been completed.
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

1) one most confortable way is put formatted output from the File to the JTable with removed H & V grid

2) by using Html is possible tabular output to the JTextPane, JTextArea not designated use Htlm correctly

3) create three JTextArea (added to JPanel)

EDIT
4) here is your answer
EDIT

5) maybe nicest will be JList with Html Renderer, but required deepest knowledge about Html & css (Html <= 3.2)

I am writing into the JTextArea, and then I save to a file. I believe you think I am reading from a file, and writing to the JTextArea. It needs to be a TextArea, because I have an infinite amount of lines I can have.

Thanks for your help.

The first thing you get is a blank template, the the picture is when you read from the database, then you edit it and click the upload button, and then it saves, and then it deletes what was in the database, and then uploads the txt file. It needs to save in the format shown in the picture, otherwise it won't work.

How can I resolve this issue_

Can you explain the issue? What changes do you want made to the data when it is copied from the text area to the file?

I would like the file to save in the same format as in the Text Area. For example if I have text like this

This is some text, and
this is in a new line

I would like it to save like that not like this

This is some text, and this is in a new line

like it currently does.

can help you to calculate number of row, but JTextArea must before know number of Column see JTextArea(int rows, int columns)

myTextArea.setLineWrap(true);
myTextArea.setWrapStyleWord(true);


1) that job for JTable or JList,

2) in very limited *** for JTextPane

3) JTextArea has own Model or Document can do that

4) hold data from Resultset and compare that with data from JTextArea

5) last property and really stupid way is searching for LineSeparator in JTextArea,

Add a newline character: ('\n') to the end of the lines when writing them to the file.

I didn't set a certain number of rows and columns. I have it blank, so that it fulls up the whole frame, which is also resizable. Adding your code didn't help either.

The way I have it the text isn't even close to reaching the end. See the screenshot in my previous post.

How could I tell when the new line appears when I am writing to a file, and getting the information from the TextArea using the getText() method?

Also I add the \n when I am reading from the database, but I don't know where to add it when I am writing to the file.

Did you try this:
Add a newline character: ('\n') where you want the end of the lines to be when writing them to the file.

I don't know where to put it because When I get the text I use the getText() method, and I cannot put it in the actual text, because it needs to be as simple as possible, plus it may ruin the ability of the database to understand the text. is there a way to read the TextArea line by line?

What is causing the text to go to the next line in the text area?
Can you detect what is on a visual line in the text area so you can add a newline character at that point when you write the line to a file?

@NormR1 not good idea play with LineSeparator, and split Document by using ..., maybe I'm wrong somehow, somewhere, but I had no good experiences

Here is the code that sets the text when I read from the database

public void actionPerformed(ActionEvent e){
		// TODO Auto-generated method stub
		System.out.println (e.getActionCommand());
		
			currentTable = e.getActionCommand();
			database = "";
			try {
				stat = con.prepareStatement("SELECT * FROM " + e.getActionCommand() + " ORDER BY ID ASC");
				result = stat.executeQuery();
				
				while (result.next()){
					database += result.getString(1) + "," + result.getString(2) + "," + result.getString(3);
					database += "\n";
				}
				ta.setText(database);
			}catch(Exception ex){}
		
	}

and here is what I do when I save the file

public void menuSelected(MenuEvent e) {
		// TODO Auto-generated method stub
		JMenu source = (JMenu)e.getSource();
		if (source.isSelected()){
			try {
				FileWriter w = new FileWriter ("upload.txt");
				BufferedWriter out = new BufferedWriter (w);
				out.write(ta.getText());
				out.close();
				System.out.println ("Deleting");
				stat = con.prepareStatement("DELETE FROM " + currentTable);
				stat.executeUpdate();
				System.out.println ("Attempting to load data");
				stat = con.prepareStatement("LOAD DATA INFILE 'upload.txt' REPLACE INTO TABLE " + currentTable + " FIELDS TERMINATED BY ','");
				stat.executeUpdate();
				JOptionPane.showMessageDialog(null, "Kész!");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (SQLException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
		}
	}

Thanks for the help.

@mKorbel If the OP wanted the lines from the textarea when in the text file to be on separate lines, How else can you do that without there being line end characters?

out.write(ta.getText());

If the returned String has lineends, they will be written to the file. If the String does not have lineends, and you want them, then you will have to add them.

What program are you viewing the text file in? On Windows, notepad and wordpad operate differently. I think wordpad is the better of the two.

but I need to end them to the end of each row, but how can I do that when I use the getText() method. It doesn't read line by line, so I don't know when to add it.
Like I said If I add it to the actual textArea it would be as user friendly, and I am making this for people who cannot do all of this. Is there a way I can read the textArea line by line, so that I can add the '\n' to the part where I write to the file? I view it in notepad.

Look at the API doc for the text area and see what methods are avaiable.

What program are you viewing the text file in? On Windows, notepad and wordpad operate differently. I think wordpad is the better of the two.

I have another error. I will see if I can fix that. maybe the upload works correctly.

Do you have any idea why I get an access denier error in a MySQL database, when I gave the user I am connecting with all privileges, and I have added my IP to the remoteSQL option?

I mailed my webhost, and now I am waiting for a reply. I may post back in a day or so, or I will mark it solved.

Thanks for your help.

JTextArea is very terrible JComponent, pretty ignore implemented Columns and Rows, since is there possible split Document by searching for NewLine, but that doesn't talking about ViewPort, this line could be wrapped in one Column, two..., same as in two lines

if you declare with initial numbers for Column and Rows

myTextArea.setLineWrap(true);
myTextArea.setWrapStyleWord(true);

then you can calculate number or Rows occupated in the ViewPort,

I would like the file to save in the same format as in the Text Area. For example if I have text like this

This is some text, and
this is in a new line

I would like it to save like that not like this

This is some text, and this is in a new line

like it currently does.
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.