Writing to a file in a specific format??


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Writing to a file in a specific format??

  1. #1
    Join Date
    Apr 2005
    Posts
    15

    Writing to a file in a specific format??

    Hi,

    I want to create a text file in a particular format using java with a unique name..
    for e.g

    Labelxxxx Labelyyyy Label zzzzz
    detail1 detail11 detail111
    detail2 detail22 detail222
    ............

    like a tabular format..

    So how i can i do that??


    Thanks,
    Jignesh

  2. #2
    Join Date
    Jan 2006
    Location
    Sydney
    Posts
    49

    Suggestion

    I would suggest that you use a csv format. i.e comman separated format, so that if you whish you can open it in a spreadsheet etc.

    Labelxxxx, Labelyyyy, Labelzzzzz
    detail1, detail11, detail111
    detail2, detail22, detail222.


    As far as writing goes:

    Code:
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.PrintWriter;
    
    public class TestWriter {   
        public static void main(String[] args) {
            
            PrintWriter pr =null;
            
            String heading = "Labelxxxx, Labelyyyy, Labelzzzzz ";
            
            String[] values = {"val1", "val2", "val3"};
            
            try 
            { 
              
               FileWriter fw = new FileWriter("c://test.txt", false);
               BufferedWriter bw = new BufferedWriter(fw);
               
               pr = new PrintWriter(bw); 
                     
               pr.println(heading);
               
               //to print values you can use for loops etc.
               for (int i = 1 ; i<=10; i++){
                   for (int j = 0; j < values.length; j++) {
                       pr.print(values[j]);
                       if(j ==  (values.length-1)) {
                           pr.println(); // print new line character
                       }
                       else {
                           pr.print(","); 
                       }
                   }
               }
                  
            } catch (java.io.FileNotFoundException ex) { 
               System.out.println("File does not exist. "); 
            } catch (java.io.IOException ex) { 
               ex.printStackTrace(); 
            } 
            
            finally{
                pr.close();
            }
    
        }
    }
    Arul

Similar Threads

  1. reading and writing a file in VB
    By Athono in forum VB Classic
    Replies: 1
    Last Post: 12-28-2005, 03:46 PM
  2. Replies: 14
    Last Post: 11-29-2005, 07:56 AM
  3. Reading and Writing to a file
    By Fergy25 in forum Java
    Replies: 1
    Last Post: 11-14-2005, 05:28 PM
  4. Replies: 0
    Last Post: 04-22-2002, 07:06 PM
  5. Formatting text and writing to it to a file
    By Matt Lake in forum VB Classic
    Replies: 0
    Last Post: 03-13-2001, 09:10 PM

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