Solved

Formated I/O

Posted on 1998-10-19
11
149 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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

760 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

18 Experts available now in Live!

Get 1:1 Help Now