Java Text File Write nio writeString(File file, String content)

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

Description

write String

License

Apache License

Declaration

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

Method Source Code


//package com.java2s;
/*//from w  ww .j a  v a  2s. c om
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.BufferedOutputStream;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import java.nio.charset.StandardCharsets;

public class Main {
    public static void writeString(File file, String content) throws IOException {
        writeBytes(file, content.getBytes(StandardCharsets.UTF_8), 1);
    }

    public static void writeBytes(File file, byte[] pattern, int repeat) throws IOException {
        file.deleteOnExit();
        file.getParentFile().mkdirs();
        OutputStream out = null;
        try {
            out = new BufferedOutputStream(new FileOutputStream(file));
            for (int i = 0; i < repeat; i++) {
                out.write(pattern);
            }
            out.close();
            out = null;
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (final IOException e) {
                // Suppressed due to an exception already thrown in the try block.
            }
        }
    }

    public static boolean mkdirs(File directory) {
        if (directory == null) {
            return false;
        }

        if (directory.exists()) {
            return false;
        }
        if (directory.mkdir()) {
            return true;
        }

        File canonDir = null;
        try {
            canonDir = directory.getCanonicalFile();
        } catch (IOException e) {
            return false;
        }

        File parentDir = canonDir.getParentFile();
        return (parentDir != null && (mkdirs(parentDir) || parentDir.exists()) && canonDir.mkdir());
    }
}

Related

  1. writeLines(File file, Iterable lines)
  2. writeLines(String fileName, String[] lines)
  3. writeLinesToFile(final File file, final String... lines)
  4. writeString(DataOutput dataOutput, String string)
  5. writeString(DataOutput dOut, String value)
  6. writeString(final File file, final String str)
  7. writeString(OutputStream buffer, String s)
  8. writeStringToFile(File file, String content, String value)
  9. writeStringToFile(File file, String string)