Sunday, 15 April 2012

java - How to save output to a txt file -



java - How to save output to a txt file -

quick q

i have loop finds files in directory, want add together line of code write these results txt file, how best

current code :

public string filesinfolder() { // list files in directory, want create feature on page can display user string path = newdestination; system.out.println("starting searching files in directory"); // making sure called string files; file folder = new file(path); file[] listoffiles = folder.listfiles(); (int = 0; < listoffiles.length; i++) { if (listoffiles[i].isfile()) { files = listoffiles[i].getname(); system.out.println(files); } } homecoming ""; }

you can utilize filewriter , stringwriter together.

public string filesinfolder() throws ioexception { filewriter fw = new filewriter("file.txt"); stringwriter sw = new stringwriter(); // list files in directory, want create feature on page can display user string path = newdestination; system.out.println("starting searching files in directory"); // making sure called string files; file folder = new file(path); file[] listoffiles = folder.listfiles(); (int = 0; < listoffiles.length; i++) { if (listoffiles[i].isfile()) { files = listoffiles[i].getname(); sw.write(files); system.out.println(files); } } fw.write(sw.tostring()); fw.close(); homecoming ""; }

java

No comments:

Post a Comment