Sie sind auf Seite 1von 7

TUGAS

PEMROGRAMAN JARINGAN

Oleh :
Galih Maulana Adji (1641720131)

PROGRAM STUDI TEKNIK INFORMATIKA


JURUSAN TEKNOLOGI INFORMASI
POLITEKNIK NEGERI MALANG
2018
Praktikum Minggu 3

1. InputStream

package inputstream;

import java.io.*;

public class InputStream {


public static void main(String[] args) {
if (args.length != 1)
{
System.err.println ("Syntax - FileInputStreamDemo file");
return;
}
try
{
// Membuat input stream yang membaca dr file
FileInputStream fileInput = new FileInputStream ( args[0] );

int data = fileInput.read(); // Baca byte ke 1

while (data != -1) // ulangi : hingga end of file (EOF) dicapai


{
System.out.write ( data ); // menampilkan byte data ke console
data = fileInput.read(); // baca byte berikutnya
}

fileInput.close(); // Close the file


}
catch (IOException ioe)
{
System.err.println ("I/O error - " + ioe);
}
}
}
2. OutputStream
package outputstream;

import java.io.*;

public class OutputStream {


public static void main(String[] args) {
if (args.length != 2) // Two parameters are required, the
source and destination
{
System.err.println ("Syntax - FileOutputStreamDemo src dest");
return;
}
String source = args[0];
String destination = args[1];
try
{
// Open source file for input
InputStream input = new FileInputStream( source);
System.out.println ("Opened " + source + " for reading.");
FileOutputStream output = new FileOutputStream ( destination );
// Ouput output file for output
System.out.println ("Opened " + destination + " for writing.");
int data = input.read();
while ( data != -1)
{
// Write byte of data to our file
output.write (data);
// Read next byte
data=input.read();
}
// Close both streams
input.close();
output.close();
System.out.println ("I/O streams closed");
}
catch (IOException ioe)
{
System.err.println ("I/O error - " + ioe);
}

}
}
3. Writer dan Reader
 Input 1
package input1;
import java.io.*;
public class Input1 {
public static void main(String[] args) throws IOException
{
String str;
BufferedReader br;
br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Masukkan Nama Anda : Galih Maulana Adji ");
str= br.readLine();
System.out.println("Hello "+ str);

}
}

 Input 2
package input2;

import java.io.*;
public class Input2 {
public static void main(String[] args) throws IOException{
int angka1, angka2;
BufferedReader br;
br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Masukkan Angka1 : ");
angka1 = Integer.parseInt(br.readLine());
System.out.print("Masukkan Angka2 : ");
angka2 = Integer.parseInt(br.readLine());
System.out.println("Angka1 Anda : "+ angka1);
System.out.println("Angka2 Anda : "+ angka2);
}
}
Tugas Minggu 3

1. Buat program seperti pada subbab 3.4.1 dan 3.4.2 sehingga aplikasi tidak lagi
membutuhkan argumen dalam eksekusinya, melainkan memunculkan dialog untuk
membuka file yang ada pada komputer kita.

package tugasminggu3;

import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import java.io.FileInputStream;
import java.io.*;

public class TugasMinggu3 {

public static void main(String args[])


{
String[] notepad = {"E:\\coba.txt"};

if (notepad.length != 1)
{
System.err.println ("Syntax - FileInputStreamDemo file");
return;
}

try
{
StringBuffer sb = new StringBuffer();
InputStream fileInput = new FileInputStream ( notepad[0] );
int data = fileInput.read();
sb.append((char)data);

while (data != -1)


{
System.out.write ( data );
data = fileInput.read();
sb.append((char)data);
}

fileInput.close();
JOptionPane.showMessageDialog(null, sb);
}
catch (IOException ioe)
{
System.err.println ("I/O error - " + ioe);
}
}
}
2. Buat modifikasi pada kode Output Stream di subbab 3.4.2 agar nama file input dan file
output (hasil copy) ditampilkan dalam bentuk dialog.

package tugasminggu3;

import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import java.io.*;
public class Tugas2 {
public static void main(String[] args)
{
String[] notepad = {"e:\\coba.txt","e:\\coba2.txt"};
if (notepad.length != 2)
{
System.err.println ("Syntax - FileOutputStreamDemo src dest");
return;
}

String source = notepad[0];


String destination = notepad[1];

try
{
InputStream input = new FileInputStream( source);
System.out.println ("Opened " + source + " for reading.");
OutputStream output = new FileOutputStream ( destination );
System.out.println ("Opened " + destination + " for writing.");
int data = input.read();
while ( data != -1)
{
output.write (data);
data=input.read();
}
input.close();
output.close();
System.out.println ("I/O streams closed");
JOptionPane.showMessageDialog(null,"File Input : "+source+'\n'+"File Output : "+destination);
}
catch (IOException ioe)
{
System.err.println ("I/O error - " + ioe);
}
}
}

3. Buatlah program untuk membaca sebuah file text, kemudian tampilkan jumlah huruf a, i, u, e
dan o yang ada dalam file tersebut!

Das könnte Ihnen auch gefallen