I have this function that iterates through a directory and is supposed to read in each file and write it out to a generated HTML file. The BufferedReader should be reading in properly because I use the same thing elsewhere. However, in the generated HTML file, I am only getting every other line of data from the original files in the directory. Here is the method that should accomplish this:
// Tests to see if "File" is actually a directory or file,
// then writes out the file if it passes the test
void writeFiles(File directory, BufferedWriter bw) {
try{
for( File file : directory.listFiles() ){
if(!file.isDirectory()) {//is a file lets read it
FileInputStream filestream = new FileInputStream(file);
DataInputStream in = new DataInputStream(filestream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String buff = new String();
bw.write("<b>////////////////////////////////</b><br/>");
bw.write("<b>File: " + file.getName() + "</b><br/>");
bw.write("<b>////////////////////////////////</b><br/>");
while((buff=br.readLine()) != null){
bw.write(br.readLine() + "<br/>");
}
bw.write("`<br/>`");
bw.write("`<br/>`");
}else {//will make it a recursive search
writeFiles(file, bw);
}
}
}catch(FileNotFoundException fnf){
fnf.printStackTrace();
}
catch(IOException io){
io.printStackTrace();
}
}
I apologize for the bad formatting of the code in the question. Because of the HTML, the preformatted text won't let my code appear properly. However, I definitely think I have a File I/O issue in my code. Does anyone have any idea whether it is the BufferedReader or BufferedWriter? Thanks.
Here is your problem:
while((buff=br.readLine()) != null){
bw.write(br.readLine() + "<br/>");
}
You are calling br.readLine() twice.
Try:
while((buff=br.readLine()) != null){
bw.write(buff + "<br/>");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.