STREAM JAVA SOURCE CODE : SEDERHANA

berikut ini contoh source code java stream. langsung aja yah ga usah banyak omong. check this out!

source code untuk menginputkan string dengan stream.


import java.io.*;
public class inputkarakter {

public static void main(String[] args)throws IOException{
System.out.println("masukkan sembarang karakter");
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
String s = br.readLine();
System.out.println("kata yang anda masukkan adalah "+s);
//char ch = (char)br.read();
//System.out.println("karakter yang anda masukkan yaitu "+ch);
}

}

source code untuk membaca isi file yang diambil dari direktori anda.


import java.io.*;
public class bacafile {

public static void main(String[] args){
FileInputStream input = null;
FileInputStream input2 = null;
int data;
int data2;
//membuka file
try{
input = new FileInputStream("E:/semester VI/Praktikum Sister/anjing.txt");
input2 = new FileInputStream("E:/semester VI/Praktikum Sister/lu.txt");

}catch(FileNotFoundException e){
System.out.println("file tidak ditemukan");

}
//membaca data dari file
System.out.print("file pertama : ");
try{
while((data=input.read()) != -1){
System.out.print((char)data);
}
System.out.println();
}catch(IOException e){
System.out.println(e.getMessage());
}
System.out.print("file kedua : ");
try{
while((data2 = input2.read()) != -1){
System.out.print((char)data2);
}
}catch(IOException e2){
System.out.println(e2.getMessage());
}

//menutup file
try{
input.close();
}catch(IOException e){

}
}

}

source code untuk menggabungkan 2 file.


import java.io.*;
public class gabungFile {

public static void main(String[] args) throws IOException{
FileInputStream anjing = null;
FileInputStream lu = null;
BufferedReader br = null;
BufferedReader brd = null;
DataInputStream d = null;
DataInputStream c = null;
String data = null;
FileWriter fw = null;
BufferedWriter out = null;

try{
anjing = new FileInputStream("E:/semester VI/Praktikum Sister/anjing.txt");
lu = new FileInputStream("E:/semester VI/Praktikum Sister/lu.txt");
d = new DataInputStream(anjing);
c = new DataInputStream(lu);
br = new BufferedReader(new InputStreamReader(d));
brd = new BufferedReader(new InputStreamReader(c));
data = br.readLine()+" "+brd.readLine();
//System.out.println(data);
fw = new FileWriter("E:/semester VI/Praktikum Sister/anjinglu.txt");
out = new BufferedWriter(fw);
out.write(data);
out.close();
}catch(FileNotFoundException e){
System.out.println("data nggak ada");
}
}
}

untuk mempelajari source code sederhana ini jangan anda copy-paste. tetapi cobalah dengan mengetiknya sendiri kemudian pelajari source code tersebut.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s