0% found this document useful (0 votes)
38 views11 pages

Tugas Pbo Ketik

The document provides 5 examples of using different Java input/output streams: 1) Using File to check if a directory exists and list its contents 2) Using FileInputStream to read bytes from a file 3) Using BufferedInputStream to efficiently read and process bytes from an input stream 4) Using PushbackInputStream to "unread" bytes that have already been read 5) Using ObjectInputStream and ObjectOutputStream to serialize and deserialize Java objects to files

Uploaded by

Jo R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views11 pages

Tugas Pbo Ketik

The document provides 5 examples of using different Java input/output streams: 1) Using File to check if a directory exists and list its contents 2) Using FileInputStream to read bytes from a file 3) Using BufferedInputStream to efficiently read and process bytes from an input stream 4) Using PushbackInputStream to "unread" bytes that have already been read 5) Using ObjectInputStream and ObjectOutputStream to serialize and deserialize Java objects to files

Uploaded by

Jo R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

1.

Melihat direktori

import java.io.File;

public class Coding1 {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

String dirname = "/java";

File f1 = new File(dirname);

if (f1.isDirectory()){

System.out.println("Directory of " + dirname);

String s[] = f1.list();

for (int i=0; i < s.length; i++) {

File f = new File(dirname + "/" + s[i]);

if (f.isDirectory()) {
System.out.println(s[i] + " is a directory");

} else {

System.out.println(s[i] + " is a file");

} else {

System.out.println(dirname + " is not a directory");

2.Menggunakan Fileinputstream

import java.io.*;

public class Coding2 {

/**

* @param args the command line arguments


*/

public static void main(String[] args) {

int size;

// USe try-with-resources to close the stream.

try ( FileInputStream f =

new FileInputStream("FileInputStreamDemo.java") ) {

System.out.println("Total Available Bytes: " +

(size = f.available()));

int n = size/40;

System.out.println("First " + n +

" bytes of the file one read() at a time");

for (int i=0; i < n; i++){

System.out.print((char) f.read());

System.out.println("\nStill Available: " + f.available());

System.out.println("Reading the next " + n +

" with one read(b[])");

byte b[] = new byte[n];

if (f.read(b) != n) {

System.err.println("couldn't read " + n + " bytes.");

System.out.println(new String(b, 0, n));

System.out.println("\nStill Available: " + (size = f.available()));

System.out.println("Skipping half of remaining bytes with skip()");

f.skip(size/2);
System.out.println("Still Available: " + f.available());

System.out.println("Reading " + n/2 + " into the end of array");

if (f.read(b, n/2, n/2) != n/2) {

System.err.println("couldn't read " + n/2 + " bytes.");

System.out.println(new String(b, 0, b.length));

System.out.println("\nStill Available: " + f.available());

} catch(IOException e) {

System.out.println("I/O Error: " + e);

3.Contoh Menggunakan BufferedInputStream

import java.io.*;
public class Coding3 {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

String s = "This is a & copy; copyrigth symbol" +

"but this is &copy not.\n";

byte buf[] = s.getBytes();

ByteArrayInputStream in = new ByteArrayInputStream(buf);

int c;

boolean marked = false;

// Use try-with-resources to manage the files.

try ( BufferedInputStream f = new BufferedInputStream(in) )

while ((c = f.read()) != -1){

switch(c) {

case '&':

if (!marked){

f.mark(32);

marked = true;

} else {

marked = false;

break;

case ';':

if (marked) {
marked = false;

System.out.print("(c)");

} else

System.out.print((char) c);

break;

case ' ':

if (marked) {

marked = false;

f.reset();

System.out.print("&");

} else

System.out.print((char) c);

break;

default:

if (!marked)

System.out.print((char) c);

break;

} catch (IOException e){

System.out.println("I/O Error: " + e);

}
4.Contoh menggunakan PushbackInputStream

import java.io.*;

public class Coding4 {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

String s = "if (a == 4) a = 0;\n";

byte buf[] = s.getBytes();

ByteArrayInputStream in = new ByteArrayInputStream(buf);

int c;

try ( PushbackInputStream f = new PushbackInputStream(in) )

while ((c = f.read()) != -1) {

switch(c) {

case '=':
if ((c = f.read()) == '=')

System.out.print(".eq.");

else {

System.out.print("<-");

f.unread(c);

break;

default:

System.out.print((char) c);

break;

} catch(IOException e) {

System.out.println("I/O Error: " + e);

}
5.Contoh menggunakan Serialization
import java.io.*;

public class Coding5 {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

//Object serialization

try ( ObjectOutputStream objOStrm =

new ObjectOutputStream(new FileOutputStream("serial")))

MyClass object1 = new MyClass ("Hello", -7, 2.7e10);

System.out.println("object1:" + object1);

objOStrm.writeObject(object1);

catch(IOException e ){

System.out.println("Exception during serializarion: " + e);

//Object deserialization

try(ObjectInputStream objIStrm =

new ObjectInputStream( new FileInputStream("serial")))

MyClass object2 = (MyClass) objIStrm.readObject();

System.out.println("object2: " + object2);

}
catch(Exception e){

System.out.println("Exception during deserialization: " + e);

class MyClass implements Serializable{

String s;

int i;

double d;

public MyClass(String s, int i, double d){

this.s = s;

this.i = i;

this.d = d;

public String toString(){

return "s=" + s + "; i=" + "; d=" + d;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy