how to open all files in a directory in java, read them, creat new files, write to them -
i sorry dont have code, in few answers how make file , write it, have question. give path folder in compilation, , want each file ends .jack create same file name ends .xml
and open xml file , write it. exemple:
start.jack bet.jack => start.xml bet.xml and in each xml file write stuff according whats written in jack file.
so need open jack file, read it, , write xml file itself.
i hope explained myself correctly.
my code:
public string readfile(string filename) { string content = null; file file = new file(filename); try { filereader reader = new filereader(file); char[] chars = new char[(int) file.length()]; reader.read(chars); content = new string(chars); reader.close(); } catch (ioexception e) { e.printstacktrace(); } return content; } i took lines stackoverflow, , worked perfectly
file f = new file("your folder path here");// folder path string filelist = f.list(); // gives list of files in folder. for(string str : filelist){ if(str.endswith(".jack")){ // read content of file "str" , store in veriable filereader reader = new filereader("your folder path"+str); char[] chars = new char[(int) new file("your folder path"+str).length()]; reader.read(chars); string content = new string(chars); reader.close(); // write content in xml file bufferedwriter bw = new bufferedwriter( new filewriter("you folder path"+str.replace(".jack",".xml"))); bw.write(content); //now can write variable in file. bw.close(); } }
Comments
Post a Comment