inputstream

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.

Scala: How to use a String during testing instead of an InputStream

Imagine that you have a method that requires an InputStream, and you want to test that method. How can you test it?

In the Scala Cookbook I wrote how your method can accept a Source instead of a File to make it easier to test, so I won’t write any more about that right now.

But if you have a method that accepts an InputStream and you want to be able to test it with a String, here’s what you can do:

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.

Java file open, read, and write utilities

Java file utilities FAQ: Do you have any Java file utilities you can share?

As I was working on another Java/Swing application this weekend, I ran across my "Java file utilities" class, and I thought I'd share that class here today. It's nothing too major, but it does include Java methods that let you open, read, write, and copy files using Java.

Java URL example - Download the contents of a URL

Java URL download FAQ: How can I download the contents of a URL using Java?

Note: It's 2013 now and this code is a little old, but hopefully it will still get you pointed in the right direction.

In some Java applications you'll want to download the contents of a URL across a network. For example, I've written two applications that do this regularly.

Java Jar file: How to read a file from a Jar file

Java jar file reading FAQ: Can you show me how a Java application can read a file from own of its own Jar files?

Here's an example of some Java code I'm using to read a file (a text file) from a Java Jar file. This is useful any time you pack files and other resources into Jar files to distribute your Java application.

Java - read Jar file example #1

The source code to read a file from a Java Jar file uses the getClass and getResourceAsStream methods:

A Java HTTPS client example

Java HTTPS client FAQ: Can you share some source code for a Java HTTPS client application?

Sure, here's the source code for an example Java HTTPS client program I just used to download the contents of an HTTPS (SSL) URL. I actually found some of this in a newsgroup a while ago, but I can't find the source today to give them credit, so my apologies for that.

Java URL example - A Java class to download and parse URL contents

Java URL download FAQ: Can you share some source code for a Java URL example, specifically a Java class to download and parse the contents of a URL?

This example is a little weak, but it's a program that downloads and parses the contents of a given URL. The purpose has nothing to do with URLs ... it has a lot more to do with the parsing that I am trying to achieve. The parsing code is actually going to be used in an anti-spam program that I am working on.

Java wget: JGet, something like wget

Java FAQ: Can you share some source code for a “Java wget” program, i.e., a Java program that works like the Unix wget or curl commands?

Here's the source for a program I've named JGet, which acts similar to the wget or curl programs. I didn't have wget installed when I needed it (and my client wouldn't let me install it), so I wrote this Java wget replacement program.