Java Text File Write by Charset writeContent(String content, String charset, File file)

Here you can find the source of writeContent(String content, String charset, File file)

Description

write Content

License

Apache License

Declaration

public static void writeContent(String content, String charset, File file) throws IOException 

Method Source Code

//package com.java2s;
/**/*from ww w.  j  a v  a2 s  . c om*/
 * Copyright 2013-2018 the original author or authors from the Jeddict project (https://jeddict.github.io/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.nio.charset.Charset;

public class Main {
    public static void writeContent(String content, File file) throws IOException {

        writeContent(content, Charset.defaultCharset().toString(), file);
    }

    public static void writeContent(String content, String charset, File file) throws IOException {

        if (file.isDirectory()) {
            throw new IOException("Cannot write content to directory");
        }

        if (file.exists()) {
            file.createNewFile();
        }

        FileOutputStream fos = new FileOutputStream(file);
        fos.write(content.getBytes(charset));
        fos.close();
    }

    public static byte[] getBytes(File file) throws IOException {

        if (!file.exists()) {
            throw new FileNotFoundException();
        }

        if (file.length() > Integer.MAX_VALUE) {
            throw new IOException("file is too large for single read");
        }

        byte[] bytes = new byte[(int) file.length()];

        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            int read = fileInputStream.read(bytes);
            if (read != file.length()) {
                throw new IOException("could not read entire file");
            }
        }

        return bytes;
    }
}

Related

  1. write(Path path, Iterable lines, Charset cs, OpenOption... options)
  2. write(Path path, Iterable lines, Charset cs, OpenOption... options)
  3. write(String toWrite, File file, Charset charset)
  4. writeAll(Path path, String data, Charset cs)
  5. writeAll(String data, OutputStream outputStream, Charset charset)
  6. writeFile(File file, String content, Charset charset)
  7. writeFile(String content, File file, Charset encoding)
  8. writeFile(String path, String content, Charset charset)
  9. writeFile(String path, String content, Charset encoding)