Reputation: 31
so i got this weird error. What i'm trying to do is this:
Point 1 & 3 i've got covered - however when i try to read all the xml files, something is wrong. I have all the xml files i want to read in an arraylist (about 30 files), and i then want to read them like this (note: this method is called in a for-loop which iterates over the 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 to read");
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(mFile.getPath()));
for (String sCurr = ""; (sCurr = br.readLine()) != null;) {
// System.out.println(sCurr);
listToRecreate.add(sCurr);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
editFile(path);
}
What then happens is that only half the content of the files are read. The weird thing is though, that if i pluck one xml file out and attempt to read that, it all works as it should. What could be the error here? Thanks in advance :)
EDIT: How i write to file
private static void editFile(String path) {
// System.out.println(path);
try {
PrintWriter writer = new PrintWriter(new File(path));
for (String str : listToRecreate) {
// System.out.println(str);
int j = 0;
for (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: I just realized that it might be my unzip method that is messing with me. UPDATE: This code works now. Thanks to 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 is = 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();
}
}
Upvotes: 2
Views: 344
Reputation: 31
For those who might find this in the future facing the same problem. I discovered that it was my unzip method that didn't fully unzip my files, and that my read/write methods checked out. I have updated the code so the unzip method works.
Thanks for everyone trying to help me out!
Upvotes: 1