Results 1 to 6 of 6

Thread: Open a txt file and write into it

  1. #1
    Join Date
    Mar 2010
    Posts
    222

    Open a txt file and write into it

    Hello,
    I am starting with Java. How can I open a txt file and then add text to the end of this file? What are the packages should I use to do this and I fairly new to the input and output concept in java and so I need you help. Any help is appreciated. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    333

    Re: Open a txt file and write into it

    Hello,
    Just follow this code and you will get what you want
    Code:
    try {
         FileOutputStream flout = new FileOutputStream(directory / file.txt);
         flout.write(text.getBytes());
         flout.close();
    }
    catch(IOException ioe) {
         / / Handle exceptions related to file access
    }

  3. #3
    Join Date
    Nov 2009
    Posts
    359

    Re: Open a txt file and write into it

    Hello,
    We must use this constructor
    Code:
     FileWriter(File file, boolean append)
    putting append to true, you can write at the end of your file. This is the most important part, so I think you should use this constructor, if you not going to use this then you will not append the data to the end of the file. I hope you got what I am trying to explain here.

  4. #4
    Join Date
    Dec 2009
    Posts
    192

    Re: Open a txt file and write into it

    Hello,
    I am new to java, but I have a code which may interest you, take a look at it
    Code:
    import java.io.File;
     
    Public class mfl {
     
    	/ / File fl;
    	
      Public mfl() {
    		great();
    	}
     
      Public String mthd(String path) throws Exception{	  
    	  File fl = new File(path);  
    	  if(fl.exists()& & File.canRead()){
    		  
    		  return "Ok"; / / message
    	  }
    	  else
    	  {
    		  return "Not Ok"; / / message
    	  }
      }
      
    }

  5. #5
    Join Date
    Nov 2009
    Posts
    518

    Re: Open a txt file and write into it

    Hello,
    As mentioned just before, Fi
    Code:
    leWriter (File file, boolean append).
    If your file exists and that you want to add lines to the end, you make a
    Here is the code for it
    Code:
    FileWriter wr = new FileWriter(file,true);
    I recommend you to just go through the java.io package and the API which are used by this package.

  6. #6
    Join Date
    Nov 2009
    Posts
    347

    Re: Open a txt file and write into it

    Hello,
    I have modified this above code, see if this helps you
    Code:
    Public class mfl {
     
    	
      Public mfl() {
    		great();
    	}
     
      Public String mthd(String pt, String str) throws Exception{	  
    	  File fl = new File(pt);  
    	  if(fl.exists()& & File.canRead()){
    		  
    		  FileWriter wr = new FileWriter(fl, true);
    wr.append(chain);
    wr.close();
    		  return "The Station" "+ chain +"> Was added in <"+ + pt">";
    	  }
    	  else
    	  {
    		  return "The fl does not exist";
    	  }
      }

Similar Threads

  1. Read and Write a .txt file with Java
    By Mulan in forum Software Development
    Replies: 4
    Last Post: 28-07-2010, 02:03 AM
  2. Javascript write to file
    By Din_the1 in forum Software Development
    Replies: 4
    Last Post: 29-05-2010, 12:38 PM
  3. Write to the middle of a file
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 01:25 PM
  4. Write to an XML file without overwriting
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 18-01-2010, 11:11 AM
  5. How to Write a Batch File ?
    By Ebenezer in forum Operating Systems
    Replies: 2
    Last Post: 30-07-2009, 02:39 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,646,047.84919 seconds with 16 queries