text based

1.Tujuan
● Mendapatkan input dari command-line
● Mengetahui cara untuk memanipulasi properties dari sistem
● Membaca standart input
● Membaca dan menulis file
2. Percobaan
Percobaan 1 : Standart Input – Membaca Karakter
import java.io.*;
public class StandartInput {
public StandartInput() {
}
public static void main(String args[]) throws IOException {
System.out.println(“Hi, what’s your favorite character?”);
char favChar;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
favChar = (char) br.read();
System.out.println(favChar + ” is a good choice!”);
}
}
Distributed by Meruvian Education
Output Percobaan:
Distributed by Meruvian Education
Percobaan 2 : Membaca Keseluruhan String
package jeni.textbased.application;
import java.io.*;
public class GreetUser {
public GreetUser() {
}
public static void main(String args[]) throws IOException {
System.out.println(“Hi, what’s your name?”);
String name;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
name = br.readLine();
System.out.println(“Nice to meet you, ” + name + “! :)”);
}
}
Distributed by Meruvian Education
Output Percobaan
Distributed by Meruvian Education
Percobaan 3 : Membaca File
import java.io.*;
public class ReadFile {
public ReadFile() {
}
public static void main(String args[]) throws IOException {
System.out.println(“What is the name of the file to read from?”);
String filename;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
filename = br.readLine();
System.out.println(“Now reading from ” + filename + “…”);
FileInputStream fis = null;
try {
fis = new FileInputStream(filename);
} catch (FileNotFoundException ex) {
System.out.println(“File not found.”);
}
try {
char data;
int temp;
do {
temp = fis.read();
data = (char) temp;
if (temp != -1) {
System.out.print(data);
}
} while (temp != -1);
} catch (IOException ex) {
System.out.println(“Problem in reading from the file.”);
}
}
}
Distributed by Meruvian Education
Output Percobaan
Distributed by Meruvian Education
Percobaan 4 : Menulis File
import java.io.*;
public class WriteFile {
public WriteFile() {
}
public static void main(String args[]) throws IOException {
System.out.println(“What is the name of the file to be written to?”);
String filename;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
filename = br.readLine();
System.out.println(“Enter data to write to ” + filename + “…”);
System.out.println(“Type q$ to end.”);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filename);
} catch (FileNotFoundException ex) {
System.out.println(“File cannot be opened for writing.”);
}
try {
boolean done = false;
int data;
do {
data = br.read();
if ((chardata == ‘q’) {
data = br.read();
if ((char)data == ‘$’) {
done = true;
} else {
fos.write(‘q’);
fos.write(data);
}
} else {
fos.write(data);
}
} while (!done);
} catch (IOException ex) {
System.out.println(“Problem in reading from the file.”);
}
}
}
Distributed by Meruvian Education
Output Percobaan
Distributed by Meruvian Education

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s