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 4098786
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:20:52+00:00 2026-05-20T20:20:52+00:00

I want to write first a sequence of strings and then a sequence of

  • 0

I want to write first a sequence of strings and then a sequence of bytes into a file, using Java. I started by using FileOutputStream because of the array of bytes. After searching the API, I realised that FileOutputStream cannot write Strings, only ints and bytes, so I switched to DataOutputStream. When I run the program, I get an exception. Why?

Here’s a portion of my code:

 try {
            // Create the file
            FileOutputStream fos;
            DataOutputStream dos; // = new DataOutputStream("compressedfile.ecs_h");
            File file= new File("C:\\MyFile.txt");
            fos = new FileOutputStream(file);
            dos=new DataOutputStream(fos);

            /* saves the characters as a dictionary into the file before the binary seq*/
            for (int i = 0; i < al.size(); i++) {
                String name= al.get(i).name; //gets the string from a global arraylist, don't pay attention to this!
                dos.writeChars(name); //saving the name in the file
            }

            System.out.println("\nIS SUCCESFULLY WRITTEN INTO FILE! ");

            dos.writeChars("><");
            String strseq;

            /*write all elements from the arraylist into a string variable*/
            strseq= seq.toString();
            System.out.println("sTringSeq: " + strseq);

            /*transpose the sequence string into a byte array*/
            byte[] data = new byte[strseq.length() / 8];

            for (int i = 0; i < data.length; i++) {
                data[i] = (byte) Integer.parseInt(strseq.substring(i * 8, (i + 1) * 8), 2);
                dos.write(data[i]);
            }


            dos.flush();
            //Close the output stream
            dos.close(); 
} catch(Exception e){}
  • 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-20T20:20:53+00:00Added an answer on May 20, 2026 at 8:20 pm

    The problem with your code is that the last for loop was counting over the wrong number of bytes. The code below fixes your problem writing your test data to a file. This works on my machine.

    public static void main(String[] args) {
    
        ArrayList<String> al = new ArrayList<String>();
        al.add("String1");
        al.add("String2");
    
        try {
            // Create the file
            FileOutputStream fos = new FileOutputStream("MyFile.txt");
            DataOutputStream dos = new DataOutputStream(fos);
    
            /* saves the characters as a dictionary into the file before the binary seq */
            for (String str : al) {
                dos.writeChars(str);
            }
    
            System.out.println("\nIS SUCCESFULLY WRITTEN INTO FILE! ");
    
            dos.writeChars("><");
            String strseq = "001100111100101000101010111010100100111000000000";
    
            // Ensure that you have a string of the correct size
            if (strseq.length() % 8 != 0) {
                throw new IllegalStateException(
                        "Input String is cannot be converted to bytes - wrong size: "
                                + strseq.length());
            }
    
            int numBytes = strseq.length() / 8;
            for (int i = 0; i < numBytes; i++) {
                int start = i * 8;
                int end = (i + 1) * 8;
                byte output = (byte) Integer.parseInt(strseq.substring(start, end), 2);
                dos.write(output);
            }
    
            dos.writeChars("> Enf of File");
            dos.flush();
            // Close the output stream
            dos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    The approach of writing bytes directly to a test file does have a few problems (I assume that it’s a text file in that your test file name ends with .txt), the most obvious one being that some text editors don’t handle/display null characters very well (your last test byte was: 00000000 or null). If you want to see the bytes as readable bytes then you could investigate encoding them using Base64 encoding.

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

Sidebar

Related Questions

I'm trying to split up a nucleotide sequence into amino acid strings using a
I want to write a function that takes an array of letters as an
I want to write a raw byte/byte stream to a position in a file.
Given this code in Java: FileOutputStream os = new FileOutputStream(/tmp/test.dat); os.write(0x14); os.write(0xfe); os.write(0xae); os.write(String.valueOf((char)
I want to write a program for finding out the sum of first and
I am trying to write my first WCF Service. Right now I just want
I want to write a method in Java that verifies that some conditions hold
I am using c# for programming! I want to write one regular expression in
I want to write a Swing application in Griffon but I am not sure
I want to write a command that specifies the word under the cursor in

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.