read

Scala file reading performance - Line counting algorithms

Out of curiosity about Scala’s file-reading performance, I decided to write a “line count” program in Scala. One obvious approach was to count the newline characters in the file:

// took 101 secs (10M lines)
// work on one character at a time
def countLines1(source: Source): Long = {
  var newlineCount = 0L
  for {
    c <- source
    if c.toByte == NEWLINE
  } newlineCount += 1
  newlineCount
}

As the comment shows, this took 101 seconds to read a file that has 10M lines. (An Apache access log file for this website.)

How to load (open and read) an XML file in Scala

Scala FAQ: How do I load an XML file in Scala? (How do I open and read an XML file in Scala?)

I demonstrated this in my earlier Scala XML - Searching XMLNS namespaces, XPath tutorial, but you can load an XML file in Scala like this:

Scala - How to open and read files in Scala

Scala file FAQ: How do I open and read files in Scala?

When you're writing Scala scripts, you often want to read text files. Fortunately it's pretty easy to open and read from a file in Scala. You can just use an approach like this:

How to read Perl command-line arguments

Perl FAQ: How do I read command-line arguments in Perl?

If you want to handle simple Perl command line arguments, such as filenames and strings, this tutorial is for you. If you want to handle command-line options (flags) in your Perl scripts (like "-h" or "--help"), this new Perl getopts command line options/flags tutorial is what you need.

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:

iPad PDF documents - reading, transferring, and saving

iPad PDF FAQ: How do I transfer a PDF file onto my iPad, iPhone, or iPod? (Or, "How can I read a PDF on my iPhone, iPad, or iPod?)

Since the iPod, iPhone, and iPad all use the Apple iOS operating system, the process of getting a PDF file onto these devices is identical (or nearly identical) for each device. In this article I'll generally refer to the iPad, as it's actually hard to read most PDF documents on an iPhone of iPod.)

SQLite script - How to read/execute a SQLite script

SQLite script FAQ: How do I read/execute a "create tables" script from the SQLite command line? (How do I read or execute commands in a file from the sqlite3 command line?)

Many times when you're working with a database, you'll keep all your "create table" commands in a database script, which you'll then execute from your database server command line prompt. The file of database commands you execute is often referred to as a "script", or in this case, a "SQLite script".

Bash shell script - how to prompt and read user input

Unix/Linux bash shell script FAQ: How do I prompt a user for input from a shell script (Bash shell script), and then read the input the user provides?

Answer: I usually use the shell script "read" function to read input from a shell script. Here are two slightly different versions of the same shell script. This first version prompts the user for input only once, and then dies if the user doesn't give a correst Y/N answer:

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 "file open" and "file read" methods (examples)

Java file reading FAQ: Can you share some off-the-shelf Java methods I can use to open and read files in Java?

Sure. Taken directly from my Java file utilities article (Java file utilities to open, read, write, and copy files), here is the source code for two Java methods that let you read a text file.

Syndicate content