Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to write string data to file using java program?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 21-01-2010
Member
 
Join Date: Nov 2009
Posts: 57
How to write string data to file using java program?
  

Hello to all,
I am new to this forum. I am B.Sc.I.T. graduate. Yesterday one of my friend asked me one question like How to write string data to file using java program? I unable to give answer. That's why I am asking this question on this forum. If you know solution of this question then give it to me.
Thanks in advanced.


Last edited by Linoo : 21-01-2010 at 08:55 PM.
Reply With Quote
  #2  
Old 21-01-2010
Member
 
Join Date: Apr 2008
Posts: 1,948
Re: How to write string data to file using java program?

I have written following code to write string data to file. Just go through it.

Code:
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class WriteToFileExample
{
  public static void main(String[] args)
{
try
{
      File f = new File("java.txt");
      
      String d = "started Learning Java Programming";
 
      
      FileUtils.writeStringToFile(f, d);
    } catch (IOException e)
    {
      e.printStackTrace();
    }
  }
}
Reply With Quote
  #3  
Old 21-01-2010
Member
 
Join Date: May 2008
Posts: 2,007
Re: How to write string data to file using java program?

Just go through following code.

Code:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class MainClass {
  public static void main(String[] args) {
    String p = new String("www.java2s.com\n");

    File aF = new File("test.txt");   
    FileOutputStream outputFile = null;  
    try {
      outputFile = new FileOutputStream(a, true);
      System.out.println("File stream created successfully.");
    } catch (FileNotFoundException e) {
      e.printStackTrace(System.err);
    } 

    FileChannel outChannel = outputFile.getChannel();
    
    ByteBuffer b = ByteBuffer.allocate(1024);
    System.out.println("New buffer:           position = " + b.position()
                       + "\tLimit = " + b.limit() + "\tcapacity = "
                       + b.capacity());


    for (char ch : p.toCharArray()) {
      buf.putChar(ch);
    }
    System.out.println("Buffer after loading: position = " + b.position()
                       + "\tLimit = " + b.limit() + "\tcapacity = "
                       + b.capacity());
    b.flip();
    System.out.println("Buffer after flip:    position = " + buf.position() 
                       + "\tLimit = " + b.limit() + "\tcapacity = " 
                       + b.capacity());

    try {
      outChannel.write(b);
      outputFile.close();
      System.out.println("Buffer contents written to file.");
    } catch (IOException e) {
      e.printStackTrace(System.err);
    }
  }
}
Reply With Quote
  #4  
Old 21-01-2010
Member
 
Join Date: Apr 2008
Posts: 2,000
Re: How to write string data to file using java program?

You can easily write string data to file using following java program.


Code:
import java.io.*;
import java.util.*;
 
class WriteTest1
{
	public static void main(String[] args)
	{	
		try {
			BufferedWriter out1 = new BufferedWriter(new FileWriter("java.txt"));
			out.write("aString");
			out1.newLine();
			out1.write("this is a");
			out1.newLine();
			out1.write("ttest");
			out1.close();
		}
		catch (IOException e)
		{
			System.out.println("Exception ");		
		}
 
		return ;
	} }
Reply With Quote
  #5  
Old 21-01-2010
Member
 
Join Date: May 2008
Posts: 2,291
Re: How to write string data to file using java program?

Hey you can use following code to do this process.

Code:
import java.io.*;

public class WriteTextFileEg{
    public static void main(String[] args)throws IOException{
    Writer op = null;
    String textfile = "alfad khan";
    File file = new File("writing.txt");
    output = new BufferedWriter(new FileWriter(file));
    op.write(textfile);
    op.close();
    System.out.println("Your file has been written");        
    }
}
Reply With Quote
  #6  
Old 21-01-2010
Member
 
Join Date: Nov 2005
Posts: 1,323
Re: How to write string data to file using java program?

You have to use following code to write string data to file. Just try to understand each line.


Code:
import java.io.*;

public class WriteFile{

    public static void main(String[] args) throws IOException{

      File f1 =new File("textfile1.txt");
      FileOutputStream fop1=new FileOutputStream(f);

      if(f1.exists()){
      String strp="This data is written through the program";
          fop1.write(strp.getBytes());

          fop1.flush();
          fop1.close();
          System.out.println("following data has been written");
          }

          else
            System.out.println("This file is not exist");
    }
  }
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to write string data to file using java program?"
Thread Thread Starter Forum Replies Last Post
Java program to iterate a subset of a string. Nadiaa Software Development 5 19-03-2012 02:53 PM
Need help to write this program in java? frkadeel Software Development 1 01-12-2010 03:58 PM
How to compare string using java program regardless of their case? MAGALY Software Development 4 30-01-2010 07:23 PM
program to reverse a string in java. Deepest BLUE Software Development 3 26-11-2009 11:03 PM
how to write program on palindrome in java? Linoo Software Development 3 26-11-2009 05:19 PM


All times are GMT +5.5. The time now is 03:06 AM.