Wednesday 15 February 2012

java - Why is is my program not pulling in the files from the folder -



java - Why is is my program not pulling in the files from the folder -

i have programme suppose read files in folder , combine files on file , place them new folder. of files not beingness pulled in , not know why.

the file names wonder1.txt, wonder2.txt, wonder3.txt, , wonder4.txt , folder name alice, few of files in new folder.

import java.io.*; import java.util.scanner; import java.util.*; import java.lang.*; import java.io.printwriter; public class alice { public static void main(string[] args) throws filenotfoundexception, ioexception { file folder = new file("/users/dandre/desktop/alice"); //reads in files in folder (final file fileentry : folder.listfiles()) { string filename = fileentry.getabsolutepath(); bufferedreader br = new bufferedreader(new filereader(filename)); seek { stringbuilder stringbuilder = new stringbuilder(); string line = br.readline(); while (line != null) { stringbuilder.append(line); stringbuilder.append("\n"); line = br.readline(); } /** * pass original file content string method * creates new file same content. */ newfile(stringbuilder.tostring()); } { br.close(); } } } public static void newfile(string filecontent) { seek { string newfilelocation = "/users/dandre/desktop/final/final_copy.txt"; printwriter author = new printwriter(newfilelocation); writer.write(filecontent);//writes original file content new file writer.close(); system.out.println("file created"); } grab (exception e) { e.printstacktrace(); } } }

the problem solution haven't initialize printwriter in append mode, because of new file gets overwritten content of lastly file written.

public static void newfile(string filecontent) { seek { string newfilelocation = "c:\\users\\shayan\\desktop\\files2\\final_copy.txt"; printwriter author = new printwriter(new fileoutputstream(new file(newfilelocation), true /* append = true */)); writer.write(filecontent);//writes original file content new file writer.close(); system.out.println("file created"); } grab (exception e) { e.printstacktrace(); }

}

the lastly argument in constructor of fileoututstream set true, indicating should opened in append mode.

java

No comments:

Post a Comment