/**
* @(#)File_s.java
*
* File_s application
*
* @author
* @version 1.00 2012/5/26
*/
import java.io.*;
import java.util.*;
import java.util.Scanner;
public class File_s {
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 baris_ke, String isi_sisipan)
{
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 ="";
int index_baris =1;
while ((strLine = br.readLine()) != null) {
if (Isi_Baru != "")
{
Isi_Baru += "\n";
}
if(index_baris == baris_ke)
{
Isi_Baru += isi_sisipan.toString();
} else
{
Isi_Baru += strLine.toString();
}
index_baris++;
}
System.out.println(Isi_Baru);
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 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) {
int pil = 0;
Scanner s = new Scanner(System.in);
while (pil != 5)
{
System.out.println("1. Input");
System.out.println("2. Edit");
System.out.println("3. Cetak");
System.out.println("4. Hapus");
System.out.println("Silahkan Masukkan Pilihan Anda ! ");
pil = s.nextInt();
switch (pil)
{
case 1 : {
System.out.println("Silahkan Masukkan Kata ! ");
String kata = s.next();
input(kata);
break;
}
case 2 : {
System.out.println("Silahkan Masukkan Baris yang akan di Edit ! ");
int baris_nya = s.nextInt();
System.out.println("Silahkan Masukkan Kata ! ");
String kata = s.next();
edit_baris(baris_nya,kata);
break;
}
case 3 : {
cetak();
break;
}
case 4 : {
hapus();
break;
}
default :{
break;
}
}
}
}
}