Solved

Formated I/O

Posted on 1998-10-19
11
152 Views
Last Modified: 2012-05-04
I need to create an output file with 4 columns. Is there a Java class that allows me to produce formated I/O ?

This is what I want my file to look like:
Block Address    Size     Status       ID
--------------------     ------    ----------       --
      0                     1024    free      
1024                        14     reserved  17
1038                        57     free
0
Comment
Question by:adubon
  • 7
  • 3
11 Comments
 

Author Comment

by:adubon
ID: 1225930
Edited text of question
0
 

Author Comment

by:adubon
ID: 1225931
Edited text of question
0
 

Author Comment

by:adubon
ID: 1225932
Edited text of question
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:adubon
ID: 1225933
Edited text of question
0
 
LVL 6

Expert Comment

by:gadio
ID: 1225934
What you need is to use the formatters. Have a look into the java.text.NumberFormat and DecimalFormat. If you need some samples I'll paste them here later.
0
 

Author Comment

by:adubon
ID: 1225935
I would really appreciate some examples.   Would NumberFormat also allow me to format the first line of strings?
0
 
LVL 8

Accepted Solution

by:
diakov earned 200 total points
ID: 1225936
You can use my two procedures for alligning strings.
With allignString(s, c, size) you get as a result a string long size chars, starting with s and filled with character c up to size, if necessary. If s is longer than size it is cut.
With fillString(c, size) you get as a result a string filled with characters c and length size.
The showResultset() is an example how to use it.

      protected String allignString(String s, char c, int size)
      {
            String res = "";
            int len = s.length();
            if (len >= size)
            {
                  res = s.substring(0, size);
            }
            else
            {
                  String ccc = fillString(c, size - len);
                  res = s + ccc;
            }
            return res;
      }
      protected String fillString(char c, int size)
      {
            char data[] = new char[size];
            for (int i = 0; i < size; i++)
                  data[i] = c;
            return new String(data);
      }

      protected void showResultSet(ResultSet r)
      {
            try
            {
                  ResultSetMetaData md = r.getMetaData();
                  String ctitle, cval;
                  int ctype = 0, csize, ccount;
                  boolean first = true;
                  String displayrow = "";

                  ccount = md.getColumnCount();

                  while (r.next())
                  {
                        if (first)
                        {
                              first = false;
                              displayrow = "";
                              for (int i = 1; i <= ccount; i++)
                              {
                                    csize = md.getColumnDisplaySize(i);
                                    ctitle = md.getColumnLabel(i);
                                    displayrow += allignString(ctitle, ' ', csize);
                              }
                              writeln(displayrow);
                              writeln(fillString('-', displayrow.length()));
                        }
                        displayrow = "";
                        for (int i = 1; i <= ccount; i++)
                        {
                              ctype = md.getColumnType(i);
                              csize = md.getColumnDisplaySize(i);
                              if ((ctype == Types.BINARY) || (ctype == Types.LONGVARBINARY) || (ctype == Types.VARBINARY))
                              {
                                    cval = "BIN";
                              }
                              else
                              {
                                    if (ctype == Types.OTHER)
                                    {
                                    cval = "OBJ";
                                    }
                                    else
                                    {
                                          cval = r.getString(i);
                                          if (r.wasNull())
                                                cval = "NULL";
                                    }
                              }
                              displayrow += allignString(cval, ' ', csize);
                        }
                        writeln(displayrow);
                  }
            }
            catch (SQLException e)
            {
                  while (e != null)
                  {
                        writeln("[error] " + e.getMessage());
                        e = e.getNextException();
                        sqlerrors++;
                  }
            }
      }


0
 
LVL 8

Expert Comment

by:diakov
ID: 1225937
.
0
 

Author Comment

by:adubon
ID: 1225938
I think that there is an actual Java class that will acomplish this.
0
 
LVL 8

Expert Comment

by:diakov
ID: 1225939
Well, feel free to experiment with my functions. They work pretty well for me.

Reject the answer please, to unlock the question.
0
 

Author Comment

by:adubon
ID: 1225940
Thanks for all your help.  If you ever come across the Java class please let me know.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
more than one jdk and one jre 1 41
xampp tool 12 47
Opening PDF on button click and fill new document 2 35
servlet web applications   metadata-complete="true" or false 3 36
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now