Go Back   Wiki NewForum | Latest Entertainment News > Career Forum & Tips > Tech Forum & Tutorial > Java Forum & Tutorial


Simple Program for Reading Writing Content From File


Reply
Views: 2198  
Thread Tools Rate Thread
  #1  
Old 05-25-2009, 04:46 PM
bholus7
Guest
 
Posts: n/a
Default Simple Program for Reading Writing Content From File

Simple Program for Reading Writing Content From File


Program for reading content from file:

import java.io.*;
public class MyFirstFilereadingApp
{
// Main method
public static void main (String args[])
{
// Stream to read file
FileInputStream fin;
try
{
// Open an input stream
fin = new FileInputStream ("c:\\myfile1.txt");
// Read a line of text
System.out.println( new DataInputStream(fin).readLine() );
// Close our input stream
fin.close();
}
// Catches any error conditions
catch (IOException e)
{
System.err.println ("Unable to read fr! om file");
System.exit(-1);
}
}
}

Program for writing content from file:

import java.io.*;
public class MyFirstFileWritingApp
{
// Main method
public static void main (String args[])
{
// Stream to write file
FileOutputStream fout;
try
{
// Open an output stream
fout = new FileOutputStream ("c:\\myfile1.txt");
// Print a line of text
new PrintStream(fout).println ("hello world!");
// Close our output stream
fout.close();
}
// Catches any error conditions
catch (IOException e)
{
System.err.println ("Unable to write to file");
System.exit(-1);
}
}
}

Reply With Quote
Reply

New topics in Java Forum & Tutorial





Powered by vBulletin® Version 3.8.10
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
WikiNewForum)