Baca File di Java


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package bacafile;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
*
* @author komp18
*/
public class BacaFile {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
File file = new File (“E:/ANGGA’s DATA/3. COLLAGE/PSSI – Semester 1/Algoritma dan Struktur Data 1/102410101070/Test.txt”); // INI LOKASI FILE YANG INGIN DI BACA
StringBuffer contents = new StringBuffer();
BufferedReader reader = null;

try
{
reader = new BufferedReader(new FileReader(file));
String text = null;

//repeat until all lines is read
while ((text = reader.readLine()) != null)
{
contents.append(text)
.append(System.getProperty(“line.separator”));
}

}catch (FileNotFoundException e)
{
e.printStackTrace();
}catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
if (reader != null)
{
reader.close();
}
}catch (IOException e)
{
e.printStackTrace();
}
}

// show file contents here
System.out.println(contents.toString());
}
}

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 )

Foto Google

You are commenting using your Google 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 )

Connecting to %s