3-COE312 - Lecture Java IO
3-COE312 - Lecture Java IO
Java I/O
COE312
Omar Arif
Slides adapted from Dr Imran Zualkernan slides.
2
Handling The technical term for this is: Java will throw
an exception (throw an error).
3
Method Description
OutputStre
2) public void write(byte[])throws is used to write an array of
IOException byte to the current output
stream.
am 3) public void flush()throws flushes the current output
IOException stream.
import java.io.FileOutputStream;
FileOutputStream(”testout.txt");
single fout.write(65); // Write ‘A’
character fout.close();
System.out.println("success...");
}catch(Exception e){
System.out.println(e);
}
}
}
File is located in
the Eclipse project
folder (FileOutput)
Output is a
file called
testout.txt
14
import java.io.FileOutputStream;
Writing a array
byte [] b = s.getBytes();// converting String into byte
String to a
file fout.write(b);
fout.close();
System.out.println("success...");
} catch (Exception e) {
System.out.println(e);
}
}
}
15
Output to a
String
16
InputStream
17
Method Description
FileInputStre
to b.length bytes of data from the
input stream.
am
int read(byte[] b, int off, int It is used to read up to len bytes of
len) data from the input stream.
Example – FileInputStream(”input.txt");
Note we read an int.
Reading a int i = fin.read();
fin.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
20
Example – FileInputStream("testout.txt");
Note: EOF is the same as -1
Reading int i = 0;
fin.close();
} catch (Exception e) {
System.out.println(e);
}
}}
22
Streams
Sender
Buffer Receiver
23
Adding a
Buffer to the
FileOutputSt
ream
25
write
Constructor Description
BufferedOutputStrea FileOutputStrea
m m
import java.io.*;
FileOutputStream("testout.txt");
BufferedOutputStream(fout);
bout.flush();
bout.close();
fout.close();
System.out.println("success");
}
28
Wrapping bout.write()
functionality BufferedOutputStream
around (add buffering)
write()
existing one
FileOutputStream
(writes to file)
29
import java.io.*;
Using int i;
while ((i = bin.read()) != -1) {
System.out.print((char) i);
InputBuffer }
bin.close();
fin.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
30
Wrapping
functionality BufferedInputStream
(add buffering)
around existing read()
one
FileInputStream
(read data from file)
31
Wrapping FileOutputStream
Model
BufferedOutputStream
32
Primitive
data types
in Java
https://www.binaryconvert.com/
result_float.html?
decimal=0510460490520490530
57
34
void write(byte[] b, int off, It is used to write len bytes of data to the output
int len) stream.
FileOutputStream(”data.txt");
data.writeFloat((float)3.14159);;
Example data.flush();
data.close();
System.out.println("Succcess...");
}
}
Method Description
int read(byte[] b, int off, int len) It is used to read len bytes of data from the
input stream.
DataInputStre
char value.
void readFully(byte[] b, int off, int len) It is used to read len bytes from the input
stream.
38
import java.io.*;
FileOutputStream
FileInputStream
readFloat()
3.14159
40
import java.io.File;
import java.io.IOException;
try {
File file = new File("output.txt");
Example if (file.createNewFile()) {
System.out.println("New File is created!");
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
43