Solved

How to write to a file with fixed line size ?

Posted on 2006-06-28
19
216 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java virtual machine monitoring tools 3 39
Receive file in Servlet 1 36
strCopies  challenge 17 73
maven project error 5 21
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 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…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

758 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