Solved

How to write to a file with fixed line size ?

Posted on 2006-06-28
19
223 Views
Last Modified: 2010-08-05
Hi,

I am simply writing just one record, namely email address to a file.
The precondition is that I have to have the line fixed in size ( I mean the just one field whose value is sould be set to)
127 bytes + (CRLF) ?

How do set this and write to this file..
Currently I was simply writing to the file as: (I have the emails in an ArrayList - mUpdateSuccessEmailList)

 public void writeFFFile(BufferedWriter lBuffWriter) throws IOException
    {
       StringBuffer buff = new StringBuffer();
       
       Iterator iter = mUpdateSuccessEmailList.iterator();
       while(iter.hasNext())
       {
          buff.append(iter.next() + "\n");
       }
       
       lBuffWriter.write(buff.toString());
       lBuffWriter.flush();
    }

Let me know...

Thanks.
_Esam.

0
Comment
Question by:_Esam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 4
  • +1
19 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17005144
It'd probably be easier, since ascii-based to do

byte[] line = new byte[129];

and use a ByteArrayOutputStream
0
 

Author Comment

by:_Esam
ID: 17005221
Little more elaboration please..:)

_Esam...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17005288
                 final int LENGTH = 129;
                  ByteArrayOutputStream out = new ByteArrayOutputStream(129);
                  out.write("me@nowhere.com".getBytes());
                  out.write(new byte[LENGTH - out.size()]);
                  byte[] bytes = out.toByteArray();
                  bytes[LENGTH - 1] = '\n';
                  bytes[LENGTH - 2] = '\r';
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 4

Expert Comment

by:astorer
ID: 17005309
Lots of different ways to do this.  Some more efficient than others.  If your not bothered about efficiency and just want compact code, then.

private static String SPACES_127="                       ";// that's 127 spaces

 public void writeFFFile(BufferedWriter lBuffWriter) throws IOException
    {
       StringBuffer buff = new StringBuffer();
       
       Iterator iter = mUpdateSuccessEmailList.iterator();
       while(iter.hasNext())
       {
             //write first 127 characters of a string made up of the email address + 127 spaces
             lBuffWriter.write( iter.next().toString() + SPACES_127, 0, 127);
             lBuffWriter.write("\n");
       }
    }

This is not elegant or efficient but it is compact code.
Note that often when manipulating strings like the above, the JVM will actually use StringBuffers under the covers anyway, so not a big deal.

Andrew
0
 

Author Comment

by:_Esam
ID: 17005337
>>lBuffWriter.write( iter.next().toString() + SPACES_127, 0, 127);

I don't get this part?

_Esam

0
 
LVL 4

Expert Comment

by:astorer
ID: 17005340
Just to make one other point.  Usually, dealing in bytes for I/O is more efficient than Strings.  But the parameter to your method is a BufferedWriter which deals with Strings not bytes.  So little efficiency gained by using an array of bytes etc.

Keep it simple!  Strings are usually easier to understand.

Andrew


0
 
LVL 4

Expert Comment

by:astorer
ID: 17005348
>>lBuffWriter.write( iter.next().toString() + SPACES_127, 0, 127);

Breaking it down...
1. Take your email address       iter.next().toString()
2. add 127 spaces to the end      + SPACES_127
3. This gives you a string that has too may spaces on the end
4. So, takes 127 characters starting from the first one (0)  that's the   , 0, 127);  bit





0
 

Author Comment

by:_Esam
ID: 17005364
>>>Sorry CEHJ .... I am pretty unfamiliar with .......

               final int LENGTH = 129;
               ByteArrayOutputStream out = new ByteArrayOutputStream(129);
               out.write("me@nowhere.com".getBytes());
               out.write(new byte[LENGTH - out.size()]);
               byte[] bytes = out.toByteArray();
               bytes[LENGTH - 1] = '\n';
               bytes[LENGTH - 2] = '\r';

How do I write to the FF File?

_Esam.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17005374
Just write the byte array 'bytes' in my example
0
 

Author Comment

by:_Esam
ID: 17005379
>So, takes 127 characters starting from the first one (0)  that's the   , 0, 127);  bit

Hmm.. Got it..

Where is the \n and \r at the end of each line?

Thanks.
_Esam.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17005410
>>Where is the \n and \r at the end of each line?

Here:

           bytes[LENGTH - 1] = '\n';
               bytes[LENGTH - 2] = '\r';
0
 

Author Comment

by:_Esam
ID: 17005508
>ByteArrayOutputStream

How I wrap to the File ? or BufferedWriter or ?

_Esam
0
 
LVL 35

Expert Comment

by:girionis
ID: 17007624
> How I wrap to the File ? or BufferedWriter or ?

http://javaalmanac.com/egs/java.io/SerializeObj.html
0
 

Author Comment

by:_Esam
ID: 17009653
>Just write the byte array 'bytes' in my example

That's what I am asking !!!
How do I write this to a file...?

_Esam..
0
 
LVL 35

Expert Comment

by:girionis
ID: 17009761
Try this:

ObjectOutput out = new ObjectOutputStream(new FileOutputStream("myfilename"));
out.write(bytes);
out.close();

where "bytes" is the bytes array.
0
 

Author Comment

by:_Esam
ID: 17009870
>>public void writeFFFile(BufferedWriter lBuffWriter) throws IOException
    {
       StringBuffer buff = new StringBuffer();
       
       Iterator iter = mUpdateSuccessEmailList.iterator();
       while(iter.hasNext())
       {
          buff.append(iter.next() + "\n");
       }
       
       lBuffWriter.write(buff.toString());
       lBuffWriter.flush();
    }

I was passing a BufferedWriter to this method....
Due to some complicacy ... I did not pass the file itself...

What do I pass to this method now..... OutputStream ??? ObjectOutput ??


Let me know..

Thax
_Esam
0
 
LVL 4

Expert Comment

by:astorer
ID: 17011436
My initial response from Comment from astorer
Date: 06/28/2006 11:00PM BST answers the question.  That's how you could modify your function to do what you want.  That is, to write your list to a BufferedWriter.

I assumed you knew how to setup that BufferedWriter since you specified it.  

If you need help with the BufferedWriter then I suggest raising a separate question.

Good luck
Andrew

PS I'd suggest offering more than 125 points for this amount of help too :-)
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 17013714
public void writeFFFile(BufferedWriter lBuffWriter) throws IOException
{
      final int LENGTH = 129;
      ByteArrayOutputStream out = new ByteArrayOutputStream(129);
      Iterator iter = mUpdateSuccessEmailList.iterator();
      while(iter.hasNext())
      {
            out.write(((String)iter.next()).getBytes());
            byte[] spaces= new byte[LENGTH - out.size()];
            Arrays.fill(spaces, (byte)' ');
            out.write(spaces);
            byte[] bytes = out.toByteArray();
            bytes[LENGTH - 2] = '\r';
            bytes[LENGTH - 1] = '\n';
            lBuffWriter.write(new String(bytes));
            lBuffWriter.flush();
            out.reset();
      }
      out.close();      
      
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17018884
:-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…
Suggested Courses

734 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