Here you can find the source of save(String targetDir, String filename, String textToSave)
Parameter | Description |
---|---|
targetDir | the target directory |
filename | the target filename |
textToSave | the content for the file |
public static boolean save(String targetDir, String filename, String textToSave)
//package com.java2s; /******************************************************************************* * Copyright (c) 2013 itemis AG (http://www.itemis.de). * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html *******************************************************************************/ import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class Main { /**/*from www . j av a 2 s. co m*/ * Helper for saving a text to file * * @param targetDir the target directory * @param filename the target filename * @param textToSave the content for the file * @return true if ok */ public static boolean save(String targetDir, String filename, String textToSave) { // ensure that directory is available File dir = new File(targetDir); if (!(dir.exists() || dir.mkdirs())) { System.err.println("Error: couldn't create directory " + targetDir + "!"); return false; } // delete file prior to saving File file = new File(targetDir + "/" + filename); file.delete(); // save contents to file try { BufferedWriter out = new BufferedWriter(new FileWriter(file)); out.write(textToSave); out.close(); System.out.println("Created file " + file.getAbsolutePath()); } catch (IOException e) { return false; } return true; } }