Pin It

How to write to a file line by line using Apache Commons-io API in Java ?.

Program to demonstrate how to write to a file line by line using Apache Commons-io API in Java.

package com.hubberspot.apache.commons.examples;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.commons.io.FileUtils;

public class WriteContentsToFile {

    public static void main(String[] args) {

        Collection< String > welcome = new ArrayList< String >();
        welcome.add("Welcome to Hubberspot.com");
        welcome.add("Its a site for Java developers.");
        welcome.add("Learn Java by Examples.");
        welcome.add("Everything you want to know about Java.");
        welcome.add("Tutorials, Source Codes, SCJP, SCWCD, Frameworks,");
        welcome.add("Java EE and Design Patterns.");
        
        File file = new File("D:/hubberspot/welcome.txt");
        
        try {
            
            FileUtils.writeLines(file, welcome);
        
        } catch (IOException e) {
           
            e.printStackTrace();
        }

    }

}



Output of the program :


Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot