Kamis, 08 Maret 2012

Read File pada java

ini adalah sintax untuk menuliskan program READ FILE pada java : 


package Test_Jeni1;

/**
 *
 * @author INDRI
 */
import java.io.*;
public class Read_File {
    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.");
}
}
}

#untuk menjalankan program ini jangan lupa menyediakan file notepad yang sudah ada isinya di D ataupun di E. :D

0 komentar:

Posting Komentar

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites