Advanced Programming CH-02

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 52

CHAPTER 02: Systems Programming

By: Muleta Taye T. E-mail: muleta.taye@aastu.edu.et


April 5, 2023
CH02: Topics to be covered

2.1 File Descriptors


2.2 Reading and Writing Files
2.3 Files and Directories
2.4 File Locking
2.5 Memory Mapped I/O
2.6 Creating Processes
2.7 Pipes and Signals

By: Mule Ta – 2
CH02: Systems Programming

ˆ Where and how data is stored?

By: Mule Ta – 3
CH02: Systems Programming

ˆ Where and how data is stored? 7→ Variables


ˆ Data must be persistent
↓ How?

By: Mule Ta – 3
CH02: Systems Programming

ˆ Where and how data is stored? 7→ Variables


ˆ Data must be persistent
↓ How?

FILES !
ˆ What are Stream ?

By: Mule Ta – 3
CH02: Systems Programming

ˆ Where and how data is stored? 7→ Variables


ˆ Data must be persistent
↓ How?

FILES !
ˆ What are Stream ?
ˆ is a logical container of data that allows us to read from and write to it.
ˆ streams are the sequence of data that are read from the source and written
to the destination.
ˆ The stream-based IO operations are faster than normal IO operation

By: Mule Ta – 3
CH02: Systems Programming

ˆ Where and how data is stored? 7→ Variables


ˆ Data must be persistent
↓ How?

FILES !
ˆ What are Stream ?
ˆ is a logical container of data that allows us to read from and write to it.
ˆ streams are the sequence of data that are read from the source and written
to the destination.
ˆ The stream-based IO operations are faster than normal IO operation

By: Mule Ta – 3
CH02: Systems Programming

ˆ Java provides two types of streams:.


ˆ Byte Stream
ˆ devised to transmit 8bit of data.
ˆ Two abstract classes
1. InputStream : byte stream based input operations
2. OutputStream: byte stream based Output operations
ˆ Character Stream
ˆ devised to transmit 16bit of data.
ˆ Two abstract classes
1. Reader : class used for character stream based input operations
2. Writer: class used for charater stream based output operations

By: Mule Ta – 4
CH01: Functional Programming

Table 1: InputStream class

Methods Description
int available() It returns the number of bytes that can be read from the input stream.
int read() It reads the next byte from the input stream.
int read(byte[] b) It reads a chunk of bytes from the input stream and store them in its byte array, b.
void close() It closes the input stream and also frees any resources connected with this input stream.
mark() marks the position in the input stream up to which data has been read
reset() returns the control to the point in the stream where the mark was set
skips() skips and discards the specified number of bytes from the input stream
markSupported() checks if the mark() and reset() method is supported in the stream

By: Mule Ta – 5
CH01: Functional Programming

Table 1: InputStream class

Methods Description
int available() It returns the number of bytes that can be read from the input stream.
int read() It reads the next byte from the input stream.
int read(byte[] b) It reads a chunk of bytes from the input stream and store them in its byte array, b.
void close() It closes the input stream and also frees any resources connected with this input stream.
mark() marks the position in the input stream up to which data has been read
reset() returns the control to the point in the stream where the mark was set
skips() skips and discards the specified number of bytes from the input stream
markSupported() checks if the mark() and reset() method is supported in the stream

Table 2: OutputStream class

Methods Description
void write(int n) It writes byte(contained in an int) to the output stream.
void write(byte[] b) It writes a whole byte array(b) to the output stream.
void flush() It flushes the output steam by forcing out buffered bytes to be written out.
void close() It closes the output stream and also frees any resources connected with this output stream.

By: Mule Ta – 5
CH02: Systems Programming

ˆ Assume a file advancedProgramming.txt

