Java source code examples

Java source code samples. Java code examples.

Entries Tagged as 'java.lang'

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

April 22nd, 2008 · 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));
}
}

[Read more →]

Tags: java.lang

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

April 22nd, 2008 · 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 [...]

[Read more →]

Tags: java.lang

Reverses the contents of a StringBuffer

April 20th, 2008 · 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);
}
}

[Read more →]

Tags: java.lang

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

April 20th, 2008 · 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);
}
}

[Read more →]

Tags: java.lang

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

April 20th, 2008 · 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);
}
}

[Read more →]

Tags: java.lang

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

April 20th, 2008 · 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);
}
}

[Read more →]

Tags: java.lang

Finds out the position of occurrence of String in a StringBuffer

April 20th, 2008 · 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);
}
}

[Read more →]

Tags: java.lang

Inserts a String to a StringBuffer at the specified position

April 20th, 2008 · 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);
}
}

[Read more →]

Tags: java.lang

Finds out the substring of a StringBuffer

April 20th, 2008 · 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);
}
}

[Read more →]

Tags: java.lang

Finds the substring of a StringBuffer starting from specified index.

April 20th, 2008 · 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);
}
}

[Read more →]

Tags: java.lang