tokens -word,paragraph,sentences
delimiter -" " | "-" | ", " | "6"
String str = "one-two-three";
String[] temp;
String delimiter = "-";
temp = str.split(delimiter);
for(int i =0; i < temp.length ; i++)
System.out.println(temp[i]);
-------------------------------------------------------------------------------------------
note: IMPORTANT : Some special characters need to be escaped while providing them as
delimiters like "." and "|".
str = "one.two.three";
delimiter = "\\.";
temp = str.split(delimiter);
for(int i =0; i < temp.length ; i++)
System.out.println(temp[i]);
/*
#
Using second argument in the String.split() method, we can control the maximum
#
number of substrings generated by splitting a string.
# */
temp = str.split(delimiter,2);
for(int i =0; i < temp.length ; i++)
System.out.println(temp[i]);
output
one
two
three
------
one
two.three
Tuesday, August 17, 2010
Compiling a Simple Program with directory structure
One source file,
Hello.java, defines a class called greetings.HelloC:> 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();
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.
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);
}
}
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
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
Subscribe to:
Posts (Atom)