Writing in a file with Java

The task of writing a file with Java is very simplified, with input / output streams. This is a group of core classes used for I / O, and include classes serialization that allows a program to read and write to all objects of a stream. Many methods of writing to a file can be employed and which are included in the package java.io. Here is a simple example of writing to a file with Java.

Imports of all kinds necessary. We import every single class for purposes of illustration, but could import whole package with the statement “import java.io. *.”

Declaring an object FileInputStream for the entry and exit records and exemplify with the input and output file names (input.txt and output.txt, respectively).

Use the method of writing class FileOutputStream to write in the output file. Other methods are available to write a file, but writing is used here as the simplest possible example.

Notice how the program stays in a loop that reads one byte of inflow and writes the byte to the output stream until the end of the input file is reached.

See the complete code for this example:

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;

WriteBytes public school (public static void main (String [] args) throws IOException (FileInputStream in = null; FileOutputStream out = null; try (int i; = new FileInputStream ( “input.txt”); out = new FileOutputStream ( “Output.txt”);

While ((i = in.read ())! = -1) (Out.write (i);)) (if finally (! = Null) (in.close ( );) If (! = Null) (Out.close ();))))

Featured Links:

Google Payload
Proven Money Maker At $9,547 A Day.
Burn The Fat Feed The Muscle
Burn The Fat - Top Selling Fat Loss Ebook Since 2003.
Save My Marriage Today!
Have You Ever Stayed Awake at Night Stressing About Whether or Not Your Marriage Will Last ... And What You Can Possibly Do to Save It?
Ultra Hot* SpywareBot: #1 AntiSpyware
Scan your computer for hidden AdWare and Spyware, Remove them permanently.
Top Movie Downloads
Join the Internet revolution and start downloading free movies and more!
Get Google Pay-Per-Click Ads Free!
Internet Marketer Gets $87 Million in Google Pay-Per-Click Ads FREE! ... And Makes Over $314 Million as a Result! ...And Now He's Going to Give You This Same Secret for Next to Nothing!
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • StumbleUpon
  • TwitThis

Leave a comment

You must be logged in to post a comment.