Pages

Friday, August 27, 2010

Write text file in Java

Hi!

Here is an example code to create a text file an put some contents in it. This program will create a file called write.txt.


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.File;
import java.io.Writer;
import java.io.FileNotFoundException;
import java.io.IOException;

public class WriteTextFileExample
{
    public static void main(String[] args)
    {
        Writer writer = null;

        try
        {
            String text = "This is a text file";

            File file = new File("write.txt");
            writer = new BufferedWriter(new FileWriter(file));
            writer.write(text);
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                if (writer != null)
                {
                    writer.close();
                }
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
}

Wednesday, August 25, 2010

Hex viewer in console

Hi!

If you want to view the hex content of a file under Mac OS X or Linux system, you can type in a console
hexdump -C file.xml

 It will show the hex content with the corresponding characters like this

00000000  3c 3f 78 6d 6c 20 76 65  72 73 69 6f 6e 3d 22 31  |00000010  2e 30 22 20 65 6e 63 6f  64 69 6e 67 3d 22 55 54  |.0" encoding="UT|

Monday, August 23, 2010

Ruby get time in milliseconds

Hi

If you want to get the current time in milliseconds in Ruby, type this following Ruby code.

Thanks to Antoine that show me the mistake ;-)

Time.now.to_f*1000

Friday, August 20, 2010

Set Subversion variables

Hi,

If you want to substitute variable with Subversion data, you must create a file with the Subversion's variable and lauch a command to told to Subversion to replace them.

Possible variable are:

Date

This keyword describes the last time the file was known to have been changed in the repository, and is of the form $Date: 2006-07-22 21:42:37 -0700 (Sat, 22 Jul 2006) $. It may also be specified as LastChangedDate.
Revision
This keyword describes the last known revision in which this file changed in the repository, and looks something like $Revision: 144 $. It may also be specified as LastChangedRevision or Rev.
Author
This keyword describes the last known user to change this file in the repository, and looks something like $Author: harry $. It may also be specified as LastChangedBy.
HeadURL
This keyword describes the full URL to the latest version of the file in the repository, and looks something like $HeadURL: http://svn.collab.net/repos/trunk/README $. It may be abbreviated as URL.
Id
This keyword is a compressed combination of the other keywords. Its substitution looks something like $Id: calc.c 148 2006-07-28 21:30:43Z sally $, and is interpreted to mean that the file calc.c was last changed in revision 148 on the evening of July 28, 2006 by the user sally.

You can create a version.properties file and set this content:
$HeadURL$
$Author$
$Date$
$Revision$
After that launch this command in terminal:
 svn propset svn:keywords "Date HeadURL Author Revision" version.properties

When you will check out the project, Subversion will replace data...

Tuesday, August 10, 2010

Changing user and group ID

Hi!

If you need to change the user and the group id of an account under Mac OS X, you have to follow theses steps. We suppose that the actual id for the user and the group is 666 and we want to change them to 999.
  1. Logged as MY_USER type the command id
  2. The uid = User ID and gid is the Group ID. Note thems...
  3. Log with another account 
  4. sudo dscl . -change /Users/MY_USER UniqueID 666 999
  5. sudo dscl . -change /Users/MY_USER PrimaryGroupID 666 999
  6. sudo chown -R 999:999 /Users/MY_USER
  7. Open a session with MY_USER
Nota, if you want to connect to a NFS server, you need the same ids to be able to access the shared folders...

Tuesday, August 3, 2010

Ruby -=- undefined method `[]' for #Enumerable::Enumerator

Hi!

When I upgrade to Ruby 1.8.7 I've got some problems. I'm getting the error message undefined method `[]' for #Enumerable::Enumerator.

The problem occurs with the version 1.8.7 of Ruby and it's supposed to be corrected in the version 9. To fix this problem you can either downgrade to Ruby 1.8.6 OR put this piece of code in your environnement.rb within initializer block:

unless '1.9'.respond_to?(:force_encoding)
  String.class_eval do
    begin
      remove_method :chars
    rescue NameError
      # OK
    end
  end
end