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

  • Home
  • SEARCH
  • 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 993453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:25:31+00:00 2026-05-16T06:25:31+00:00

I need to write raw bytes to the file. I do it with: (.write

  • 0

I need to write raw bytes to the file. I do it with:

(.write (FileOutputStream "/path") bytes)

…where bytes must be of type byte[]. Please note it cannot be Byte[].

I tried to convert my sequence with both (bytes) and/or (into-array) functions and got frustrated, one example:

user=> (bytes (into-array (filter #(not (= % 13)) (to-byte-array (File. "e:/vpn.bat")))))
java.lang.ClassCastException: [Ljava.lang.Byte; cannot be cast to [B (NO_SOURCE_FILE:0)

CONTINUED:

The into-array with Byte/TYPE works fine. However, the byte-array does not. The file gets empty:

(import 'FileOutputStream)
(use 'clojure.contrib.io)

(defn remove-cr-from-file [file]
  (with-open [out (FileOutputStream. file)]
    (let [dirty-bytes (to-byte-array file)
          clean-seq   (filter #(not (= 13 %)) dirty-bytes)
          clean-bytes (byte-array clean-seq)]
      (.write out clean-bytes))))
  • 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-16T06:25:32+00:00Added an answer on May 16, 2026 at 6:25 am

    Update: the new part of the question (“CONTINUED”) is answered towards the end.


    Just to make it clear what actually happens here:

    This question actually illustrates an interesting point: the array cast functions — bytes, ints, … — are not and cannot be used as conversion functions. They only cast to the target type, meaning in particular that the input to bytes must already be an array of appropriate type.

    This makes sense, since converting from int[] to long[] is not a simple matter of viewing the numbers in a different light — you’d also have to allocate a different amount of storage for the array — so being able to tell just by looking at the operator whether the operation in question is a conversion or a cast is a Good Thing.

    The reason why casts are useful in a dynamic language such as Clojure has to do with efficiency (you can use casting alongside type hints to speed things up) and interop (where you often need a thing of just the right type). The reason why the compiler can’t just infer the correct array type is because there is not always enough information to do so (not to mention it might not even be clear what the “correct” type might be).

    To fix the snippet in question, one could use either Byte/TYPE (as suggested by Jieren) or skip the into-array and bytes and wrap the filter in bytes-array instead (as suggested by Brenton Ashworth).


    The problem with the code newly included in the question text is that it opens a FileOutputStream on the file prior to reading its contents. The act of opening the FOS already clears the file:

    (with-open [out (FileOutputStream. some-file)]
      :foo)
    
    ; => :foo
    ; side effect: some-file is now empty
    

    You’ll have to read from the file outside the with-open:

    (let [foo (byte-array
               (filter #(not= 13 %)
                       (to-byte-array some-file)))]
      (with-open [out (FileOutputStream. some-file)]
        (.write out foo)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I read a raw byte array from any file... Dim bytes() as
I need to write data into drive. I have two options: write raw sectors.(_write(handle,
The application need write file's last modification date. void Dater(String DateFile) { File file
I need to write to a text file using JavaScript. I have a machine
I need to write the output of the code I have to a file
I'm trying to write a bitmap (.bmp) parser/reader by reading raw bytes from the
I am using C# and pinvoke to read/write a raw sd card. I need
I want to write my own DNG file importer and converter (I need to
I need to write some data from time to time to a file, appending
I need to analyse a binary data file containing raw data from a scientific

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.