Running external command from java
October 22, 2009
Leave a comment
import java.io.*;
public class ExternalCommand
{
public static void main(String args[]) throws Exception
{
String line;
OutputStream stdin = null;
InputStream stderr = null;
InputStream stdout = null;
// launch EXE and grab stdin/stdout and stderr
Process process = Runtime.getRuntime ().exec ("bc");
stdin = process.getOutputStream ();
stderr = process.getErrorStream ();
stdout = process.getInputStream ();
// "write" the parms into stdin
line = "13*3" + "\n";
stdin.write(line.getBytes() );
stdin.flush();
stdin.close();
// clean up if any output in stdout
BufferedReader brCleanUp =
new BufferedReader (new InputStreamReader (stdout));
while ((line = brCleanUp.readLine ()) != null) {
System.out.println ("[Stdout] " + line);
}
brCleanUp.close();
// clean up if any output in stderr
brCleanUp =
new BufferedReader (new InputStreamReader (stderr));
while ((line = brCleanUp.readLine ()) != null) {
System.out.println ("[Stderr] " + line);
}
brCleanUp.close();
}
}
ref: http://www.rgagnon.com/javadetails/java-0014.html
Categories: Uncategorized
easy tabular file access with sql like command
October 19, 2009
Leave a comment
<?
/////////////////////////////////////////////////////
// Abdul Arfan 2007 Dec
// db file
// akses file jadi mirip database
////////////////////////////////////////////////////
//insert(array("arfan", "1204556658", "Abdul Arfan"));
delete(array(0=>"arfan"));
display_all();
///////////////////////////
// untuk menginsert data //
///////////////////////////
function insert( $array )
{
$handle = fopen ( "./db.txt", "a" );
$first =true;
write_array($handle, $array);
}
///////////////////////////
// untuk searching data //
///////////////////////////
function select( $array_constrain )
{
$result = null;
$counter=0;
$handle = fopen ( "./db.txt", "r" );
while($line = fgets($handle))
{
$array = split(",", $line);
$masuk = true;
if($array_constrain)
{
foreach ($array_constrain as $key => $value)
{
if($array[$key]!=$value)
{
$masuk = false;
}
}
}
if($masuk)
{
$result[$counter]=$array;
$counter++;
}
}
return $result;
}
function delete( $array_constrain )
{
$result = null;
$counter=0;
$handle = fopen ( "./db.txt", "r" );
$handle2 = fopen ( "./db-temporary.txt", "w" );
while($line = fgets($handle))
{
$array = split(",", $line);
$cocok = true;
if($array_constrain)
{
foreach ($array_constrain as $key => $value)
{
if($array[$key]!=$value)
{
$cocok = false;
}
}
}
if(!$cocok)
{
write_array($handle2, $array);
}
}
fclose ($handle );
fclose ($handle2 );
//hapus db.txt
$myFile = "db.txt";
unlink($myFile);
rename ( "db-temporary.txt", "db.txt" );
return $result;
}
function update( $array_constrain , $array_update)
{
$result = null;
$counter=0;
$handle = fopen ( "./db.txt", "r" );
$handle2 = fopen ( "./db-temporary.txt", "w" );
while($line = fgets($handle))
{
$array = split(",", $line);
$cocok = true;
if($array_constrain)
{
foreach ($array_constrain as $key => $value)
{
if($array[$key]!=$value)
{
$cocok = false;
}
}
}
if($cocok)
{
foreach ($array_update as $key => $value)
{
$array[$key] = $array_update[$key];
}
}
write_array($handle2, $array);
}
fclose ($handle );
fclose ($handle2 );
//hapus db.txt
$myFile = "db.txt";
unlink($myFile);
rename ( "db-temporary.txt", "db.txt" );
return $result;
}
/**
fungsi-fungsi tambahan
*/
function write_array($handle, $array)
{
$first =true;
foreach($array as $el)
{
if($first)
{
$total = trim($el);
$first=false;
}
else
{
$total = $total.", ".trim($el) ;
}
}
fwrite($handle, $total."\n");
}
/**
* return dari ini adalah satu baris saja
* jadi bukan bentuk tabel
*/
function select_one( $array_constrain )
{
$handle = fopen ( "./db.txt", "r" );
while($line = fgets($handle))
{
$array = split(",", $line);
foreach ($array_constrain as $key => $value)
{
if($array[$key]==$value)
return $array;
}
}
return null;
fclose ($handle );
}
function display_all()
{
$result = select(null);
echo "<table border=\"1\">";
foreach($result as $row)
{
echo "<tr>";
foreach($row as $element)
{
echo "<td>$element</td>";
}
echo "</tr>";
}
echo "</table>";
}
?>
Categories: php
Simple Java Input and Output
October 12, 2009
Leave a comment
import java.io.*;
public class SimpleIO
{
public static void main(String args[])
{
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("what is your name? ");
String name = reader.readLine();
System.out.println("hello "+name+", have a nice day");
}
catch(Exception e)
{
System.out.println("Exception occured");
e.printStackTrace();
}
}
}
Categories: java
Recent Comments