Archive

Archive for the ‘Java’ Category

Renaming file or directory in Java

October 25, 2009 Leave a comment

We moved. Please visit this link for this post.

 

 

 

I am going to show how to rename file or directory in Java.

import java.io.File;

/**
 * This class shows how to rename file or directory
 * @author FaYna Soft Labs
 */
public class Main {

    public static void main(String[] args) {
        // File (or directory) with old name
        File oldFile = new File("C:/sampleDir");

        // File (or directory) with new name
        File newFile = new File("C:/sample");

        // Rename file (or directory)
        boolean success = oldFile.renameTo(newFile);
        if (!success) {
            System.err.println("Error");
        }
    }
}

You can copy this example to your IDE and use it for your objective

Advertisements
Categories: Java, Java 1.6 Tutorial

Moving file or directory to other directory in Java

October 24, 2009 Leave a comment

We moved. Please visit this link for this post.

 

 

 

In Wallpaper Changer application version 1.2.* we added feature which allows to move file in application’s file manager.

I want to show how we did it. As you see this post refer to core Java not Android. That’s why I will post code with method main.

Example is simple, just copy it and use.

import java.io.File;

/**

* This class represents how need to move file or directory to other directory

* @author FaYna Soft Labs

*/

public class Main {

public static void main(String[] args) {

// File (or directory) to be moved

File file = new File("C:/sampleDir/sample.data");

// Destination directory

File dir = new File("C:/sampleDir/otherDir");

// Move file to new directory

boolean success = file.renameTo(new File(dir, file.getName()));

if (!success) {

System.err.println("Error");

}

}

}

That’s all, you moved file/directory.

Categories: Java, Java 1.6 Tutorial

Scheduling a timer task to run repeatedly

October 18, 2009 Leave a comment

We moved. Please visit this link for this post.

 

 

 

I am going to show example of how to use TimerTask class:

package com.faynasoftlabs.tutorial.java.timertaskexample;

import java.util.Timer;

import java.util.TimerTask;

/**

* Class which shows how to use timertask class

* @author FaYna Soft Labs

*/

public class Main {

public static void main(String[] args) throws Exception {

int delay = 5000; // delay for 5 sec.

int period = 1000; // repeat every sec.

Timer timer = new Timer();

timer.scheduleAtFixedRate(new TimerTask() {

public void run() {

System.out.println("done");

}

}, delay, period);

}

}

This code will show done every second. I put 5 sec delay for first showing. It is very simple example of using TimerTask.

Categories: Java, Java 1.6 Tutorial

Using FileFilter in Java

October 15, 2009 Leave a comment

We moved. Please visit this link for this post.

 

 

 

What need to do when you want to filter files by extensions in catalog.

There are 2 ways:

implements FileFilter or FilenameFilter.

I am going to show how to do this with FileFilter. Read more…

Categories: Java, Java 1.6 Tutorial

Thinking in Java 4-th edition Book Recommendation

October 14, 2009 Leave a comment

We moved. Please visit this link for this post.

 

 

 

I have a rule. If I want to learn something new I always read article in style “Hello, World”, after that I read one book and last one I read documentation. Read more…

Sorting directories and files in Java

October 13, 2009 Leave a comment

We moved. Please visit this link for this post.

 

 

 

Several days ago we modified looks of our file manager in Wallpaper Changer application. In first version we used simple sort without Comparator or Comparable interfaces.

And finally I fixed this issue. I want to show how I did it. Read more…

Categories: Java, Java 1.6 Tutorial

Inner classes

October 13, 2009 Leave a comment

We moved. Please visit this link for this post.

 

 

 

Intro
An Inner class (aka Nested class) is a class declared entirely within the body of another class or interface.
The inner class is a valuable feature because it allows you to group classes that logically belong together and to control the visibility of one within the other. However, it’s important to understand that inner classes are distinctly different from composition. At first, inner classes look like a simple code-hiding mechanism: You place classes inside other classes. Initially, inner classes may seem odd, and it will take some time to become comfortable using them in your designs.

Read more…

Categories: Java 1.6 Tutorial