การเขียนข้อมูลลง Text File
ตัวอย่างนี้เป็นการเขียนข้อมูลลง Text File ครับ
/** * File Name : WriteTextFile.java * Created Date : Nov 9, 2009 : 10:19:34 AM * Copyright © 2009 www.fun4station.com */ package com.fun4station.example; import java.io.File; import java.io.FileWriter; import java.io.IOException; /** * @Author Supot Saelao * @Version 1.0 */ public class WriteTextFile { public static void main(String[] args) { WriteTextFile file = new WriteTextFile(); file.writeTextFile("C:/data/textfile.txt"); } private void writeTextFile(String fileName){ FileWriter writer = null; String data = "Test data write to text file...."; try { writer = new FileWriter(new File(fileName), true); writer.write(data); writer.flush(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }finally{ try{ if(writer != null) writer.close(); }catch (Exception e) {} } } }
หากไม่ต้องการเขียนข้อมูลต่อท้ายข้อมูลเดิมในไฟล์ ก็ให้เปลี่ยน parameter “true” เป็น “false” ครับ
หมายเหตุ :
1. นอกจาก FileWriter แล้วยังมี Class อื่นๆที่ใช้ในการเขียน Text file อีกครับ
กำลังหาพอดีเลยครับ ขอบคุณครับ
Hey there! Would you mind if I share your blog with my facebook group? There’s a lot of folks that I think would really appreciate your content. Please let me know. Thanks