write

I can't write a book

As shown in the image, I’ve finally come to the realization that I can’t write a book. But I can write a few sentences, and then a few pages, and eventually a chapter on a given topic. And then I can write another chapter, and then another. But to sit down and say, “I’m going to write a book,” no, that’s too hard.

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.

How to write text files in Scala

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

Problem

You want to write plain text to a file, such as a simple configuration file, text data file, or other plain-text document.

Solution

Scala doesn’t offer any special file writing capability, so fall back and use the Java PrintWriter or FileWriter approaches:

Android: Generic methods to write/save Android preferences data

I was working with Android Preferences recently, and after writing a few “save my preference” methods, I decided to take a few minutes to organize my code and put all of those Preference methods in a PreferenceUtils class. I made the methods static, and following functional programming principles, I made their output depend only on their input, so they require you to pass in a Context reference, along with your preference key and value.

How to read and write from a file in a Linux bash shell script alvin May 2, 2014 - 9:28am

I’m currently writing a complicated Linux bash shell script where I need to keep a counter in an external file, and to do so, I need to be able to write to a file and then read from that file.

In short, this is how I write my counter to that file:

# create a variable to represent the filename
COUNTER_FILE="counter.tmp"

# write to the file
echo "0" > $COUNTER_FILE

Later in the code I increment the counter and write it to the file like this:

Scala: Saving/writing XML to a file

Problem: You want to write XML data to a file in a Scala application, such as saving application data or configuration information to a file.

Solution

Use the scala.xml.XML.save method to write a Scala literal to a file. Given this XML literal:

PHP write stderr - Write to STDERR in a PHP script

PHP STDERR FAQ - How do I write to STDERR in a PHP script, specifically a PHP command line script?

I've written a lot of my command line scripts in PHP lately, and I just ran across this problem in my own script, how to write to STDERR in PHP. Fortunately the solution is easy, just use the PHP fwrite function like this:

fwrite(STDERR, “hello, world\n”);

Or, in a more real-world example, here's how I'm writing MySQL error messages to STDERR in PHP:

A Perl “write to file” example

Perl write to file FAQ: Can you demonstrate an example of how to write to a file in Perl?

Somehow I managed to go all these years without showing a simple Perl "write to file" example. Let's fix that.

Perl “write to file” example

Here's a short snippet of code that shows how to write to a file in Perl: