Posts

Showing posts from January, 2012

Reading a file

package readfile;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author nurhak.kaya
 */
public class ReadFile {

    public static void main(String[] args) {
        File file = new File("text.txt");
        StringBuffer sb = new StringBuffer();
        BufferedReader br = null;
        String text = null;

        try {
            br = new BufferedReader(new FileReader(file));

            while ((text = br.readLine()) != null) {
                sb.append(text).append(
                        System.getProperty("line.separator"));
                System.out.println(text);

            }

        } catch (Exception e) {
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException ex) {
                    Logger.getLogger(ReadFile.class.getName()).log(
    …

Writing a text to a file

package writetofile;


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

/**
 *
 * @author nurhak.kaya
 */
public class WriteToFile {

    public static void main(String[] args) {

        File file = new File("text.txt");
        String data = "This is a sample text data!";
        Writer writer = null;

        try {
            writer = new BufferedWriter(new FileWriter(file));
            writer.write(data);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Using the StringTokenizer class

StringTokenizer class might be very usefull especially when you read a text data from a socket. Here is an example to show how we can use this class.


package tokenizer;

import java.util.StringTokenizer;

/**
 *
 * @author nurhak.kaya
 */
public class Tokenizer {

    public static void main(String[] args) {

        String text1 = "This is a sample text!";
        String text2 = "30.01.2012";

        StringTokenizer st1 = new StringTokenizer(text1);
        StringTokenizer st2 = new StringTokenizer(text2, ".");

        while (st1.hasMoreElements()) {
            String string1 = (String) st1.nextElement();
            System.out.println(" --> " + string1);

        }
        System.out.println("/////////////////////");

        while (st2.hasMoreElements()) {
            String string2 = (String) st2.nextElement();
            System.out.println(" --> " + string2);
        }
    }
}

Getting the environmental variables on Windows

Here is an example for getting the environmental variables on a Windows machine. 


package environmentalvariables;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author nurhak.kaya
 */
public class EnvironmentalVariables {

    public static void main(String[] args) {

        Map map = System.getenv();
        Set keys = map.keySet();
        Iterator iterator = keys.iterator();

        while (iterator.hasNext()) {

            String key = iterator.next().toString();
            String value = map.get(key).toString();

            System.out.println(key + " --> " + value);

        }
    }
}