Java Write String to File writeFile(String filename, String text)

Here you can find the source of writeFile(String filename, String text)

Description

Write a text file.

License

Apache License

Parameter

Parameter Description
filename the name of the file, can include path.
the text to be written to the file (lines separated by '#').

Declaration

public static void writeFile(String filename, String text) throws IOException 

Method Source Code

//package com.java2s;
/*// ww  w .  j a  va  2s .c o  m
Copyright 2008 Flaptor (flaptor.com) 
    
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.BufferedWriter;
import java.io.File;

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

public class Main {
    /**
     * Write a text file.
     * @param filename the name of the file, can include path.
     * @param the text to be written to the file (lines separated by '#').
     */
    public static void writeFile(String filename, String text) throws IOException {
        File file = new File(filename);
        if (null != file.getParentFile()) {
            file.getParentFile().mkdirs();
        }
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        String[] lines = text.split("#");
        for (String line : lines) {
            writer.write(line);
            writer.newLine();
        }
        writer.close();
    }
}

Related

  1. writeFile(String fileName, String output)
  2. writeFile(String filename, String s)
  3. writeFile(String fileName, String source)
  4. writeFile(String filename, String str)
  5. writeFile(String filename, String string)
  6. writeFile(String filename, String text)
  7. writeFile(String filename, String text)
  8. writeFile(String fileName, String text)
  9. writeFile(String filename, String text, boolean create)