binary

How to read and write binary files in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.3, “How to read and write binary files in Scala.”

Problem

You want to read data from a binary file or write data to a binary file.

Solution

Scala doesn’t offer any special conveniences for reading or writing binary files, so use the Java FileInputStream and FileOutputStream classes.

Java “file write” (or “file save”) methods

Java write/save FAQ: Can you share an example of how to write to a file in Java?

Sure. Here are two "Java file save" or "Java file write" examples, taken from my Java file utilities article (Java file utilities to write, save, open, read, and copy files). I'm not going to explain these too much, but if you need a simple method to write to a file in your Java programs, I hope these methods will help you.

A Ruby script to remove binary (garbage) characters from a text file

Problem: You have a file that should be a plain text file, but for some reason it has a bunch of non-printable binary characters (also known as garbage characters) in it, and you'd like a Ruby script that can create a clean version of the file.

Solution: I've demonstrated how to do this in another blog post by using the Unix tr command, but in case you'd like a Ruby script to clean up a file like this, I thought I'd write up a quick program and share it here.

Shell script error - bad interpreter - No such file or directory

Some times when you take a file from a DOS/Windows system and move it to a Linux or Unix system you'll have problems with the dreaded ^M character. This happened recently when I moved an Ant script from a Windows system to my Mac OS X system. When I tried to run the shell script under the Mac Terminal I got this "bad interpreter" error message:

A sed command to display non-visible characters in a text file

I just ran into a need to see what non-printable (non-visible?) characters were embedded in a text file in a Unix system, when I remembered this old sed command:

sed -n 'l' myfile.txt

Note that the character in that sed command is a lower-case letter "L", and not the number one ("1").

This command shows the contents of your file, and displays some of the nonprintable characters with the octal values. On some systems tab characters may also be shown as ">" characters.

cvs binary file example - fix a binary file with cvs admin

Several times when working with CVS I've forgotten to check a binary file into CVS with the binary file flag. Fortunately this problem can be fixed using the cvs admin command. It's just important to remember that fixing this problem is a two-step operation.