Opening/Closing text files


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Opening/Closing text files

  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

    >



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


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center