iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 9, 2008
Form Field Hints
EarthWeb.com
 
May 8, 2008
Meet The Hardy Heron: What's New in Ubuntu 8.04
OReilly Network
 
May 8, 2008
Does Enterprise Development Have to Be Painful? (Part Two)
OReilly Network
 
May 8, 2008
Perl Pragma Primer
WebReference.com
 
May 8, 2008
1 comment
.net
 
May 7, 2008
Poll: Do you check the download speed of the pages you build?
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /Java

How to use File Streams 

  Views:    6648
  Votes:    2
by Brendan Murtagh 12/15/03 Rating: 

Synopsis:

A basic overview of how to use File Streams in Java to copy data from one file into another.
Pages: 
The Article

File streams are perhaps the easiest streams to understand. The file streams-- File Reader, File Writer, FileInputStream , and FileOutputStream each read or write from a file on the native file system. You can create a file stream from a file name in the form of a string, a File object, or a FileDescriptor object.

The following program uses FileReader and FileWriter to copy the contents of a file named text0r.txt into a file called out0r.txt:

import java.io.*;

 

public class FileCopy {

    public static void main(String[] args) throws IOException {

            File inputFile = new File("text0r.txt");

            File outputFile = new File("out0r.txt");

 

        FileReader in = new FileReader(inputFile);

        FileWriter out = new FileWriter(outputFile);

        int c;

 

        while ((c = in.read()) != -1)

           out.write(c);

 

        in.close();

        out.close();

    }

}

This program is very simple. It opens a FileReader on text0r.txt and opens a FileWriter on out0r.txt. The program reads characters from the reader as long as there's more input in the input file and writes those characters to the writer. When the input runs out, the program closes both the reader and the writer.

Here is the code that the Copy program uses to create a file reader:

File inputFile = new File("text0r.txt");

FileReader in = new FileReader(inputFile);

This code creates a File object that represents the named file on the native file system. File is a utility class provided by java.io. The Copy program uses this object only to construct a file reader on a file. However, the program could use inputFile to get information, such as its full path name, about the file.

After you've run the program, you should find an exact copy of text0r.txt in a file named out0r.txt in the same directory. Here is the content of the file:

this is a testing tool

that will be outputted to

the file specified in the

excellent coding in this

tutorial. :)

FileReader and FileWriter read and write 16-bit characters. However, most native file systems are based on 8-bit bytes. These streams encode the characters as they operate according to the default character-encoding scheme. You can find out the default character-encoding by using System.getProperty("file.encoding"). To specify an encoding other than the default, you should construct an OutputStreamWriter on a FileOutputStream and specify the encoding.

Pages: 

Similar/related articles:


 
  Sponsors