Java Write String to File writeFile(File f, String s)

Here you can find the source of writeFile(File f, String s)

Description

write File

License

Open Source License

Declaration

public static boolean writeFile(File f, String s) 

Method Source Code

//package com.java2s;
/* /* w  w w .j  a  v a  2 s .  c  o m*/
    
 Created on Mar 4, 2005
    
 The Bungee View applet lets you search, browse, and data-mine an image collection.  
 Copyright (C) 2006  Mark Derthick
    
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.  See gpl.html.
    
 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 also contact the author at 
 mad@cs.cmu.edu, 
 or at
 Mark Derthick
 Carnegie-Mellon University
 Human-Computer Interaction Institute
 Pittsburgh, PA 15213
    
 */

import java.io.BufferedWriter;
import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;

public class Main {
    public static boolean writeFile(File f, String s) {
        BufferedWriter out = getWriter(f);
        if (out != null) {
            try {
                out.write(s);
                out.close();
                return true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static BufferedWriter getWriter(String filename) {
        return getWriter(new File(filename));
    }

    public static BufferedWriter getWriter(File file) {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
        } catch (FileNotFoundException e) {
            System.err.println("Can't find file " + file);
            e.printStackTrace();
        }
        return out;
    }

    public static String printStackTrace() {
        (new RuntimeException("Relax.  There's no error, we're just printing the stack trace")).printStackTrace();
        return null;
    }

    public static String printStackTrace(Throwable e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        return sw.toString();
    }
}

Related

  1. writeFile(File f, String content)
  2. writeFile(File f, String content)
  3. writeFile(File f, String content, String encoding)
  4. writeFile(File f, String contents)
  5. writeFile(File f, String entry)
  6. writeFile(File f, String str)
  7. writeFile(File f, String str)
  8. writeFile(File f, String text)
  9. writeFile(File file, Iterable lines)