FileWriter related question

Hey,

in my program, user inputs data, which is written to a file. I want to ask my user for the filename and save it as such. I can do this alright, but my problem is how to save the data as .txt file. Currently my code saves without format.

I would also like to add feature in which user can later on edit particular files.Therefore I need to print contents of a folder to screen. Where should I start looking for this?

This is my current method code, it's not a revolutionary one so make sure you understand it (in case you would borrow it) ;)

publicstaticvoid save()throws IOException{

String name;

BufferedReader stdin =new BufferedReader(new InputStreamReader(System.in));

BufferedWriter out;

System.out.println("give me your filename");

name= stdin.readLine();

out =new BufferedWriter(new FileWriter(name,true));

out.write("datadatadata");

out.close();

}

br,

nomi

[1375 byte] By [nomikaia] at [2007-11-26 13:36:04]
# 1

if you want to save it as a txt file, concatentate .txt to the string the user gives when you create the file writer

public static void save() throws IOException {

String name;

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

BufferedWriter out;

System.out.println("give me your filename");

name= stdin.readLine();

//Add this line, or do this in the FileWriter c'tor out = new BufferedWriter(new FileWriter(name+ ".txt", true));

name = name + ".txt";

out = new BufferedWriter(new FileWriter(name, true));

out.write("datadatadata");

out.close();

}

~Tim

Message was edited by:

SomeoneElse

SomeoneElsea at 2007-7-7 22:20:31 > top of Java-index,Java Essentials,New To Java...
# 2
That was rather cheap but it works :)Any hints where I should start looking at for printing the contents of a folder? I couldn't find any good catches on API documentation.br, nomi
nomikaia at 2007-7-7 22:20:31 > top of Java-index,Java Essentials,New To Java...
# 3
java.io.File.list() and java.io.File.listFiles() .
sabre150a at 2007-7-7 22:20:31 > top of Java-index,Java Essentials,New To Java...
# 4

> That was rather cheap but it works :)

> Any hints where I should start looking at for

> printing the contents of a folder? I couldn't find

> any good catches on API documentation.

>

> br,

> nomi

If you are doing this with a GUI, look at javax.swing.JFileChooser

http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JFileChooser.html

~Tim

SomeoneElsea at 2007-7-7 22:20:31 > top of Java-index,Java Essentials,New To Java...
# 5
File[] contents=new File(System.getProperty("user.dir")).listFiles();should do the trick.
Lord_Jirachia at 2007-7-7 22:20:31 > top of Java-index,Java Essentials,New To Java...
# 6
Hey,I don't have a GUI for my program. Thanks for the help! :)
nomikaia at 2007-7-7 22:20:31 > top of Java-index,Java Essentials,New To Java...