Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6224123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:36:59+00:00 2026-05-24T08:36:59+00:00

I’ve read a lot of stuff both on stackoverflow and around the net, I’ve

  • 0

I’ve read a lot of stuff both on stackoverflow and around the net, I’ve also tried a dozen variations in my code & am yet to find a solution.
The method has to write out a text file according to these file specs I was given, so that another application on a windows based system can import the file. Have discovered a test for this is that if the line breaks look correct in notepad then the import succeeds.

After the file is written to the server, it is then read again passed into a byte array that is later on attached to an email etc. (not sure it this would be relevant)

I started with the higher level classes I usually use to write out files, and as I read more on the subject, tried using FileOutputStream so I could specify the CRLF bytes explicitly. (Still doesn’t align correctly in notepad which is the test I’m using)

byte[] CRLF = { 0x0D, 0x0D, 0x0A };//"If you write \r\n on Windows, you'll actually get 0x0D 0x0D 0x0A in the file"
     Map<String, ByteBuffer> fileMap = new HashMap<String, ByteBuffer>();
     //eg: BA20110627-2.TXT
     Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
     DateFormat df = new SimpleDateFormat("yyyyMMdd");
     String date = df.format(nowTimestamp);
     int count = 1;
     for(Map<String, List<String>> fileLineListsAndTotAmountMap : fileLineListsAndTotAmountMaps){
         String fileName = "BA"+date+"-"+count+".TXT";
         FileOutputStream out = new FileOutputStream(fileStorePath+fileName);
         String header = ""+new Header(date).toString();
         byte[] headerAsBytes = header.getBytes();
         out.write(headerAsBytes);
         out.write(CRLF);
         //each map only has one entry
         Map.Entry<String, List<String>> entry = (Entry<String, List<String>>) fileLineListsAndTotAmountMap.entrySet().toArray()[0];
         for(String s: entry.getValue()){
                if (!s.equals("")) {
                    String line = s;
                    byte[] lineAsBytes = line.getBytes();
                    out.write(lineAsBytes);
                    out.write(CRLF);

                }
          }
         //totNumOfDebitRecords, totAmountForDebitRecords
         String footer = ""+new Footer(new Integer(fileLineListsAndTotAmountMap.size()).toString(), entry.getKey());
         byte[] footerAsBytes = footer.getBytes();
         out.write(footerAsBytes);
         out.write(CRLF);
         out.flush();
         out.close();
         fileMap.put(fileName, ByteBuffer.wrap(getBytesFromFile(new File(fileStorePath+fileName))));
      }

The only other thing I can think of trying is using maybe java.nio.charset.CharsetEncoder & trying to encode it as ASCII though I can’t really see how that would help.

Can anyone shed a little more light on this sort of issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:37:00+00:00Added an answer on May 24, 2026 at 8:37 am

    I haven’t read the rest of your code yet, but this is already inaccurate:

    //"If you write \r\n on Windows, you'll actually get 0x0D 0x0D 0x0A in the file"
    byte[] CRLF = { 0x0D, 0x0D, 0x0A };
    

    CRLF is just 0x0d, 0x0a…

    Additional problems:

    • You’re using String.getBytes() without specifying an encoding: almost always a bad idea
    • You’re not using finally blocks for your streams
    • You’re using ""+x to convert a value to a string – just call toString (in the case of header you’ve already got a string); ""+x will work, but it’s ugly code – it talks about concatenation, which isn’t what you’re trying to achieve.
    • You should use a Writer (e.g. OutputStreamWriter) to write text data – that’s what it’s there for. Then you don’t need to worry about the bytes, you can just work in terms of strings. I would strongly suggest OutputStreamWriter instead of FileWriter so that you can specify the encoding.

    Once you’ve fixed all that, your code will be in a better position to fix the specific problem of line breaks… although I’d strongly recommend that if you’re still having problems, you ask a question with a short but complete program which just prints a few lines of text. The header, footer etc are irrelevant to the business of “writing a line separator”.

    You may well find that the IO-related classes in Guava make all of this easier, too…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am writing an app with both english and french support. The app requests
I have this code to decode numeric html entities to the UTF8 equivalent character.
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.