-2

I have two string variables containing some user input.

        String name="neil";
        String mobile="5555";

now I want to display neil an 5555 in a text file. Please help me to display this in a text file, I know how to display content of a file which is already exist, but donot know how to work with this case, please help me...

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
neil
  • 21
  • 3
  • 2
    Create a file and write to it ? – Suresh Atta Mar 02 '16 at 12:11
  • Yep, that's what I want. Actually want to write these 2 values to a file – neil Mar 02 '16 at 12:18
  • First what you do in current servlet is bad. You declare the response to be a text/html one, but only send plain text. It is not bad to send plain text but you should set ContentType accordingly. Next if your question is *how to write two values to a text file* please ask **that** question and remove everything unrelated to the actual question. – Serge Ballesta Mar 02 '16 at 12:44

1 Answers1

0
PrintWriter out = response.getWriter();

This should be changed to use "java.io.File" class. Has been explained in this thread: How to use PrintWriter and File classes in Java?

File file = new File("C:/Users/Me/Desktop/directory/file.txt");
file.getParentFile().mkdirs();

PrintWriter out = new PrintWriter(file);

Something like this.

ps: check out "try-with-resources"

Community
  • 1
  • 1
Andrii Plotnikov
  • 3,022
  • 4
  • 17
  • 37