Archive for December, 2008
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 »
December 17th, 2008 | Posted in Java, Tip | No Comments
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 »
December 17th, 2008 | Posted in Tip | No Comments
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 »
December 16th, 2008 | Posted in Java, Tip | No Comments
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 »
December 15th, 2008 | Posted in Java | No Comments
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 »
December 15th, 2008 | Posted in Java | No Comments
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 »
December 14th, 2008 | Posted in Tip | No Comments
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 »
December 14th, 2008 | Posted in Collections, Java, Tip | No Comments
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 »
December 13th, 2008 | Posted in Java, Performance, Tip | 2 Comments
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 »
December 13th, 2008 | Posted in Java, Performance, Tip | 4 Comments
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 »
December 12th, 2008 | Posted in Linux, Python, Tip | 3 Comments
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 »
December 11th, 2008 | Posted in Linux, Tip | No Comments
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 »
December 9th, 2008 | Posted in Tip | No Comments
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 »
December 9th, 2008 | Posted in Linux, Tip | No Comments
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 »
December 8th, 2008 | Posted in C#, Collections, Tip | No Comments
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 »
December 8th, 2008 | Posted in C#, Collections, Tip | No Comments
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 »
December 6th, 2008 | Posted in Java, Tip | 5 Comments
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 »
December 6th, 2008 | Posted in Java, Tip | 2 Comments
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 »
December 5th, 2008 | Posted in Python, Tip | No Comments
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 »
December 4th, 2008 | Posted in Tip | 2 Comments
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 »
December 3rd, 2008 | Posted in Linux, Tip | No Comments