Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Please Help  RSS feed

 
Imam Bux Mallah
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is code so far I have just worked (please don't laugh at this)

import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;


class Database
{
public static void main(String[] args) throws IOException
{
FileWriter fwID = new FileWriter("Employees.txt");
String ID = JOptionPane.showInputDialog("Enter your respective ID");
fwID.write(ID);
fwID.close();

FileWriter fwNAME = new FileWriter("Employees.txt",true);
String name = JOptionPane.showInputDialog("Enter your respective name");
fwNAME.write("\n"+name);
fwNAME.close();


}
}

when I run this program
the output of both the inputs is on the same line in Exployees.txt

Q. how can I return my second string in new line in txt document?

Please help needed urgently...THANKS IN ADVANCE
 
Mohamed Sanaulla
Bartender
Posts: 3179
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Javaranch, Just a few tips- Please UseAMeaningfulSubjectLine and UseCodeTags for the source code. I got this link from a google search: http://www.coderanch.com/t/279182/Streams/java/Starting-new-line-FileWriter-class I would suggest you to have a look at PrintWriter class as well. That gives a better API.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

As explained in the link Mohamed provided, this is a Windows quirk. For Windows, you probably need to use a combination of return and newline characters, "\r\n", instead of simply "\n".

(Also see Wikipedia: Newline - Common Problems.)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!