Kamis, 08 Maret 2012

Write FILE Pada java

ini adalah sintax untuk menuliskan program write file pada java :



package Project;


/**
 *
 * @author INDRI
 */
import java.io.*;
public class Write_File {
   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 ((char)data == ' ') {
data = br.read();
if ((char)data == '$') {
done = true;
} else {
fos.write('_');
fos.write(data);
}
} else {
fos.write(data);
}
} while (!done);
} catch (IOException ex) {
System.out.println("Problem in reading from the file.");
}
}
}



#tapi sebelum menjalankan program tersebut buatlah file notepad yang kosong di D ataupun di E .sebagai outputan progam java ini .. 

ini contoh hasil program pada notepad :


hallo_aku_indri_yuli_K._ini_contoh_outputannya_:)q$
q$
;
$

0 komentar:

Posting Komentar

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites