Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to write to a file with fixed line size ?

Posted on 2006-06-28
19
Medium Priority
?
226 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
Independent Software Vendors: 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!

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

721 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