New Member FAQ | Forums | Earn Revenue


Resources Entrance Ask Experts Exam Papers Jobs English Projects Universities Colleges Courses Schools Training My India



My Profile
Active Members
TodayLast 7 Days more...



Awards & Gifts
Online Exams

Fresher Jobs


Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian cities including Bangalore, Chennai, Hyderabad, Pune or Kochi

Resources


Find educational articles, blogs, discussion threads and other resources.

Colleges


Find details about any college in India or search for courses.

website counter



Writing into file - Java


Posted Date:     Total Responses: 0    Posted By: Annapurna   Member Level: Gold   Points/Cash: 3   


import java.io.*;
public class WriteIntoFile
{
public static void main(String[] args)
{
FileOutputStream fos = null;
File file = new File ("File1.txt");
try
{
if (! file.exists() )
{
file.createNewFile();
System.out.println("New File is Created. ");
}
else
System.out.println("File already exists.");
fos = new FileOutputStream(file, true);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the text you want to enter into file");
System.out.println("Enter @ at the end");
String str = null;

while(true)
{
str = br.readLine();
String s = " ";
str = str + s;
if((str.charAt(str.length() - 2)) == '@')
{
str = str.substring(0, str.length() - 2);
// System.out.println(str);
fos.write(str.getBytes());
System.out.println("Successfully added.");
break;
}
fos.write(str.getBytes());
}

fos.close();

FileReader fr = new FileReader(file);

LineNumberReader ln = new LineNumberReader(fr);

int count = 0;

while (ln.readLine() != null){

count++;

}
System.out.println("No. of lines are : " +count);
System.out.println("Absolute path of the file is : " +file.getAbsolutePath());

}
catch(FileNotFoundException f)
{
System.out.println(f);
}
catch(IOException ie)
{
System.out.println(ie);
}
finally
{
System.out.println("The Size of the file is : " +file.length());
}
}
}


Project Feedbacks


No feedbacks found. Be the first to respond and make money from revenue sharing program.

Post Feedback
You must Sign In to post a feedback.
Next Project: Retrieving employee details by giving employee id - Java
Previous Project: Example program for Null Pointer Exception - Java

Return to Project Index

Post New Project


Related Projects



Advertise Here





Contact Us   Advertise   Editors    Privacy Policy    Terms Of Use   

ISC Technologies.
2006 - 2009 All Rights Reserved.