Archive for December, 2008

Test Whether a String Can Be Converted to a Double in Java

This is a bit of a hack, but I don’t know of any way to do it more elegantly. You kind of wish the Java developers had gone one more step and included this in their framework, but anyway….
The same type of idea should work for Integers, Longs, Floats, etc.

public static boolean IsDouble(String value)
{
[...] Read more »

How to Delete Files in Directory Using Java

Let’s say you have a folder with files that you want to delete. The following code illustrates how to do this. You could also combine it with the code in this post to recursively delete files in subdirectories as well.

import java.io.*;
 

 
public static void DeleteFilesInDirectory(String dir)
{
String[] fileNames = new File(dir).list();
 
[...] Read more »

Search for Files Recursively in Java

In this post, I explained how to find files using wildcard patterns. This current post builds on the code from that post and allows you to do a wildcard search recursively (in all subdirectories) rather than just in a single directory. Notice that this method calls itself (which is what recursiveness is all about).

import java.util.*;
import [...] Read more »

How to Append Text to a File in Java

In a previous post, I wrote about writing text to a new file (or overwriting an existing one). The following code is similar except this time it appends text to the end of an existing one. The path will use the current path where Java is executing unless you specify a directory in filePath (e.g. [...] Read more »

How to Write Text to a File in Java

It is pretty straightforward to write text to a file. This creates a new file (or overwrites an existing one) and writes the text to it that you pass it. The path will use the current path where Java is executing unless you specify a directory in filePath (e.g. C:\\Temp\\JavaTest.txt).

import java.io.*;
 

 
public static void WriteTextToFile(String filePath, [...] Read more »

When Windows XP Takes a Long Time to go into StandBy Mode

Well, if you click on Start and Turn Off Computer, it will bring up a screen asking whether you want to go into StandBy, Restart, or Turn Off. Sometimes I have noticed that it starts to take a really long time for this screen to come up. I have noticed that this usually happens when [...] Read more »

Force Collections to Accept Only One Data Type in Java

Collections, such as ArrayList and Set, in Java used to be able to contain any class. So you could implement the following code without a problem.

ArrayList oldWay = new ArrayList();
oldWay.Add("xyz");
oldWay.Add(456);
oldWay.Add(new CustomWhatever());

But sometimes this flexibility caused problems because you might want to limit what could be contained in that list (for example, limit it to String [...] Read more »

Read from Multiple Text Files in Java

This post explains a way to read from multiple text files. It builds on this post that explains how to iterate over large files line by line without reading them into memory. It also builds on this post about using wildcards to search for files in a directory.
It also allows you to specify whether the [...] Read more »

Convert an int to a long and Vice Versa in Java

OK, first is to discuss the difference between an int and a long. A long is basically a longer version of an int. Or in other words, it can be bigger or smaller than int and consumes more memory. The following code shows how you can find the maximum values for these. The long class [...] Read more »

How to Modify PYTHONPATH on Linux

When you are installing modules in Python that need to be available to Python scripts stored in various locations on the server, you can use PYTHONPATH to specify where those resources are on the server. This environment variable tells Python where to find those modules/libraries.
This post explains how to set environment variables in Linux. You [...] Read more »

How to Use the vi Text Editor in Linux

In Linux, a common way to edit text files is to use a utility called vi. I haven’t used it a lot, but from what people tell me, it takes awhile to get used to (you do everything with keystrokes rather than a mouse), but once you figure it out it is very powerful.
To edit [...] Read more »

A Simple Way to Print a Large Image on Multiple Sheets in Windows

Let’s say you have a large image that you want to print on a bunch of regular sheets of paper and then tape them together. This is an inexpensive way to create a poster (as opposed to having it done professionally).
You can go to Web sites that will break it up for you. But the [...] Read more »

Setting Environment Variables in Linux

I have an application that relies on environment variables to find shared object files. If these are not in place, I get an error like “lib.so: cannot open shared object file: No such file or directory.”
If I set these variables the following way, it works temporarily:

export VARIABLE_NAME=/path/abc

But I want it to persist for all sessions [...] Read more »

Force Collections to Accept Only One Class in C#

Collections, such as List and ArrayList, in C# used to be able to contain any class. So you could implement the following code without a problem.

ArrayList oldWay = new ArrayList();
oldWay.Add("abc");
oldWay.Add(123);
oldWay.Add(new CustomWhatever());

But sometimes this flexibility caused problems because you might want to limit what could be contained in that list (for example, limit it to string [...] Read more »

Example of Implementing IEnumerable and IEnumerator in C#

In the .NET Framework, there are two interfaces designed to allow you to iterate easily over collections of objects as you would typically do in a for loop. Many classes in the .NET Framework have implemented these interfaces and do their work behind the scenes so you don’t have to worry about how it is [...] Read more »

How to Compare Two Objects in Java (by Overriding equals)

Here’s a simple class you might use in a Java system:

public class Person
{
public String Name;
public String SocialSecurityNumber;
public String PhoneNumber;
 
public Person(String name, String socialSecurityNumber, String phoneNumber)
{
Name = name;
[...] Read more »

Compare Class Types of Two Objects in Java

If you have two objects and want to determine whether they are instances of the same class, this is easy to do in Java.
Cow c = new Cow();
Print(c instanceof Cow); //true
Print(c instanceof Horse); //false
This also works when you have a class hierarchy (inheritance). The instanceof operator will return true if the object can be “upcast” [...] Read more »

How to Generate a Sequence of Numbers in Python

Let’s say you want to generate a sequence of numbers from 1 to 10 in Python. Here’s how:

x = range(1, 11) // it goes up to one less than the second parameter

Let’s say you want to generate a sequence from 0 to 10:

x = range(11) // default is to start at 0 Read more »

Our Experience with Cricket Wireless Broadband

I live in a housing development in which we currently only have one option for Internet service–DSL. We wanted to find a way to get out of this monopoly situation, so when I saw an ad for Cricket Wireless Broadband for $40 per month it caught my interest. So I went to a Cricket store [...] Read more »

Copy Files From One Linux Server to Another

Let’s say you have a file on your Linux server and want to copy it to another server. Or vice versa. You can also copy files from one remote server to another. An easy way to do this is with the scp tool, which is built on top of ssh.
Open a command line. The command [...] Read more »