import java.io.*;
public class SystemProgramming {
public static void main(String args[]) {
byte[] readAsArray = new byte[100];

try {
InputStream input = new FileInputStream("advancedProgramming.txt");
System.out.println("Result of available(): " + input.available());
System.out.println("Result from read(): " + input.read());

input.read(readAsArray);
System.out.print("Data read from the file: ");
String data = new String(readAsArray);
System.out.println(data);

input.close();

} catch (Exception e) {
e.printStackTrace();
}

By: Mule Ta – 6
CH02: Systems Programming

ˆ Assume a file advancedProgramming.txt

import java.io.*;
public class SystemProgramming {
public static void main(String args[]) {
byte[] readAsArray = new byte[100];

try {
InputStream input = new FileInputStream("advancedProgramming.txt");
System.out.println("Result of available(): " + input.available());
System.out.println("Result from read(): " + input.read());

input.read(readAsArray);
System.out.print("Data read from the file: ");
String data = new String(readAsArray);
System.out.println(data);
Result of available(): 68
input.close(); Result from read(): 73
Content is: ntelligence plus character, that is the key goal of true education!
} catch (Exception e) {
e.printStackTrace();
}

By: Mule Ta – 6
CH02: Systems Programming

ˆ How to Interact ?
ˆ Three ways to read console input.
1. BufferedReader class
• Oldest style!
java.io.BufferedReader;
java.io.InputStreamReader;
public class BufferReaderInputDemo{
public static void main(String [] args) throws IOException{
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader read = new BufferedReader(input);

try{
String data = read.readLine(); // reciving data
}catch(Exception){
// handling the exception
}finally{
// code executed regardless
}
}
}
2. Using Scanner class
3. Using Console class

By: Mule Ta – 7
CH02: Systems Programming

ˆ three ways to read console input.


2 Scanner class
• Commonly used method
java.util.Scanner;
public class ScannerInputDemo{
public static void main(String [] args) {
Scanner recive = new Scanner(System.in)

try{
String data = read.next(); // reciving data
}catch(Exception){
// handling the exception
}finally{
// code executed regardless
}
}
}

By: Mule Ta – 8
CH02: Systems Programming

ˆ three ways to read console input.


2 Console class
• Commonly used method in old java!
java.util.Console;
public class ConsoleInputDemo{
public static void main(String [] args) {
Console revFromConsole = System.console();

try{
String data = revFromConsole.readLine(); // reciving data
}catch(Exception){
// handling the exception
}finally{
// code executed regardless
}
}
}

By: Mule Ta – 9
CH02: Systems Programming

ˆ What about Kicking data to Output entity ?


ˆ there are two methods to write console output.
1. Using print() and println() methods
2. Using write() method

ˆ What is File ?
ˆ Everything is a FILE in Computing!
ˆ is a named location that can be used to store related information
ˆ is directory a file?

By: Mule Ta – 10
CH02: Systems Programming

ˆ What about Kicking data to Output entity ?


ˆ there are two methods to write console output.
1. Using print() and println() methods
2. Using write() method

ˆ What is File ?
ˆ Everything is a FILE in Computing!
ˆ is a named location that can be used to store related information
ˆ is directory a file?
• is a Special kind of directory!
• is a collection of files and subdirectories.
• Java application needs to be able to handle files.
ˆ Any Operation ?

By: Mule Ta – 10
CH02: Systems Programming

ˆ What about Kicking data to Output entity ?


ˆ there are two methods to write console output.
1. Using print() and println() methods
2. Using write() method

ˆ What is File ?
ˆ Everything is a FILE in Computing!
ˆ is a named location that can be used to store related information
ˆ is directory a file?
• is a Special kind of directory!
• is a collection of files and subdirectories.
• Java application needs to be able to handle files.
ˆ Any Operation ?
• Create
• Delete
• Write
• Updating

By: Mule Ta – 10
CH02: Systems Programming

ˆ In java, the File class has been defined in the java.io package.
ˆ The File class represents a reference to a file or directory
ˆ To create a file:
ˆ File (String pathname)
ˆ Syntax:
import java.io.File;
File file = new File(String pathName);
ˆ File (String parent, String child)
ˆ It Creates a new File instance from a parent absolute pathname and a child
pathname string.
ˆ Syntax:
import java.io.File;
File file = new File(String absolutePathname, String filename);
ˆ File (URI uri)
ˆ It creates a new File instance by converting the given file: URI into an abstract
pathname.
ˆ Syntax:
import java.io.File;
File file = new File(URI urlPathName);

By: Mule Ta – 11
CH02: Systems Programming

• Method for operating on the file Object

By: Mule Ta – 12
CH02: Systems Programming

ˆ How File is Accessed?


ˆ The operating system produces and maintains an entry to represent the file
whenever the user opens input/output streams.
↓ How handles ?

By: Mule Ta – 13
CH02: Systems Programming

ˆ How File is Accessed?


ˆ The operating system produces and maintains an entry to represent the file
whenever the user opens input/output streams.
↓ How handles ?
”File descriptor”
ˆ File descriptors
ˆ are number that uniquely identifies an open file in a computer’s operating system.
ˆ java.io.FileDescriptor is used to open files with specified names.
ˆ Commonly used in to store it in
• FileInputStream or FileOutputStream.
ˆ File descriptors has Fields and Methods
• Fields:
7→ Err: is a handle to the standard error stream.
7→ In: is a handle to the standard input stream.
7→ Out: is a handle to the standard output stream
• Methods:
7→ sync(): It synchronises all the system buffers with the underlying system.
7→ Valid (): This method is advantageous in checking the validity of the
file descriptor object.

By: Mule Ta – 13
CH02: Systems Programming

import java.io.*;

public class SystemProgramming {


public static void main(String args[]) throws Exception {

FileDescriptor filedesctiptor = null;

// String "SOFTWARE ENG"


byte[] data2write = { 83, 79, 70, 84, 87, 65, 82, 69, 32, 69, 78, 71 };

try {
FileOutputStream out = new FileOutputStream("output.txt");

// This getFD() method is called before closing the output stream


filedesctiptor = out.getFD();

// writes byte to file output stream


out.write(data2write);

// USe of sync() : to sync data to the source file


filedesctiptor.sync();
System.out.print("\nUse of Sync Successful ");

} catch (Exception except) {


// if in case IO error occurs
except.printStackTrace();
} finally {
// releases system resources
if (out != null)
out.close();
}

By: Mule Ta –} 14
CH02: Reading and Writing Files

ˆ In java, there multiple ways to read data from a file and to write data to a file.
ˆ Different ways:
1. Using Byte Stream (FileInputStream and FileOutputStream)
2. Using Character Stream (FileReader and FileWriter)

1. FileInputStream
ˆ allows reading data from a file.
ˆ implemented based on the byte stream.
ˆ provides a method read() to read data from a file byte by byte.
ˆ Syntax:
import java.io.FileInputStream
FileInputStream input = new FileInputStream(stringPath);
\\input stream that will be linked to the file specified by the path
FileInputStream input = new FileInputStream(File fileObject);
ˆ Methods
ˆ read() 7→ reads a single byte from the file
ˆ read(byte[] array) 7→ reads the bytes from the file and stores in the specified array
ˆ read(byte[] array, int start, int length) 7→ reads the number of bytes equal to
length from the file and stores in the specified array starting from the position
start

By: Mule Ta – 15
CH02: Systems Programming

ˆ Methods:
ˆ finalize() 7→ ensures that the close() method is called
ˆ getChannel() 7→ returns the object of FileChannel associated with the input
stream
ˆ getFD() 7→ returns the file descriptor associated with the input stream
ˆ mark() 7→ mark the position in input stream up to which data has been read
ˆ reset() 7→ returns the control to the point in the input stream where the
mark was set
ˆ Example
ˆ Create a txt File named your section
ˆ write a text if the file already creaated notify error message unless.

By: Mule Ta – 16
CH02: Systems Programming

import java.io.*;

public class SystemProgramming {


public static void main(String args[]) {

File file = new File("section.txt");


import java.io.FileInputStream;
public class FileInputStreamDemo{
try {
public static void main(String [] args){
FileInputStream fi = new FileInputStream(file);
try {
System.out.println(fi.available());
FileInputStream fi= new FileInputStream("fileDemo.txt");
System.out.println(fi.skip(2));
// Assume the file has "The Storm is Coming!"
System.out.println(fi.read());
int idx = fi.read();
int idx = fi.read();
while (idx != -1) {
while(idx != -1) {
System.out.print((char) idx);
System.out.println(idx); // Result ?
idx = fi.read();
idx = fi.read();
}
}
fi.close();
}catch(Exception excpt){
System.out.println("\n");
System.out.println(excpt);
}
} catch (Exception e) {
}
e.printStackTrace();
}

By: Mule Ta – 17
CH02: Systems Programming

ˆ FileOutputStream Class
ˆ It allows writing data to a file.
ˆ Iplemented based on the byte stream.
ˆ provides a method write() to write data to a file byte by byte.
ˆ Syntax:
import java.io.FileOutputStream
FileOutputStream output = new FileOutputStream(String path, boolean value);
FileOutputStream output = new FileOutputStream(File fileObject);
ˆ Methods:
ˆ write() 7→ writes the single byte to the file output stream
ˆ write(byte[] array) 7→ writes the bytes from the specified array to the output
stream
ˆ write(byte[] array, int start, int length) 7→ writes the number of bytes equal to
length to the output stream from an array starting from the position start
ˆ finalize() 7→ ensures that the close() method is called
ˆ getChannel() 7→ returns the object of FileChannel associated with the output
stream
ˆ getFD() 7→ returns the file descriptor associated with the Output stream
ˆ getBytes() method used in the program converts a string into an array of bytes.

By: Mule Ta – 18
CH02: Systems Programming

import java.io.FileOutputStream;

public class FileOutputStreamDemo {


public static void main(String[] args) {
String data = "The Storm is Coming!";

try {
FileOutputStream output = new FileOutputStream("output.txt");

// byte[] array = data.getBytes();

// Writes byte to the file


output.write(data.getBytes());
// output.write(32);
output.flush();
output.close();
}

catch (Exception e) {
e.getStackTrace();
}
}
}

By: Mule Ta – 19
CH02: Systems Programming

ˆ Character Stream has classes for handling Files


ˆ FileReader
ˆ allows reading data from a file
ˆ implemented based on the character stream.
ˆ The FileReader class provides a method read() to read data from a file character
by character.
ˆ Syntaz:
import java.io.FileReader
FileReader read = new FileReader(String path);
FileReader read = new FileReader(File fileObj);
ˆ Methods:
• read() 7→ reads a single character from the reader
• read(byte[] array) 7→ reads the characters from the reader and stores in the
specified array
• read(byte[] array, int start, int length) 7→reads the number of characters equal
to length from the reader and stores in the specified array starting from the
position start
• The getEncoding() method can be used to get the type of encoding that is
used to store data in the file

By: Mule Ta – 20
CH02: Systems Programming

ˆ Character Stream ...


ˆ FileWriter
ˆ allows writing data from a file
ˆ implemented based on the character stream.
ˆ The FileWriter class provides a method write() to write data from a file
character by character.
ˆ Syntaz:
import java.io.FileWriter
FileWriter write = new FileWriter(String path);
FileWriter write = new FileWriter(File fileObj);
ˆ Methods:
• write() 7→ write a single character to the writer
• write(char[] array) 7→ writes the characters from the specified array to the
writer
• write(String data) 7→ writes the specified string to the writer
• The getEncoding() method can be used to get the type of encoding that is
used to store data in the file

By: Mule Ta – 21
CH02: Systems Programming

import java.io.FileReader;
import java.io.FileWriter;

class FileReaderDemo {
public class FileWriterDemo {
public static void main(String[] args) {

public static void main(String args[]) {


// Creates an array of character
char[] array = new char[100];
String data = "Just signaling a data to the output";

try {
try {
// Creates a reader using the FileReader
// Creates a FileWriter
FileReader charReader = new FileReader("section.txt");
FileWriter output = new FileWriter("output.txt");

// Reads characters
// Writes the string to the file
charReader.read(array);
output.write(data);
System.out.println(array);

// Closes the writer


// Closes the reader
output.close();
charReader.close();
}
}

catch (Exception e) {
catch(Exception e) {
System.out.println(e)
System.out.println(e);
}
}
}
}
}

By: Mule Ta – 22
CH02: Systems Programming

▶ What are Directories ?

ˆ container for multiple files.


ˆ possibly we can use File Class in java.io package.

has methods to create a directory! 7→ mkdir()
• Instantiate the File classand passing the path
• Invoke the mkdir() method using the above created file object.
• Invoke the delete() method using the above file object.
ˆ Example:
import java.io.*;
import java.util.*;

public class CreateDirectory {


public static void main(String[] args) {
import java.io.*;
System.out.println("Path: ");
public class CreateDirectory {
Scanner readPath = new Scanner(System.in);
public static void main(String[] args) {
String path = readPath.next();
File file = new File("./folder");
System.out.println("Enter the name of the desired a direc
boolean dir = file.mkdir();
path = path + readPath.next();
if (dir) {
// Instantiate the File class
System.out.println("D: Successfuly created");
File file = new File(path);
} else {
// Creating a folder using mkdir() method
System.out.println("Failed to create");
boolean dir = file.mkdir();
}
if (dir) {
}
System.out.println("Folder is created successfully");
} else {
}
System.out.println("Somthing went Wrong");
By: Mule Ta – 23
}
CH02: Systems Programming

ˆ Consistency and security is critical in computing!


ˆ Specially on concurrent/parallel programming.
ˆ While reading/writing to a file 7→ Ensure Data Integrity

By: Mule Ta – 24
CH02: Systems Programming

ˆ Consistency and security is critical in computing!


ˆ Specially on concurrent/parallel programming.
ˆ While reading/writing to a file 7→ Ensure Data Integrity

File Locking
ˆ File Locking: is a way of locking files while being used by some processes;
ˆ The Java java.nio API enables locking of the file at the operating system level
ˆ Two Kind of Locks

By: Mule Ta – 24
CH02: Systems Programming

ˆ Consistency and security is critical in computing!


ˆ Specially on concurrent/parallel programming.
ˆ While reading/writing to a file 7→ Ensure Data Integrity

File Locking
ˆ File Locking: is a way of locking files while being used by some processes;
ˆ The Java java.nio API enables locking of the file at the operating system level
ˆ Two Kind of Locks
• Exclusive locks also known as write locks()
• Shared locks also referred to as read locks
ˆ java FileChannel provides to method

By: Mule Ta – 24
CH02: Systems Programming

ˆ Consistency and security is critical in computing!


ˆ Specially on concurrent/parallel programming.
ˆ While reading/writing to a file 7→ Ensure Data Integrity

File Locking
ˆ File Locking: is a way of locking files while being used by some processes;
ˆ The Java java.nio API enables locking of the file at the operating system level
ˆ Two Kind of Locks
• Exclusive locks also known as write locks()
• Shared locks also referred to as read locks
ˆ java FileChannel provides to method
• lock() method : acquires an exclusive lock on entire file
• lock(long position, long size, boolean shared)

can be used to acquire a lock on the given part or section of a File.

By: Mule Ta – 24
CH02: Systems Programming

ˆ Consistency and security is critical in computing!


ˆ Specially on concurrent/parallel programming.
ˆ While reading/writing to a file 7→ Ensure Data Integrity

File Locking
ˆ File Locking: is a way of locking files while being used by some processes;
ˆ The Java java.nio API enables locking of the file at the operating system level
ˆ Two Kind of Locks
• Exclusive locks also known as write locks()
• Shared locks also referred to as read locks
ˆ java FileChannel provides to method
• lock() method : acquires an exclusive lock on entire file
• lock(long position, long size, boolean shared)

can be used to acquire a lock on the given part or section of a File.


These two methods will block until the lock is obtained.
• tryLock() method: acquires an exclusive lock on entire file,
• tryLock(long position, long size, boolean shared)

can be used to acquire a lock on the given part or section of a File.

By: Mule Ta – 24
CH02: Systems Programming

ˆ Consistency and security is critical in computing!


ˆ Specially on concurrent/parallel programming.
ˆ While reading/writing to a file 7→ Ensure Data Integrity

File Locking
ˆ File Locking: is a way of locking files while being used by some processes;
ˆ The Java java.nio API enables locking of the file at the operating system level
ˆ Two Kind of Locks
• Exclusive locks also known as write locks()
• Shared locks also referred to as read locks
ˆ java FileChannel provides to method
• lock() method : acquires an exclusive lock on entire file
• lock(long position, long size, boolean shared)

can be used to acquire a lock on the given part or section of a File.


These two methods will block until the lock is obtained.
• tryLock() method: acquires an exclusive lock on entire file,
• tryLock(long position, long size, boolean shared)

can be used to acquire a lock on the given part or section of a File.


By: Mule Ta – they don’t block and they return immediately. 24
CH02: Systems Programming

▶ Exclusive Lock

ˆ To create a FileChannel directly via the static open method:


ˆ The FileLock instance represents a lock on the file or a region of the file
ˆ Syntax:
import java.nio.channels.FileChannel;
FileChannel channel = FileChannel.open(path, openOptions)

ˆ To get an exclusive lock, use a writable FileChannel.



use getChannel() methods of a FileOutputStream or a RandomAccessFile.
import java.nio.channels.FileChannel;
FileChannel channel = FileChannel.open(path, StandardOpenOption.APPEND) //using open mehod
try (FileOutputStream fileOutputStream = new FileOutputStream(path); // using FileOutputStream
FileChannel channel = fileOutputStream.getChannel();
FileLock lock = channel.lock()) {
// write to the channel
}

try (RandomAccessFile file = new RandomAccessFile(path, "rw");


FileChannel channel = file.getChannel();
FileLock lock = channel.lock()) {
// write to the channel
}

By: Mule Ta – 25
CH02: Systems Programming

▶ Shared Lock

ˆ are also called read locks.


ˆ to get a read lock, we must use a readable FileChannel.
ˆ FileChannel can be obtained by calling the getChannel() method on a
FileInputStream or a RandomAccessFile
import java.nio.channels.FileChannel;
try (FileChannel channel = FileChannel.open(path, StandardOpenOption.READ);
FileLock lock = channel.lock(0, Long.MAX_VALUE, true)) {
// read from the channel
}
try (FileInputStream fileInputStream = new FileInputStream(path );
FileChannel channel = fileInputStream.getChannel();
FileLock lock = channel.lock(0, Long.MAX_VALUE, true)) {
// read from the channel
}
try (RandomAccessFile file = new RandomAccessFile("/tmp/testfile.txt", "r");
FileChannel channel = file.getChannel();
FileLock lock = channel.lock(0, Long.MAX_VALUE, true)) {
// read from the channel
}

By: Mule Ta – 26
CH02: Processes in Java

ˆ What are PROCESSES ?


ˆ a program currently being executed!
ˆ heavyweight tasks running in computing machines!
ˆ an abstract class defined in the java.lang package that encapsulates the
runtime information of a program in execution.
public abstract class Process extends Object

ˆ two ways of creating java processes


ˆ Invoking the exec() method in the Runtime instance
ˆ By using ProcessBuilder.start() method.
ˆ The processes class has method
ˆ int exitValue() : Exits code returned from the process executed;
ˆ Reads/writes output, error, and input streams to and from the process.
• InputStream getErrorStream()
• InputStream getInputStream()
• OutputStream getOutputStream()
ˆ Boolean isAlive(): Checks to see if the invoking process is still running.
ˆ void destroy(): to Kill Process
ˆ Process destroyForcibly() : to kill more forcefully

By: Mule Ta – 27
CH02: Systems Programming

ˆ Runtime Class
ˆ which encapsulates the runtime environment of the program.
ˆ class cannot be instantiated

”get a reference to the Runtime of the currently running program”

static method called Runtime.getRuntime()

only this method ?

By: Mule Ta – 28
CH02: Systems Programming

ˆ Runtime Class
ˆ which encapsulates the runtime environment of the program.
ˆ class cannot be instantiated

”get a reference to the Runtime of the currently running program”

static method called Runtime.getRuntime()

only this method ? NO, Check the API....
ˆ Syntax:
import java.lang.*;
Runtime runtime = Runtime.getRuntime();
// use runtime enviroment to use methods!

ˆ Write a simple Java program to open an application as a vscode editor and


close automatically after 10sec.
import java.lang.*;
import java.util.concurrent.TimeUnit;
public class NetbeandProcessDemo {
public static void main(String[] args) throws Exception {
Runtime runvscode = Runtime.getRuntime();
Process vscodePrc = runvscode.exec("vscode");
vscodePrc.waitFor(10, TimeUnit.SECONDS);
vscodePrc.destroy();
}
}

By: Mule Ta – 28
CH02: Systems Programming

ˆ The ProcessBuilder Class


ˆ is instantiated to manage a collection of process attributes.
ˆ To create a process 7→ start()
ˆ not safe that much
ˆ has two constructor
ˆ ProcessBuilder(List<String> command)

the command to be executed, along with command line arguments, is passed in
a list of strings.
ˆ ProcessBuilder(String... command)

the command and the command line arguments are specified through the
varargs parameter

By: Mule Ta – 29
CH02: Systems Programming

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ProcessDemo {

public static void main(String[] args) {

System.out.println("*************Calendar for Year**********");


try {
ProcessBuilder pb = new ProcessBuilder("cal", "2023");
final Process p = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (Exception ex) {
System.out.println(ex);
}
System.out.println("************************************");
}
}

By: Mule Ta – 30
CH02: Systems Programming

■ What are Pipes and Signals in Java ?

Next Class ...

By: Mule Ta – 31
CH02: Systems Programming

By: Mule Ta – 32
CH02: Systems Programming

By: Mule Ta – 33
CH02: Systems Programming

By: Mule Ta – 34
CH02: Systems Programming

By: Mule Ta – 35
CH02: Systems Programming

By: Mule Ta – 36
Done!
Question!

By: Mule Ta – 37

You might also like