Solved

How to write to a file with fixed line size ?

Posted on 2006-06-28
19
217 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 70
Eclipse Neon and jdk 1.8.0 11 138
Running Jira on Raspberry PI 2? 3 261
HSSFWorkbook cannot be resolved error 10 50
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

920 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

12 Experts available now in Live!

Get 1:1 Help Now