File IO Stream dengan sorting dan search
/**
* @(#)io_sort_search.java
*
* io_sort_search application
*
* @author
* @version 1.00 2012/6/20
*/
import java.io.*;
import java.util.*;
import java.util.Scanner;
public class io_sort_search {
public static void input(String s)
{
try
{
FileInputStream fstream = new FileInputStream("D:/tst.txt");
//Mengambil Data
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
String Isi_Baru ="";
int index_baris =0;
String source = "";
while ((strLine = br.readLine()) != null) {
source += strLine + "\n";
}
source += s;
char buffer[] = new char[source.length()];
source.getChars(0, source.length(), buffer, 0);
FileWriter f0 = new FileWriter("D:/tst.txt");
for (int i=0; i < buffer.length; i += 1) {
f0.write(buffer[i]);
}
f0.close();
} catch (Exception exp)
{
System.out.println(exp.toString());
}
}
public static void edit_baris(int pildata,String isi_sisipan,int baris_ke)
{
try
{
FileInputStream fstream = new FileInputStream("D:/tst.txt");
//Mengambil Data
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
String[] values;
//Membaca Perbaris
String Isi_Baru ="";
String baru="";
int index_baris =1;
while ((strLine = br.readLine()) != null) {
System.out.println(strLine.toString());
values = strLine.toString().split("#");
if(index_baris == baris_ke)
{
if(pildata==0)
{
values[0]= isi_sisipan.toString();
Isi_Baru+=values[0];
for(int x=1;x<values.length;x++)
{
Isi_Baru+=values[x];
}
Isi_Baru +="\n";
}else if(pildata==1)
{
values[0]= isi_sisipan.toString();
Isi_Baru+=values[0]+"#";
Isi_Baru+=values[1]+"#";
for(int x=2;x<values.length;x++)
{
Isi_Baru+="#"+values[x];
}
Isi_Baru +="\n";
}else if(pildata==2)
{
values[2]= isi_sisipan.toString();
Isi_Baru+=values[0]+"#";
Isi_Baru+=values[1]+"#";
Isi_Baru+=values[2]+"#";
for(int x=3;x<values.length;x++)
{
Isi_Baru+="#"+values[x];
}
Isi_Baru +="\n";
}else
{
Isi_Baru += strLine.toString();
Isi_Baru +="\n";
}
for(int x=0;x<values.length;x++)
{
System.out.println("data "+x+" : "+values[x]);
}
} else {
Isi_Baru+=strLine.toString();
Isi_Baru +="\n";
}
index_baris++;
}
char buffer[] = new char[Isi_Baru.length()];
Isi_Baru.getChars(0, Isi_Baru.length(), buffer, 0);
FileWriter f0 = new FileWriter("D:/tst.txt");
for (int i=0; i < buffer.length; i += 1) {
f0.write(buffer[i]);
}
f0.close();
} catch (Exception exp)
{
System.out.println(exp.toString());
}
}
public static void bubble_sort()
{
try{
//Membuka File
FileInputStream fstream = new FileInputStream("D:/tst.txt");
//Mengambil Data
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
String tampung;
int x = 0;
String[] values = new String[100];
while ((strLine = br.readLine()) != null) {
values[x] = (strLine.toString());
for (int xx=0;xx<=x;xx++)
{
for (int z=0;z<=x-1;z++)
{
if ((Integer.parseInt(values[z])) > (Integer.parseInt(values[z+1])))
{
tampung = values[z];
values [z] = values[z+1];
values[z+1] = tampung;
}
}
}
x++;
}
FileWriter f0 = new FileWriter("D:/tst.txt");
f0.write("");
f0.close();
for (int l = 0; l< x ; l++)
{
input(values[l]);
}
in.close();
}catch (Exception e){
//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
public static void sequential_search(int cari)
{
try{
//Membuka File
FileInputStream fstream = new FileInputStream("D:/tst.txt");
//Mengambil Data
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
String tampung;
int x = 0;
//int cari;
boolean ada;
ada=false;
String[] values = new String[100];
while ((strLine = br.readLine()) != null) {
values[x] = (strLine.toString());
if ((Integer.parseInt(values[x])) == cari)
{
ada = true;
if(ada = true)
{
System.out.print(" ketemu di posisi "+(x+1));
}else
{
System.out.print("tidak ada");
}
}
x++;
}
FileWriter f0 = new FileWriter("D:/tst.txt");
f0.write("");
f0.close();
for (int l = 0; l< x ; l++)
{
input(values[l]);
}
in.close();
}catch (Exception e){
//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
public static void cetak()
{
try{
//Membuka File
FileInputStream fstream = new FileInputStream("D:/tst.txt");
//Mengambil Data
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
String[] values;
//Membaca Perbaris
while ((strLine = br.readLine()) != null) {
// Cetak
System.out.println(strLine.toString());
}
in.close();
}catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
}
public static void hapus()
{
try
{
FileWriter f0 = new FileWriter("D:/tst.txt");
f0.write("");
f0.close();
} catch (Exception exp)
{
System.out.println(exp.toString());
}
}
public static void main(String[] args) {
Scanner rei=new Scanner(System.in);
for(int pilihan=1;pilihan<8;pilihan++)
{
System.out.println("===== MENU =====");
System.out.println("1.Input");
System.out.println("2.Edit");
System.out.println("3.Cetak");
System.out.println("4.Hapus");
System.out.println("5.Bubble Sort");
System.out.println("6.Sequential Search");
System.out.println("7.Keluar");
System.out.println("================");
System.out.println("Pilihan anda ?");
pilihan=rei.nextInt();
switch(pilihan)
{
case 1:
{
System.out.println("Masukan data yang anda inginkan ! ");
String data = rei.next();
input(data);System.out.println();
break;
}
case 2 :
{
System.out.println("Silahkan Masukkan Baris yang akan di Edit ! ");
int baris_nya = rei.nextInt();
//baris_edit(baris_nya);
System.out.println("Data keberapa yang ingin anda edit ? ");
System.out.println("Pilihan anda 0-2");
int data = rei.nextInt();
System.out.println("Silahkan Masukkan Kata ! ");
String kata = rei.next();
edit_baris(data,kata,baris_nya);System.out.println();
break;
}
case 3:
{
cetak();System.out.println();break;
}
case 4 :
{
hapus();System.out.println();break;
}
case 5 :
{
bubble_sort();System.out.println();break;
}
case 6 :
{
System.out.print("masukan data yang ingin dicari ");
int cari= rei.nextInt();
sequential_search(cari);System.out.println();break;
}
}
}
}
}