-1

I've been looking over my notes and been looking online but I have came up empty handed. I am trying to write code in which the user specifies the file they want to output variables to. I know I will have to use things like PrintWriter and File but can someone show me the step in the right direction.

  • hmmm How do you want to take "file" from user ? through console ? GUI ? anyway in console Scanner should suffice – niceman Apr 20 '16 at 19:51
  • I have a question asking which file they would like to write to. The problem is I don't know how to set it so they can type the file name in. Would I just treat the file name the user inputs as a variable? – netbeansuser Apr 20 '16 at 19:59
  • yes netbeansuser, filename can be a variable but it will be string which you pass to FileOutputStream's constructor `FileOutputStream file=new FileOutputStream(filenamevariablefromuser)` – niceman Apr 20 '16 at 20:04

1 Answers1

0

See here for writing to file: How do I create a file and write to it in Java?

As for the user specifying the file, instead of hard-coding the name to be "the-file-name.txt", or whatever, just ask the user where to save the file and store that in a variable, then use that variable as the file name.

Community
  • 1
  • 1
Mogzol
  • 1,405
  • 1
  • 12
  • 18