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

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-03-2010
Member
 
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.
Reply With Quote
  #2  
Old 25-03-2010
Member
 
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
}
Reply With Quote
  #3  
Old 25-03-2010
Member
 
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.
Reply With Quote
  #4  
Old 25-03-2010
Member
 
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
	  }
  }
  
}
Reply With Quote
  #5  
Old 25-03-2010
Member
 
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.
Reply With Quote
  #6  
Old 25-03-2010
Member
 
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";
	  }
  }
Reply With Quote
Reply

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



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Open a txt file and write into it"
Thread Thread Starter Forum Replies Last Post
Read and Write a .txt file with Java Mulan Software Development 4 28-07-2010 02:03 AM
Javascript write to file Din_the1 Software Development 4 29-05-2010 12:38 PM
Write to the middle of a file Miles Runner Software Development 5 22-01-2010 01:25 PM
Write to an XML file without overwriting TechGate Software Development 5 18-01-2010 11:11 AM
How to Write a Batch File ? Ebenezer Operating Systems 2 30-07-2009 02:39 PM


All times are GMT +5.5. The time now is 09:30 AM.