Here you can find the source of writeFile(String path, String text, boolean orverWite)
public static void writeFile(String path, String text, boolean orverWite)
//package com.java2s; /*//ww w. jav a 2s . c o m * @(#)Utils.java * * This program is version 2.0 of JDC Education Kit for Optical Experiments. * Copyright (C) Jasper Display Corporation 2013. * This program is free software; you can redistribute it * and/or modify it under the terms of the GNU General Public * License version 2.0 as published by the Free Software Foundation. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * You may contact Jasper Display Corporation by email at info@jasperdisplay.com * or by telephone at +1-408-855-6640. * More information is provided at http://www.jasperdisplay.com/. */ import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; public class Main { public static void writeFile(String path, String text, boolean orverWite) { try { String oldText = ""; if (!orverWite) { oldText = readFile(path); } FileOutputStream fos = new FileOutputStream(path); Writer out = new OutputStreamWriter(fos, "UTF-8"); out.write(oldText + text); out.close(); } catch (Exception e) { e.printStackTrace(); } } public static String readFile(String filePath) { String rs; FileInputStream fos; Reader reader; BufferedReader buferReader; StringBuilder text; try { fos = new FileInputStream(filePath); reader = new java.io.InputStreamReader(fos, "UTF-8"); buferReader = new BufferedReader(reader); text = new StringBuilder(); String line = null; while ((line = buferReader.readLine()) != null) { text.append(line + "\n"); } reader.close(); rs = text.toString(); } catch (Exception ex) { rs = ""; ex.printStackTrace(); } return rs; } }