Java Text File Save writeFile(String path, String text, boolean orverWite)

Here you can find the source of writeFile(String path, String text, boolean orverWite)

Description

write File

License

Open Source License

Declaration

public static void writeFile(String path, String text, boolean orverWite) 

Method Source Code


//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;
    }
}

Related

  1. writeFile(String path, String fileName, String content)
  2. writeFile(String path, String fileName, String content, String charset)
  3. writeFile(String path, String fileName, String data)
  4. writeFile(String path, String text)
  5. writeFile(String path, String text)
  6. writeFile(String path, Vector contents)
  7. writeFile(String pContent, String pPath)
  8. writeFile(String ruta, String nombre, byte[] archivo)
  9. WriteFile(String sFileName, String content)