iT邦幫忙

2025 iThome 鐵人賽

0

目的

將資料寫入food_log.csv
將資料庫中的結構化資料轉成可攜帶、可讀、可分析的純文字檔

1.主選單
新增功能9.CSV匯出功能
https://ithelp.ithome.com.tw/upload/images/20251020/20178845fQwNfZjSL2.jpg


2.FoodLogDAO
創一個新方法 , 會回傳一個List集合 , 把userId丟入。
sql方法前面有講過。

創一個FoodLog物件叫log2 , 用get方法傳入參數(含有food表格的參數)。
https://ithelp.ithome.com.tw/upload/images/20251020/20178845WQQvgt7Lry.jpg
https://ithelp.ithome.com.tw/upload/images/20251020/20178845zluiIl00Yz.jpg


3.foodLog
要宣告從food表JOIN的屬性 , 並創一個方法 , 把兩個表的屬性傳入。

新增food表的5個getter。
https://ithelp.ithome.com.tw/upload/images/20251020/20178845BnK5JuKFMW.jpg
https://ithelp.ithome.com.tw/upload/images/20251020/20178845keTIfnY3Tp.jpg


4.Main
驗證使用者是否登入。
宣告一個不可變的字串變數FILE_NAME存匯出檔案名。

呼叫上方剛創的Food , 並加上驗證錯誤處理。

建立一個檔案輸出串流(File Out Stream)將原始位元組寫入前面定義的檔案。
建立一個輸出串流寫入器 , 照指定的UTF-8編碼成位元組再通過fos寫入檔案。

寫入編號就可以顯示中文。

建一個StringBuilder欄位 , 供後面使用。

失敗處理。
https://ithelp.ithome.com.tw/upload/images/20251020/20178845mRRDSpjMhm.jpg
https://ithelp.ithome.com.tw/upload/images/20251020/20178845SLq5W7b6PL.jpg


5.結果
https://ithelp.ithome.com.tw/upload/images/20251020/201788452SgWAY954m.jpg
https://ithelp.ithome.com.tw/upload/images/20251020/201788451NJzvYInrZ.jpg


上一篇
測試功能2
下一篇
加入CSV匯入功能
系列文
飲食營養追蹤 + 健康分析 Web 系統30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言