Android Text File Write writeString(String string, File file, boolean append)

Here you can find the source of writeString(String string, File file, boolean append)

Description

Simply executes #writeBytes writeBytes ( string.getBytes(), file, append ).

License

Open Source License

Parameter

Parameter Description
append specifies whether to append to an existing file or to overwrite its contents

Exception

Parameter Description
IllegalArgumentException if string is null; file is null; file is a directory
SecurityException if a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies write access to file
IOException if an I/O problem occurs

Declaration

public static void writeString(String string, File file, boolean append)
        throws IllegalArgumentException, SecurityException, IOException 

Method Source Code

/*//from   ww w.  j a  v a 2s . c  o  m
Copyright ? 2008 Brent Boyer

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser 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 Lesser GNU General Public License for more details.

You should have received a copy of the Lesser GNU General Public License along with this program (see the license directory in this project).  If not, see <http://www.gnu.org/licenses/>.
 */

import bb.science.FormatUtil;
import bb.util.Check;
import bb.util.StringUtil;
import bb.util.ThrowableUtil;
import bb.util.logging.LogUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.util.Random;
import java.util.logging.Level;
import org.junit.Assert;
import org.junit.Test;

public class Main{
    /**
     * Simply executes <code>{@link #writeBytes writeBytes}( string.getBytes(), file, append )</code>.
     * Note: that call to <code>string.{@link String#getBytes() getBytes}</code>
     * implicitly uses the platform's default char encoding to convert the String into a byte[].
     * <p>
     * @param append specifies whether to append to an existing file or to overwrite its contents
     * @throws IllegalArgumentException if string is null; file is null; file is a directory
     * @throws SecurityException if a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies write access to file
     * @throws IOException if an I/O problem occurs
     */
    public static void writeString(String string, File file, boolean append)
            throws IllegalArgumentException, SecurityException, IOException {
        Check.arg().notNull(string);

        writeBytes(string.getBytes(), file, append);
    }
    /**
     * Writes bytes into file.
     * All resources (e.g. OutputStreams) that are created as part of this process will be closed upon method return.
     * <p>
     * @param append specifies whether to append to an existing file or to overwrite its contents
     * @throws IllegalArgumentException if bytes is null; file is null; file is a directory
     * @throws SecurityException if a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies write access to file
     * @throws IOException if an I/O problem occurs
     */
    public static void writeBytes(byte[] bytes, File file, boolean append)
            throws IllegalArgumentException, SecurityException, IOException {
        Check.arg().notNull(bytes);
        Check.arg().notNull(file);
        if (file.isDirectory())
            throw new IllegalArgumentException("file = " + file.getPath()
                    + " refers to directory, not a normal text file");
        //if (!file.canWrite()) throw new IllegalArgumentException("file = " + file.getPath() + " refers to a file that cannot be read by this application");   // DO NOT UNCOMMENT: it will fail if the file does not exist, which could be a common situation; instead let the write below fail

        OutputStream out = null;
        try {
            out = new FileOutputStream(file, append);
            out.write(bytes);
        } finally {
            StreamUtil.close(out);
        }
    }
}

Related

  1. writeFileWithBom(File file, String content, String encoding)
  2. writeLargerTextFile(File file, List aLines)
  3. writeLineTextFile(File file, String data)
  4. writeLineTextFile(File file, String[] dataArray)
  5. writeSet2File(Set set, String filePath)
  6. writeStringToFile(String content, String fileName)
  7. writeStringToFile(String content, String fileName, boolean append)
  8. writeStringToFile(String string, File file)
  9. writeStringToFile(String stringToWrite, String filename)