You are on page 1of 8

TUGAS NETWORK PROGRAMMING

MODUL 4

Oleh :
Ade Putra Lesmana
1341180010
TI 3A

JURUSAN TEKNIK INFORMASI


POLITEKNIK NEGERI MALANG
2015

1. Soal 1
Dengan bufferedInputStream
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bufferinputstram;
/**
*
* @author USER
*/
import java.io.*;
public class Bufferinputstram {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try
{
InputStream fileInput = new FileInputStream ("E:\\prak.txt");
BufferedInputStream buf = new BufferedInputStream(fileInput);
int a=0;
int data = buf.read(); // Baca byte ke 1
while ((a=buf.read()) != -1) // ulangi : hingga end of file(EOF) dicapai
{
//System.out.write ( data ); // menampilkan byte data ke console
//System.out.print((char)a);
data = buf.read(); // baca byte berikutnya
}
/**
* @param args the command line arguments
*/
buf.close(); // Close the file
}
catch (IOException ioe)
{
System.err.println ("I/O error - " + ioe);
}

}
}

Tanpa BuffredInputStream
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bufferinputstram;
/**
*
* @author USER
*/
import java.io.*;
public class Bufferinputstram {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try
{
InputStream fileInput = new FileInputStream ("E:\\prak.txt");
//BufferedInputStream buf = new BufferedInputStream(fileInput);
int a=0;
int data = fileInput.read(); // Baca byte ke 1
while ((a=fileInput.read()) != -1) // ulangi : hingga end of file(EOF) dicapai
{
//System.out.write ( data ); // menampilkan byte data ke console
//System.out.print((char)a);

data = fileInput.read(); // baca byte berikutnya


}
/**
* @param args the command line arguments
*/
fileInput.close(); // Close the file
}
catch (IOException ioe)
{
System.err.println ("I/O error - " + ioe);
}
}
}

Hasil dari praktikum diatas adalah membaca file dengan bufferedInputStream lebih
cepat daripada fileInputStream karena bufferedinputStream membaca isi file dengan
skala besar sedangkan fileInputStream membaca isi filenya per byte jadi sedikit lebih
lama.
Minimal besar file yang dipakai untuk praktikum adalah 1 mb
2. Soal 2
Output Stream dengan buffered
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bufferinputstram;
import java.io.*;
/**

*
* @author USER
*/
public class BufferOutputStream {
public static void main(String[] args) {
// TODO code application logic here
FileInputStream inputan = null;
BufferedInputStream inputan1 = null;
FileOutputStream outputan = null;
BufferedOutputStream outputan1 = null;
int data;
try
{
inputan = new FileInputStream("E:\\prak.txt");
inputan1 = new BufferedInputStream(inputan);
}
catch(FileNotFoundException fnfe)
{
System.out.println("File input tidak ada!");
return;
}
try
{
outputan = new FileOutputStream("E:\\coba_copy.txt");
outputan1 = new BufferedOutputStream(outputan);
}
catch (FileNotFoundException fnfe)
{
System.out.println("File outputan gagal!");
return;
}
try
{
while ((data = inputan1.read())!= -1)
{
outputan1.write(data);
}
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
return;
}
try
{
inputan1.close();
outputan2.close();
}
catch (IOException ioe)

{
}
}
}

Output Stream tanpa buffer


/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bufferinputstram;
import java.io.*;
/**
*
* @author USER
*/
public class BufferOutputStream {
public static void main(String[] args) {

// TODO code application logic here


FileInputStream inputan = null;
BufferedInputStream inputan1 = null;
FileOutputStream outputan = null;
BufferedOutputStream outputan1 = null;
int data;
try
{
inputan = new FileInputStream("E:\\prak.txt");
//inputan1 = new BufferedInputStream(inputan);
}
catch(FileNotFoundException fnfe)
{
System.out.println("File input tidak ada!");
return;
}
try
{
outputan = new FileOutputStream("E:\\coba_copy.txt");
//outputan1 = new BufferedOutputStream(outputan);
}
catch (FileNotFoundException fnfe)
{
System.out.println("File outputan gagal!");
return;
}
try
{
while ((data = inputan.read())!= -1)
{
outputan.write(data);
}
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
return;
}
try
{
inputan.close();
outputan.close();
}
catch (IOException ioe)
{
}
}

Hasil dari praktikum diatas adalah membaca file dengan bufferedInputStream lebih
cepat daripada fileInputStream karena bufferedinputStream membaca isi file dengan
skala besar sedangkan fileInputStream membaca isi filenya per byte jadi sedikit lebih
lama. Dan ketika writen file juga lebih cepat.
Minimal besar file yang dipakai untuk praktikum adalah 1 mb

You might also like