Java Properties Save save(String fileName, CharSequence string)

Here you can find the source of save(String fileName, CharSequence string)

Description

Saves the specified character sequence.

License

Open Source License

Parameter

Parameter Description
fileName name of the file
string a sequence of characters

Declaration

public static void save(String fileName, CharSequence string) 

Method Source Code

//package com.java2s;
/*// w  w  w . ja v a 2s. c o m
 * (c) 2011-12 Andreas de Vries
 *
 * 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 3 of the License, or
 * (at your option) any later version.
 *
 * 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, see http://www.gnu.org/licenses
 * or write to the Free Software Foundation,Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301  USA
 * 
 * As a special exception, the copyright holders of this program give you permission 
 * to link this program with independent modules to produce an executable, 
 * regardless of the license terms of these independent modules, and to copy and 
 * distribute the resulting executable under terms of your choice, provided that 
 * you also meet, for each linked independent module, the terms and conditions of 
 * the license of that module. An independent module is a module which is not derived 
 * from or based on this program. If you modify this program, you may extend 
 * this exception to your version of the program, but you are not obligated to do so. 
 * If you do not wish to do so, delete this exception statement from your version.
 */

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.FileWriter;
import java.io.IOException;

import java.util.Properties;

public class Main {
    /** The name of the property file. This file stores configuration data
     *  for the gene package programs, for example the directory which has 
     *  been opened recently for loading or saving files.
     */
    static final String propertyFile = System.getProperty("user.home")
            + System.getProperty("file.separator") + "genes.xml";

    /** Saves the specified character sequence.
     *  @param fileName name of the file
     *  @param string a sequence of characters
     */
    public static void save(String fileName, CharSequence string) {
        File file;
        try {
            Properties props = new Properties();
            props.loadFromXML(new FileInputStream(propertyFile));
            file = new File(props.getProperty("currentDirectory")
                    + System.getProperty("file.separator") + fileName);
        } catch (Exception e) {
            file = new File(fileName);
        }

        // open the file:
        FileWriter output = null;
        try {
            output = new FileWriter(file);
            try {
                output.write(string.toString());
                output.flush();
                System.out.println(file + " saved!");
            } catch (EOFException eof) {
                // do nothing, the stream will be closed in the finally clause
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                if (output != null)
                    output.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
            try {
                Properties props = new Properties();
                props.setProperty("currentDirectory", file.getParent());
                props.storeToXML(new FileOutputStream(propertyFile), null);
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }
        }
    }
}

Related

  1. save()
  2. save()
  3. save(File file, Properties properties)
  4. save(Properties properties, File propertiesFile)
  5. save(Properties props, File file, String comment)
  6. saveBuildConfiguration(IFile ifile)
  7. saveConfig()
  8. saveConfig(File settingsFile, Properties properties)
  9. saveDefaultScriptDirectory(String path)