java - Bufferedreader fails to read whole file -
so got weird error. i'm trying this:
- open zip file , extracting contents.
- iterate through contents of file , fix whitespaces of files aswell places files referenced, in several xmls.
- zip contents.
point 1 & 3 i've got covered - when try read xml files, wrong. have xml files want read in arraylist (about 30 files), , want read them (note: method called in for-loop iterates on 30 xml files):
private static void readxmlfile(string path) { // system.out.println("deleting"); iterator<string> iter = listtorecreate.iterator(); while (iter.hasnext()) { string str = iter.next(); if (listtorecreate.size() > 0) { iter.remove(); } } file mfile = new file(path); // system.out.println(path); // system.out.println("beginning read"); bufferedreader br = null; try { br = new bufferedreader(new filereader(mfile.getpath())); (string scurr = ""; (scurr = br.readline()) != null;) { // system.out.println(scurr); listtorecreate.add(scurr); } } catch (exception e) { e.printstacktrace(); } { try { br.close(); } catch (ioexception e) { e.printstacktrace(); } } editfile(path); } what happens half content of files read. weird thing though, if pluck 1 xml file out , attempt read that, works should. error here? in advance :)
edit: how write file
private static void editfile(string path) { // system.out.println(path); try { printwriter writer = new printwriter(new file(path)); (string str : listtorecreate) { // system.out.println(str); int j = 0; (string tag : listoftagnames) { str = replacetag(str, listoftagnames.get(j)); j++; } // system.out.println(str); writer.println(str); } writer.close(); } catch (exception e) { e.printstacktrace(); } } edit 2: realized might unzip method messing me. update: code works now. http://www.avajava.com/tutorials/lessons/how-do-i-unzip-the-contents-of-a-zip-file.html
public static void unzipfile(string filepath, string destpath) { try { zipfile zipfile = new zipfile(filepath); enumeration<?> enu = zipfile.entries(); while (enu.hasmoreelements()) { zipentry zipentry = (zipentry) enu.nextelement(); string name = zipentry.getname(); long size = zipentry.getsize(); long compressedsize = zipentry.getcompressedsize(); //system.out.printf("name: %-20s | size: %6d | compressed size: %6d\n", // name, size, compressedsize); file file = new file(destpath + file.separator + name); if (name.endswith("/")) { file.mkdirs(); continue; } file parent = file.getparentfile(); if (parent != null) { parent.mkdirs(); } inputstream = zipfile.getinputstream(zipentry); fileoutputstream fos = new fileoutputstream(file); byte[] bytes = new byte[1024]; int length; while ((length = is.read(bytes)) >= 0) { fos.write(bytes, 0, length); } is.close(); fos.close(); } zipfile.close(); } catch (ioexception e) { e.printstacktrace(); } }
for might find in future facing same problem. discovered unzip method didn't unzip files, , read/write methods checked out. have updated code unzip method works.
thanks trying me out!
Comments
Post a Comment