HOME      BLOG       FORUMS      CONTACT       ABOUT

Reassigns standard input stream (System.in) from a file

April 22nd, 2008 Albin Joseph Posted in java.lang No Comments »

Reassigns standard input stream (System.in) from a file

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class SystemExample {
	public static void main(String[] args) throws IOException {
		InputStream in = new FileInputStream(new File("c:\\temp\\out.log"));

		System.setIn(in);

		byte b[] = new byte[System.in.available()];
		System.in.read(b);

		System.out.println(new String(b));
	}
}
AddThis Social Bookmark Button

Redirect standard output stream (System.out) to a file

April 22nd, 2008 Albin Joseph Posted in java.lang No Comments »

We can redirect the standard output stream System.out to a file either programatically or from the console.

programatically redirecting System.out

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class SystemExample {
	public static void main(String[] args) throws FileNotFoundException {
		PrintStream p = new PrintStream(new FileOutputStream(new File("c:\\temp\\out.log")));
		System.setOut(p);

		System.out.println("Hello World");
	}
}

or

Use the redirection operator >> from the console when executing the java program. For eg:

java SystemExample >> out.log
AddThis Social Bookmark Button

Reverses the contents of a StringBuffer

April 20th, 2008 Albin Joseph Posted in java.lang No Comments »

Reverses the contents of a StringBuffer

public class StringBufferExample {
	public static void main(String[] args) {
		StringBuffer buff = new StringBuffer("Hello World");
		buff.reverse();

		System.out.println(buff);
	}
}
AddThis Social Bookmark Button

Finds out the position of last occurrence of String in a StringBuffer from the specified index

April 20th, 2008 Albin Joseph Posted in java.lang No Comments »

Finds out the position of last occurrence of String in a StringBuffer from the specified index

public class StringBufferExample {
	public static void main(String[] args) {
		StringBuffer buff = new StringBuffer("Hello World");
		int value = buff.lastIndexOf("Hello" , 5);

		System.out.println(value);
	}
}
AddThis Social Bookmark Button

Finds out the position of last occurrence of String in a StringBuffer

April 20th, 2008 Albin Joseph Posted in java.lang No Comments »

Finds out the position of last occurrence of String in a StringBuffer

public class StringBufferExample {
	public static void main(String[] args) {
		StringBuffer buff = new StringBuffer("Hello World");
		int value = buff.lastIndexOf("Hello");

		System.out.println(value);
	}
}
AddThis Social Bookmark Button

Finds out the position of occurrence of String in a StringBuffer from the specified index

April 20th, 2008 Albin Joseph Posted in java.lang No Comments »

Finds out the position of occurrence of String in a StringBuffer from the specified index

public class StringBufferExample {
	public static void main(String[] args) {
		StringBuffer buff = new StringBuffer("Hello World");
		int value = buff.indexOf("World", 1);

		System.out.println(value);
	}
}
AddThis Social Bookmark Button

Finds out the position of occurrence of String in a StringBuffer

April 20th, 2008 Albin Joseph Posted in java.lang No Comments »

Finds out the position of occurrence of String in a StringBuffer

public class StringBufferExample {
	public static void main(String[] args) {
		StringBuffer buff = new StringBuffer("Hello World");
		int value = buff.indexOf("World");

		System.out.println(value);
	}
}
AddThis Social Bookmark Button

Inserts a String to a StringBuffer at the specified position

April 20th, 2008 Albin Joseph Posted in java.lang No Comments »

Inserts a String to a StringBuffer at the specified position

public class StringBufferExample {
	public static void main(String[] args) {
		StringBuffer buff = new StringBuffer("Hello World");
		buff.insert(6, "Hey");

		System.out.println(buff);
	}
}
AddThis Social Bookmark Button

Finds out the substring of a StringBuffer

April 20th, 2008 Albin Joseph Posted in java.lang No Comments »

Finds out the substring of a StringBuffer

public class StringBufferExample {
	public static void main(String[] args) {
		StringBuffer buff = new StringBuffer("Hello World");
		String value  = buff.substring(0, 5);

		System.out.println(value);
	}
}
AddThis Social Bookmark Button

Finds the substring of a StringBuffer starting from specified index.

April 20th, 2008 Albin Joseph Posted in java.lang No Comments »

Finds the substring of a StringBuffer starting from specified index.

public class StringBufferExample {
	public static void main(String[] args) {
		StringBuffer buff = new StringBuffer("Hello World");
		String value  = buff.substring(6);

		System.out.println(value);
	}
}
AddThis Social Bookmark Button