[英]Using BufferedWriter to get input from the user and write it to a file
[英]Cannot get BufferedWriter to write out all the data from a file
我具有遍歷目錄的功能,應該讀取每個文件並將其寫到生成的HTML文件中。 BufferedReader應該正確讀入,因為我在其他地方使用了相同的東西。 但是,在生成的HTML文件中,我僅從目錄中的原始文件中獲取每隔一行數據。 這是應該完成此操作的方法:
// 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();
}
}
對於問題中代碼的格式錯誤,我深表歉意。 由於使用HTML,因此預格式化的文本不會讓我的代碼正確顯示。 但是,我絕對認為我的代碼中存在文件I / O問題。 有誰知道它是BufferedReader還是BufferedWriter? 謝謝。
這是您的問題:
while((buff=br.readLine()) != null){
bw.write(br.readLine() + "<br/>");
}
您兩次調用br.readLine()。
嘗試:
while((buff=br.readLine()) != null){
bw.write(buff + "<br/>");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.