DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2

Hybrid View

  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..
    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

    solution

    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, 02:46 PM
  2. Replies: 14
    Last Post: 11-29-2005, 06:56 AM
  3. Reading and Writing to a file
    By Fergy25 in forum Java
    Replies: 1
    Last Post: 11-14-2005, 04:28 PM
  4. Replies: 0
    Last Post: 04-22-2002, 06: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, 08:10 PM

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