Membaca Data Dari File

Wednesday, March 3rd, 2010 4 Commented

Menginport data dari file ke dalam program komputer untuk dianalisis atau diproses dalam pengolahan data adalah hal terpenting dalam Scientifics Programming. Artikel ini adalah contoh class BacaFile.java yang saya sering gunakan untuk untuk tujuan tersebut

Membaca Data dari File Baris per Baris

dataLineByLineMisalkan kita memiliki deretan data satu kolom seperti gambar.1, Nama program untuk membaca file seperti ini adalah BacaFile.java, berikut alur jalannya program.

  1. Didalam method choosingFile(), JFileChooser(“D:/myData”) digunakan untuk membuka jendela browsing  untuk mencari dimana file data disimpan dalam komputer. Secara default jendela browsing akan membuka folder myData di Drive D:/. [Baris 12]
  2. Setelah ditemukan dan jika file setuju dibuka : if(returnVal == JFileChooser.APPROVE_OPTION), file tersebut dismpan menjadi objek stream sehingga mudah digunakan oleh program. [Baris 18-20]
  3. Method readLineByLine() akan melakukan looping untuk membaca data stream ini baris demi baris. [Baris 30-32]
public class BacaFile {
    FileInputStream fstream;
    DataInputStream in;
    BufferedReader br;
    Vector data;

    public BacaFile() {
        data = new Vector();
    }

    public void chooseFile() {
        JFileChooser chooser = new JFileChooser(“D:/”);
        FileNameExtensionFilter filter = new FileNameExtensionFilter(“Text Only”, “txt”);
        chooser.setFileFilter(filter);
        int returnVal = chooser.showOpenDialog(new JFrame());
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                fstream = new FileInputStream(chooser.getSelectedFile());
                in = new DataInputStream(fstream);
                br = new BufferedReader(new InputStreamReader(in));
            }catch (Exception e){
                System.err.println("Error:" + e.getMessage());
            }
        }
    }

    public Vector readLineByLine() {
        String strLine;
        try {
            while ((strLine = br.readLine()) != null) {
                data.addElement(strLine);
            }
            in.close();
            return data;
        } catch (Exception e) {
            System.err.println("Error:" + e.getMessage());
            return null;
        }
    }
}

Class diatas dijalankan menggunakan TestClass.java

public class TestClass {
    public static void main(String args[]) {
        BacaFile bacaFile = new BacaFile();
        bacaFile.chooseFile();
        Vector data = bacaFile.readElementByElement();
        for(int i=0;data.size(); i++) {
            System.out.println(String.valueOf(data.get(i)));
        }
    }
}

Membaca dari File Elemen per Elemen

dataElemenByElemen

Agar program dapat membaca data elemen per elemen yang membentuk matrix nxm seperti gambar 2. Kode program BacaFile.java perlu disisipkan String[] subLine = strLine.split(“\t”) [Baris 30-31]. Seperti yang dicontohkan diatas, strLine menyimpan data berupa string baris per baris, misalnya untuk baris pertama strLine berisi “439 397 252 168 375” yang dipisahkan masing-masing menggunakan Tab. Kemudian strLine.split(“\t”) akan memisahkan mereka berdasarkan pemisah tab dan menyimpannya dalam array String[].

    while ((strLine = br.readLine()) != null) {
        String[] subLine = strLine.split(“\t”);
        data.addElement(subLine);
     }

Untuk menampilkan data pada kolom ke 3 misalnya, modifikasi TestClass.java pada baris 6-8, menjadi seperti ini

    for(int i=0;data.size(); i++) {
        String[] subData = data.get(i);
        System.out.println(subData[2]);
    }
Related Posts
  • Interpolasi Kriging untuk Membuat Peta Kontur

4 Responses to “Membaca Data Dari File”

  1. dian says:

    mas,bisa bantu saya..

    Scanned 192.168.8.0 – 192.168.8.128
    5/16/2010 3:29:38 PM

    IP Ping Hostname

    192.168.8.1 0 ms N/A
    192.168.8.2 Dead N/S
    192.168.8.14 Dead N/S
    192.168.8.15 Dead N/S
    192.168.8.16 Dead N/S
    192.168.8.17 Dead N/S
    192.168.8.27 Dead N/S
    192.168.8.28 Dead N/S
    192.168.8.29 Dead N/S
    192.168.8.30 Dead N/S
    192.168.8.31 Dead N/S
    192.168.8.92 Dead N/S
    192.168.8.102 Dead N/S
    192.168.8.103 Dead N/S
    192.168.8.104 Dead N/S
    192.168.8.105 Dead N/S
    192.168.8.106 0 ms mrs-441f029518b.domain_not_set.invalid
    192.168.8.107 Dead N/S
    192.168.8.108 Dead N/S
    192.168.8.109 Dead N/S
    192.168.8.110 Dead N/S
    192.168.8.111 5 ms N/A
    192.168.8.112 22 ms N/A
    192.168.8.113 Dead N/S
    192.168.8.114 130 ms N/A
    192.168.8.115 26 ms N/A
    192.168.8.116 Dead N/S
    192.168.8.117 Dead N/S

    bagaimana agar java dapat mengenali ip yang aktif berdasarkan pingnya..codingnya gimana ya?tolong bantuannya..makasi n_n

  2. Emily N. says:

    Hi, I’m very interested in Linux but Im a Super Newbie and I’m having trouble deciding on the right distribution for me (Havent you heard this a million times?) anyway here is my problem, I need a distribution that can switch between reading and writing in English and Japanese (Japanese Language Support) with out restarting the operating system.

  3. Kristan Bren says:

    This is a bit off discussion, which I apologize for, but would you and your readers mind voicing your opinion about the recent oil spill, you’re opinion seriously helps and I can’t thank you enough for taking a few seconds to give it. I left the URL in the appropriate field, thank you!

  4. I recently decided to create a short movie about this, I would be appreciative if you could maybe take a moment to look it and possibly leave a comment about what you think, I left the video link in the “website” field, hopefully you can get to it, I appreciate it greatly

Leave a Reply