How to write file using FileWriter class in java?

Below code shows how to write a text file using FileWriter class in java –

/****************************************************************************************
 * Created on 08-2011 Copyright(c) http://kodehelp.com All Rights Reserved.
 ****************************************************************************************/
package com.kodehelp.java.io;
 
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
 
/**
 * Created by Vigilance India.
 * User: http://kodehelp.com
 * Date: Aug 1, 2011
 */
public class FileWriterExample {
    public static void main(String args[]){
        FileWriter fileWriter = null;
        try{
            String textToWrite = "This is the string which will be written in the text file";
            fileWriter = new FileWriter("/demo.txt");
            fileWriter.write(textToWrite);
        }catch(FileNotFoundException fnfEx){
            fnfEx.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(fileWriter!=null)
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
}

5 comments

  1. Aaavuami says:

    Muchas gracias por el código, es altamente funcionable. Desarrolle un Cliente SFTP Streaming en c# y por requerimientos, tuve que modelar el cliente en Java, el problema que tenía, es que no era Streaming, y al operar con archivos demasiados grandes, no funcionaba el cliente. Mil gracias…

  2. Aaavuami says:

    Muchas gracias por el código, es altamente funcionable. Desarrolle un Cliente SFTP Streaming en c# y por requerimientos, tuve que modelar el cliente en Java, el problema que tenía, es que no era Streaming, y al operar con archivos demasiados grandes, no funcionaba el cliente. Mil gracias…

  3. John Tran says:

    I have used Zehon SFTP, it only takes two lines of code to achieve what you need.  Check out some of the examples here, http://zehon.com/SFTP_samples.htm

  4. John Tran says:

    I have used Zehon SFTP, it only takes two lines of code to achieve what you need.  Check out some of the examples here, http://zehon.com/SFTP_samples.htm

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.