Tuesday, August 17, 2010

Compiling a Simple Program with directory structure

One source file, Hello.java, defines a class called greetings.Hello

C:> dir
greetings/
C:> dir greetings
Hello.java

package greetings;

public class Hello {
public static void main(String[] args) {
for (int i=0; i < args.length; i++) {
System.out.println("Hello " + args[i]);
}
}
}
C:> javac greetings\Hello.java
C:> dir greetings
Hello.class Hello.java
C:> java greetings.Hello thiyagu karthik
Hello thiyagu
Hello karthik

Compiling Multiple Source Files


C:> javac greetings\*.java

Friday, August 13, 2010

Copy one file to another using java File,BufferedReader,BufferedWriter

This example reads text files using the classes FileReader, BufferedReader, FileWriter, and BufferedWriter.

import java.io.*;
import java.util.*;

public class CopyTextFile {

public static void main(String args[]) {
//... Get two file names from use.
System.out.println("Enter a filepath to copy from, and one to copy to.");
Scanner in = new Scanner(System.in);

//... Create File objects.
File inFile = new File(in.next()); // File to read from.
File outFile = new File(in.next()); // File to write to

//... Enclose in try..catch because of possible io exceptions.
try {
copyFile(inFile, outFile);

} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
}


//=============================================================== copyFile
// Uses BufferedReader for file input.
public static void copyFile(File fromFile, File toFile) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(fromFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(toFile));

//... Loop as long as there are input lines.
String line = null;
while ((line=reader.readLine()) != null) {
writer.write(line);
writer.newLine(); // Write system dependent end of line.
}

//... Close reader and writer.
reader.close(); // Close to unlock.
writer.close(); // Close to unlock and flush to disk.
}


//=============================================================== copyFile2
// Uses Scanner for file input.
public static void copyFile2(File fromFile, File toFile) throws IOException {
Scanner freader = new Scanner(fromFile);
BufferedWriter writer = new BufferedWriter(new FileWriter(toFile));

//... Loop as long as there are input lines.
String line = null;
while (freader.hasNextLine()) {
line = freader.nextLine();
writer.write(line);
writer.newLine(); // Write system dependent end of line.
}

//... Close reader and writer.
freader.close(); // Close to unlock.
writer.close(); // Close to unlock and flush to disk.
}
}

File, BufferedReader,BufferedWriter using java

File fromFile=new File("myfile.txt");
File toFile=new File("myout.txt");


BufferedReader br=new BufferedReader(new FileReader(fromFile));
BufferedWriter bw=new BufferedWriter(new FileWriter(toFile));

String line = null;
while ((line=br.readLine()) != null) {
bw.write(line);
bw.newLine(); // Write system dependent end of line.
}

br.close();
bw.close();

Thursday, August 12, 2010

Behavior of PuTTY pscp recursive directory copy different from that of scp?

if you want to copy
the entire "temp" directory to a server (Red Hat Enterprise Linux 4)
using pscp from PuTTY 0.60:

C:\temp\dir1\a.txt
C:\temp\dir2\b.txt


Scenario 1: if there is a trailing slash (or backslash) in the source
path, only the content of "temp" is copied:

C:\>pscp -r temp/ karthik@xxxxxxxxxxxx:/home/karthik/test/

On the server:

/home/karthik/test/dir1/a.txt
/home/karthik/test/dir2/b.txt


Scenario 2: if there is no trailing slash in the source path, the
entire "temp" is copied:

C:\>pscp -r temp michael@xxxxxxxxxxxx:/home/karthik/test/

On the server:

/home/karthik/test/temp/dir1/a.txt
/home/karthik/test/temp/dir2/b.txt


Doing a similar action from a Linux machine to the same server, with
or without the trailing slash, the result of scenario 2 is obtained.

.properties or .ini file read and write using java

[user.properties]

DBuser=anonymous
DBpassword=&8djsx
DBlocation=bigone

--------------------------------------------------------------------
class ReadandWrite {
public static void main(String args[]) {
ReadandWrite props = newReadandWrite();
props.readit();
props.writeit();

}

public void readit() {
try{
Properties p = new Properties();
p.load(new FileInputStream("user.properties"));
System.out.println("user = " + p.getProperty("DBuser"));
System.out.println("password = " + p.getProperty("DBpassword"));
System.out.println("location = " + p.getProperty("DBlocation"));
p.list(System.out);
}
catch (Exception e) {
System.out.println(e);
}
}

public void writeit() {
try{
Properties p = new Properties();
p.load(new FileInputStream("user.properties"));
p.list(System.out);
// new Property
p.put("today", new Date().toString());
// modify a Property
p.put("DBpassword","foo");
FileOutputStream out = new FileOutputStream("myuser.properties");
p.save(out, "/* properties updated */");
}
catch (Exception e) {
System.out.println(e);
}
}
}

java best way to do program

public class ReadandWrite {

public static void main(String[] args) {

new ReadandWrite().readIt();
new ReadandWrite().writeIt();
}


private void readIt()
{
try {

}catch(){ }
}

private void writeIt()
{
try {

}catch(){}
}

} //end of the class

Wednesday, August 11, 2010

Java Configuration Files

The chances are all you really need are some name-value pairs to specify a few parameters. If this is the case Java comes with a wonderfully simple solution right out of the box, .properties files.

The syntax for a .properties file could not be simpler; all lines beginning with a # are comments and all other lines are in the form:
CODE:

1.
< key>= < value>

Below is a sample configuration for the Virtual Learning environment
CODE:

1.
# Basic portal config
2.
BASE_URL=www.eve.nuim.ie/evePortal
3.

4.
# Database stuff
5.
DB_JNDI_NAME=jdbc/eve_portal
6.

7.
# File locations
8.
DATA_DIR=/var/eve-data
9.
PDFLATEX_BIN=/opt/local/bin/pdflatex
10.
IMAGEMAGICK_BIN_DIR=/opt/local/bin

If you choose to use this type of configuration file you can locate and open it all in one step by using functionality built into the java class loader as follows:
JAVA:

1.
Properties configFile = new Properties();
2.
configFile.load(this.getClass().getClassLoader().getResourceAsStream("/my_config.properties"));

You can they read out any key in the following way:
JAVA:

1. some_var = configFile.getProperty("some_key");


Simple as that, no need to load complex parsing libraries, no big long messy code. Unless you have a good reason to go with a more complex format you really should be using .properties files.

Properties configFile = new Properties();
configFile.load(new FileInputStream(”configuration.conf”));
String foo = configFile.getProperty(”my_key”);