Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to write to a file with fixed line size ?

Posted on 2006-06-28
19
Medium Priority
?
230 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

783 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