Advanced Programming CH-02
Advanced Programming CH-02
By: Mule Ta – 2
CH02: Systems Programming
By: Mule Ta – 3
CH02: Systems Programming
By: Mule Ta – 3
CH02: Systems Programming
FILES !
What are Stream ?
By: Mule Ta – 3
CH02: Systems Programming
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
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
By: Mule Ta – 4
CH01: Functional Programming
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
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
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
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
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
try{
String data = read.next(); // reciving data
}catch(Exception){
// handling the exception
}finally{
// code executed regardless
}
}
}
By: Mule Ta – 8
CH02: Systems Programming
try{
String data = revFromConsole.readLine(); // reciving data
}catch(Exception){
// handling the exception
}finally{
// code executed regardless
}
}
}
By: Mule Ta – 9
CH02: Systems Programming
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 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 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
By: Mule Ta – 12
CH02: Systems Programming
By: Mule Ta – 13
CH02: Systems Programming
By: Mule Ta – 13
CH02: Systems Programming
import java.io.*;
try {
FileOutputStream out = new FileOutputStream("output.txt");
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.*;
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;
try {
FileOutputStream output = new FileOutputStream("output.txt");
catch (Exception e) {
e.getStackTrace();
}
}
}
By: Mule Ta – 19
CH02: Systems Programming
By: Mule Ta – 20
CH02: Systems Programming
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) {
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);
catch (Exception e) {
catch(Exception e) {
System.out.println(e)
System.out.println(e);
}
}
}
}
}
By: Mule Ta – 22
CH02: Systems Programming
By: Mule Ta – 24
CH02: Systems Programming
By: Mule Ta – 24
CH02: Systems Programming
By: Mule Ta – 24
CH02: Systems Programming
By: Mule Ta – 24
CH02: Systems Programming
↓
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
↓
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
By: Mule Ta – 25
CH02: Systems Programming
▶ Shared Lock
By: Mule Ta – 26
CH02: Processes in Java
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!
By: Mule Ta – 28
CH02: Systems Programming
By: Mule Ta – 29
CH02: Systems Programming
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ProcessDemo {
By: Mule Ta – 30
CH02: Systems Programming
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