Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Have Programming Question? Ask it here!

Srilakhsmi v k
2 Answer(s)      5 years and 6 months ago
Posted in : Java Beginners

View Answers

March 12, 2009 at 8:16 PM

use exists() method from File to find if the file already exists then write to the file:

String str = "this is test"; // or read from the command line
File file = new File("test.txt");
BufferedWriter bw;
if(file.exists()){ // check whether the file already exists, if true just append to the file otherwise create new one
bw = new BufferedWriter(new FileWriter(file,true));
} else {
bw = new BufferedWriter(new FileWriter(file));

I hope this will help you.


March 19, 2009 at 1:11 AM

hi friend

This code will works fine.

class FileWrite
public static void main(String args[])
File file = new File("Hello.txt");

FileWriter fstream = new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fstream);
System.out.println("File does not exist");
}catch (Exception e){
System.err.println("Error: " + e.getMessage());

RoseIndia Team

Related Tutorials/Questions & Answers:
files  write a java program to calculate the time taken to read a given number of files. file names should be given at command line.   Hello Friend, Try the following code: import*; import java.util.*; class
header files
header files   do you have a all header files of java
Uploading files
Uploading files  Hi, Please provide html code for my question. I need to insert the browsed files temporarily to text area while attaching more than one files during mailing
validation files
validation files  If I place validation files in the folder where the action classes are present,will there any error arise
configuration files are used in Struts
configuration files are used in Struts  What configuration files are used in Struts
files uploding and downloading codes
files uploding and downloading codes  any one know JSP codes for upload files,download files and delete files from a created virtual memory
invoking exe files on sound
invoking exe files on sound  how to invoke .exe files on input as sound in java
download pdf files
download pdf files  pls help me,I don't know how to convert .doc,.docx files into pdf files and download that pdf files using servlet or jsp
php merge files
php merge files  Split and merge files in PHP Classes
replacing regex in large files
replacing regex in large files  replacing regex in large files
get files list - Ajax
get files list  Please,friend how to get files list with directories from ftp server or local files on web browser. Thanks, Tin Linn Soe   Hi friend, Index of Files Index
Retrieving list of uploaded files.
Retrieving list of uploaded files.  I need to retrieve list of files... files on a website. its uploaded. now the file resides in a directory in the server. now the list of uploaded files should be showing in page where other users can
files - Java Beginners
files  Why do we require files to store data?  Hi friend, files to store data, easy to find and access them. Thanks
library files - Development process
library files   Hi members, i am trying to develop a media player in java using netbeans but what library files should i import for this development
merge two war files
merge two war files  How to merge two war files using maven??? is there any command
merge two war files
merge two war files  how to merge two war files using maven
php show list of files
php show list of files  How can i pull the limited number of files in PHP
intersection of two files
intersection of two files  hi!could you please provide me with the java program for displaying intersection of two files
Structs jar files
Structs jar files  please Post required jar files for structs. Thanks, L.Mahesh Kumar
compiling .class files
compiling .class files  how do i compile .class files using eclipse
Resource files in iphone
Resource files in iphone  hiii how can we use resource files in iphone development ???   hello, if you want to use any file in your application the first you have to added it in your resource folder .. right click
List files recursively in linux
List files recursively in linux  Hi, How to List files recursively in linux? I want to list all the .php file. Thanks   Hi, You can use following command: find . -name *.php -print Thanks
getting files from VSS
getting files from VSS  I am not able to get the files from VSS.I am using following code. and just let me know is localpath attribute is for our local project path? I am getting the problem in this line failed
Mutliple files upload
Mutliple files upload  Hi Sir, Am doing a project in Jsp and Servlets, i want to upload multiple files inside the grid of the table, and submit the form.Send em the answers as soon as possible..   Please visit
encryption and decryption of files
encryption and decryption of files   Please can any one provide me with the code for encryption and decryption of files using RSA algorithm. I want to use a browse option to search for the file which is needed for encryption
Reading and writting multiple files
Reading and writting multiple files  how can i read and write say two different files at the same time using threads
Reading multiple xml files
Reading multiple xml files  How can we read many xml files from a folder? The same procedure as that of "listfiles
Renaming multiple files
Renaming multiple files  Hi, I have number of files in a directory. Files are named as: 1DeoyaRohit0705$.mp4,2DeoyaRohit0705$.mp4, 3_DeoyaRohit0705$.mp4. .... Now I wanted to rename these files as: 1RohitBBorse.mp4
Tomcat access to network files
Tomcat access to network files  i have web application which is running on tomcat 6.0, it access network files from JSP. but i am getting an errror. please help
Compare 2 files
Compare 2 files  I would like to compare 2 files in Java. please send me the code snipt for this scenario: File1... to compare these files and print the diffrence by comparing the report name
Reading files in Java
Reading files in Java  I have to make a program in my project of finance that reads the text file and process the data in it. How to read the text... suggest for Reading files in Java? Thanks
configuration files - Java Beginners
configuration files  i have to use configuration file in my program "listing of number of files and their modified time of directory",but problem is that directory name should be provided through configuratin file not input
Developer Files - Development process
Developer Files  Hi, I dont have real time experience. can u tell as a j2ee developer should i edit struts properties files or not . like wise changes in any other properties file. Thanks Prakash
quqtion on jar files
quqtion on jar files  give me realtime examples on .jar class files   A Jar file combines several classes into a single archive file. Basically,library classes are stored in the jar file. For more information,please go
Copy Files - Java Beginners
Copy Files  I saw the post on copying multiple files ( and I have something... a list of JPEG files that my boss gave me, and I was planning on putting them
listing files - Java Beginners
listing files  how to list the files and number of files only...]); System.exit(0); default : System.out.println("Multiple files...]); System.exit(0); default : System.out.println("Multiple files are not allow
regarding header files - Spring
*; As per tutorail i have copied spring.jar and commaon-logging .jar files in cureent
Creating files of PDF's thumnails
Creating files of PDF's thumnails  Hi, How to create PDF's thumbnail in PHP? Thanks   Hi, You should have ImageMagick support on your server. You can then use following code to create PDF thumbnail image. <?php
Upload and Download multiple files
Upload and Download multiple files  Hello Sir/Madam, I need a simple code for upload and download multiple files(it may be image,doc and txt)which has to be store in database and also retrieve the file from database
Displaying files on selection of date.
Displaying files on selection of date.  Hi, I am developing a GUI... show the particular txt files of the selected date. I want the java logic... in the database for files. import java.awt.*; import java.sql.*; import java.util.
storing data into flat files
storing data into flat files  how can i retrive data from database and store data in flat files   Hi Friend, Try the following code: import*; import java.sql.*; import java.util.*; class StoreDataIntoFile
java files - Java Beginners
java files  Hi! How to create files (not temporary) when i got exception in my java program. I want to write the complete exception in file... Thanks in advance...   Hi Friend, Try the following code: import
code for serching files and folders
code for serching files and folders  i want to create a code in java in which i can search any file or folder present on my local machine,with appropriate file path and location of that file or folder....if there are more than