DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    chris Guest

    Opening/Closing text files


    I am new to java and I am not quite sure how to write/read from a text file.
    Could anyone help me with this and provide and example?

    Thanks,
    Chris

  2. #2
    Ruchi Dhar Guest

    Re: Opening/Closing text files


    Java has excellent io package which provides all useful method to do I/O operations.
    Here is a quick file copy code. This code reads contents from one file and
    writes to another file.

    import java.io.*;

    public class CopyFile
    {

    public static void main(String args[]) throws IOException
    {
    if(args.length != 2)
    {
    System.out.println("Usage java CopyFile <srcFile> <destFile>\n");
    System.exit(0);
    }

    final File srcFile = new File(args[0]);
    if(!srcFile.exists())
    {
    System.out.println(srcFile.getAbsolutePath() + ": Is not existing");
    System.exit(0);
    }

    BufferedReader in = new BufferedReader(new FileReader(srcFile), 4096);
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(args[1]),
    4096), true);
    String line = "";
    while((line = in.readLine() != null)
    out.println(line);
    out.close();
    in.close();
    }
    }


    "chris" <christopher_ack@yahoo.com> wrote:
    >
    >I am new to java and I am not quite sure how to write/read from a text file.
    > Could anyone help me with this and provide and example?
    >
    >Thanks,
    >Chris



  3. #3
    Gene Guest

    Re: Opening/Closing text files


    note: not to state the obvious, but file I-O is only possible in a Java application.
    Not possible in an applet or recommended in an EJB. Otherwise the response
    is correct and accurate.



    "Ruchi Dhar" <rdhar@verticalnet.com> wrote:
    >
    >Java has excellent io package which provides all useful method to do I/O

    operations.
    >Here is a quick file copy code. This code reads contents from one file

    and
    >writes to another file.
    >
    >import java.io.*;
    >
    >public class CopyFile
    >{
    >
    > public static void main(String args[]) throws IOException
    > {
    > if(args.length != 2)
    > {
    > System.out.println("Usage java CopyFile <srcFile> <destFile>\n");
    > System.exit(0);
    > }
    >
    > final File srcFile = new File(args[0]);
    > if(!srcFile.exists())
    > {
    > System.out.println(srcFile.getAbsolutePath() + ": Is not existing");
    > System.exit(0);
    > }
    >
    > BufferedReader in = new BufferedReader(new FileReader(srcFile), 4096);
    > PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(args[1]),
    >4096), true);
    > String line = "";
    > while((line = in.readLine() != null)
    > out.println(line);
    > out.close();
    > in.close();
    > }
    >}
    >
    >
    >"chris" <christopher_ack@yahoo.com> wrote:
    >>
    >>I am new to java and I am not quite sure how to write/read from a text

    file.
    >> Could anyone help me with this and provide and example?
    >>
    >>Thanks,
    >>Chris

    >



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links