Sie sind auf Seite 1von 3

/*

* 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 pbo;
import java.util.*;
import java.io.*;

/**
*
* @author M. Irpansyah
*/
public class kodingSendiri {
public static void main(String[] args) throws IOException{
String file = "D:\\file.txt";
String data;
int masuk;
int num= 1;
for (int i = 0;i < num;){
Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
System.out.println("1. Input");
System.out.println("2. Cari");
System.out.println("3. Hapus");
System.out.println("4. Tampil");
System.out.println("5. Quit");
System.out.print("Masukkan Operasi Anda : ");
masuk = input2.nextInt();
System.out.print("");
if(masuk == 1){
System.out.print("Masukkan Data Anda : ");

data = input.nextLine();

try{
Writer output;
// FileWriter fileWriter = new FileWriter(file);
// BufferedWriter bufferedWriter = new BufferedWriter(new
fileWriter(file));
// bufferedWriter = new BufferedWriter(new FileWriter(file, true));
output = new BufferedWriter(new FileWriter(file, true));
output.write(data);
output.close();
}
// try(FileWriter fileWriter = new FileWriter(file)) {
// fileWriter.write(data);
// fileWriter.newLine();
// }
catch(FileNotFoundException ex){
System.out.println(
"Unable to open file '" +
file + "'");
}
}else if(masuk == 2)
{
String word = ""; int val = 0;
while(!word.matches("Tidak"))
{
System.out.print("Masukkan Kata Yang Mau Dicari : ");
Scanner input4 = new Scanner(System.in);
word = input4.next();
Scanner file1 = new Scanner(new File("D:\\file.txt"));

while(file1.hasNextLine())
{
String line = file1.nextLine();
if(line.indexOf(word) != -1)
{
System.out.println(word+" Ada Di Dalam File");
val++;
}else
{
val = 0;
continue;
}
}
if (val == 0)
{
System.out.print("Kata Ngak Ada");
}
System.out.println("Lanjut Atau Tidak");
word = input.next();
}
}
else if(masuk == 3)
{
// File inputFile = new File("D:\\file.txt");
//
// BufferedReader reader = new BufferedReader(new
FileReader(inputFile));
// BufferedWriter writer = new BufferedWriter(new
FileWriter(inputFile));
//
// System.out.println("Enter the word to be detele for");
// Scanner input4 = new Scanner(System.in);
// String word1 = input4.nextLine();
// String currentLine;
//
// while((currentLine = reader.readLine()) != null)
// {
// String trimmedLine = currentLine.trim();
// if(trimmedLine.equals(word1)) continue;
// writer.write(currentLine);
// }
File file2 = new File("D:\\file.txt");
BufferedReader reader = new BufferedReader(new FileReader(file2));
String line = "", oldtext = "";
while((line = reader.readLine()) != null)
{
oldtext += line + "\r\n";
}
reader.close();
System.out.print("Masukkan Huruf Yang Mau Dihapus : ");
Scanner input4 = new Scanner(System.in);
String word1 = input4.nextLine();
String replacedtext = oldtext.replaceAll(word1, "");
FileWriter writer = new FileWriter("D:\\file.txt");
writer.write(replacedtext);

writer.close();
}
else if(masuk == 4)
{
try{
byte[] buffer = new byte[1000];
FileInputStream inputStream = new FileInputStream(file);
int total = 0;
int nRead = 0;
while((nRead = inputStream.read(buffer)) != -1) {
// Convert to String so we can display it.
// Of course you wouldn't want to do this with
// a 'real' binary file.
System.out.println(new String(buffer));
total += nRead;
}

// Always close files.


inputStream.close();

}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
file + "'");
}
}
else if(masuk == 5){
System.out.println("Terimakasih Telah Menggunakan Sistem Sederhana
Ini");
break;
}
}
}
}