The Artima Developer Community
Sponsored Link

Java Answers Forum
Write to a txt

5 replies on 1 page. Most recent reply: Feb 28, 2005 3:23 PM by perry anderson

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Dimitris Paras

Posts: 12
Nickname: dimpars
Registered: Feb, 2005

Write to a txt Posted: Feb 22, 2005 5:15 AM
Reply to this message Reply
Advertisement
i 'm trying to find a way to print to a file a sequence of integers and double as illustrated below:


1 79 12 5 11 1 17
2 77 6 14 18
3 67 4 19 13 15 10 9
4 73 2 16 20
5 58 8 7 3


I am a c++ developer and i used to work with fprintf. Is there any similar method for Java?


Thank you in advance for your consideration!!!


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Write to a txt Posted: Feb 22, 2005 12:47 PM
Reply to this message Reply
This is the method Icreated for my personal use:

    /** This method creates a file and writes the lines of fileContent into it
      * @param file file path (relative or absolute)
      * @param fileContent Array containing the lines to be written into the file
      * @param doOutput Do some output in the command window
      */
    public static void saveToFile (String file, String[] fileContent, boolean doOutput) {
        saveToFile (new File (file), fileContent, doOutput);
    }
 
    /** This method creates a file and writes the lines of fileContent into it
      * @param file output file
      * @param fileContent Array containing the lines to be written into the file
      * @param doOutput Do some output in the command window
      */
    public static void saveToFile (File file, String[] fileContent, boolean doOutput) {
        try {
            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, false)));
            if (doOutput)
                System.out.print ("Writing ");
            for (int i = 0; i < fileContent.length; i++) {
                out.println(fileContent[i]);
                if (doOutput)
                    System.out.print (".");
            }
            if (doOutput)
                System.out.println (" done");
            out.close();
        } catch (IOException e) {
            System.out.println("I/O Error: " + e.getMessage());
        }
    }




The important lines are:
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, false)));
out.println(fileContent[i]);
out.close();


PrintWriter and File are in the java.io package.

Dimitris Paras

Posts: 12
Nickname: dimpars
Registered: Feb, 2005

Re: Write to a txt Posted: Feb 24, 2005 1:54 AM
Reply to this message Reply
Thank u so much!it works!!!

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Write to a txt Posted: Feb 24, 2005 10:41 AM
Reply to this message Reply
btw: If you create the filewriter like this:
new FileWriter (file, true)

you can append text to a existing file.

perry anderson

Posts: 10
Nickname: anderson
Registered: Feb, 2005

Re: Write to a txt Posted: Feb 28, 2005 3:22 PM
Reply to this message Reply
no, i'm afraid it simply cannot be done

sorry about that kid

tough

- perry

perry anderson

Posts: 10
Nickname: anderson
Registered: Feb, 2005

Re: Write to a txt Posted: Feb 28, 2005 3:23 PM
Reply to this message Reply
how did you get that lovely syntax print in your comment

thats looks really really good

thanks

- perry

Flat View: This topic has 5 replies on 1 page
Topic: NullPointerExceptions Previous Topic   Next Topic Topic: Graphics

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use