JOptionPane writing to file?

Seems like i’ve been getting stumped everywhere lately :frowning: and most things i can find throughout my book but some i cannot like so:

I’m trying to have a JOptionPane showinputdialogue show and when i enter a string like “hello” it’ll write to hello.txt and find a certain line and write to that line hello.txt and i can also type “awesome” and it’ll write to that one on the same line that i had for hello.txt but no success so far. writing to files/strings are my biggest problem i can not overcome yet and they stump me everytime.

So in psuedocode i’m trying to make it do this:

If button clicked
open JOptionPane.showinputpane
type file name without extension
locate file with extension .txt
change "hi=1" in file to "hi=2"
close JOptionPane

If someone can kinda explain what to do or a link/tutorial/book something that’ll solve me fear of strings and file/disk writing, it’d be greatly appreciated.

Do you know how JOptionPane works?
Also, you can’t change a specific line in a file; your options are either overwrite or append. So you would read in the entire file, change the line you need and rewrite the entire file:


//get file name from user
String fileName = JOptionPane.showInputDialog("Type file name:");

ArrayList<String> lines = new ArrayList<String>();

//read lines from file
Scanner scanner = new Scanner(new File(fileName+".txt"));
while(scanner.hasNextLine())
    lines.add(scanner.nextLine();
scanner.close();

//edit specific line
lines.set(5,lines.get(5).substring(0,3)+"2");

//rewrite lines to file
PrintWriter writer = new PrintWriter(new File(fileName+".txt"));
for(String s : lines)
    writer.println(s);
writer.flush();
writer.close();

Also, you know about these tutorials, yes?
http://download.oracle.com/javase/tutorial/essential/io/index.html