Results 1 to 6 of 6

Thread: How to write string data to file using java program?

  1. #1
    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.

  2. #2
    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();
        }
      }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    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);
        }
      }
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    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 ;
    	} }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    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");        
        }
    }

  6. #6
    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");
        }
      }

Similar Threads

  1. Java program to iterate a subset of a string.
    By Nadiaa in forum Software Development
    Replies: 5
    Last Post: 19-03-2012, 01:53 PM
  2. Need help to write this program in java?
    By frkadeel in forum Software Development
    Replies: 1
    Last Post: 01-12-2010, 03:58 PM
  3. Replies: 4
    Last Post: 30-01-2010, 07:23 PM
  4. program to reverse a string in java.
    By Deepest BLUE in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 11:03 PM
  5. how to write program on palindrome in java?
    By Linoo in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 05:19 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,691,319.14641 seconds with 17 queries