Dr. GÜRAY SONUGÜR

Dr. Öğr. Üyesi Güray SONUGÜR – Öğrenci Bilgi Paylaşım Platformu

Dosya Okuma

ekran3

 

 

 

 

 

 

 

Okunan dosya örneği

 

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class DosyaOku {

private JFrame frame;
private JTextField txtDosyaAdi;
private JTextField txtIcerik;
private JTextField txtAdSoyad;
private JTextField txtNumara;
private JTextField txtBolumu;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DosyaOku window = new DosyaOku();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public DosyaOku() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JLabel lblDosyaAdi = new JLabel(“Dosya Ad\u0131”);
lblDosyaAdi.setBackground(Color.CYAN);
lblDosyaAdi.setBounds(10, 11, 58, 24);
frame.getContentPane().add(lblDosyaAdi);

txtDosyaAdi = new JTextField();
txtDosyaAdi.setColumns(10);
txtDosyaAdi.setBackground(Color.CYAN);
txtDosyaAdi.setBounds(71, 15, 143, 24);
frame.getContentPane().add(txtDosyaAdi);

JLabel lblIcerik = new JLabel(“\u0130\u00E7erik”);
lblIcerik.setBounds(10, 46, 58, 24);
frame.getContentPane().add(lblIcerik);

txtIcerik = new JTextField();
txtIcerik.setColumns(10);
txtIcerik.setBounds(71, 46, 336, 24);
frame.getContentPane().add(txtIcerik);

JButton btnTemizle = new JButton(“Temizle”);
btnTemizle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
txtDosyaAdi.setText(“”);
txtAdSoyad.setText(“”);
txtNumara.setText(“”);
txtBolumu.setText(“”);
txtIcerik.setText(“”);
}
});
btnTemizle.setBounds(71, 81, 89, 23);
frame.getContentPane().add(btnTemizle);

JButton btnOku = new JButton(“Oku”);
btnOku.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{

String dosya=txtDosyaAdi.getText();

try {
String str=dosyaOku(dosya);
txtIcerik.setText(str);
String[] strSplit=str.split(“#”);
txtAdSoyad.setText(strSplit[0]);
txtNumara.setText(strSplit[1]);
txtBolumu.setText(strSplit[2]);

} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
});
btnOku.setBounds(172, 81, 69, 23);
frame.getContentPane().add(btnOku);

JLabel lblAdSoyad = new JLabel(“Ad\u0131 Soyad\u0131”);
lblAdSoyad.setBounds(10, 141, 82, 24);
frame.getContentPane().add(lblAdSoyad);

txtAdSoyad = new JTextField();
txtAdSoyad.setColumns(10);
txtAdSoyad.setBounds(102, 141, 143, 24);
frame.getContentPane().add(txtAdSoyad);

JLabel lblNumara = new JLabel(“Numaras\u0131”);
lblNumara.setBounds(10, 168, 58, 24);
frame.getContentPane().add(lblNumara);

txtNumara = new JTextField();
txtNumara.setColumns(10);
txtNumara.setBounds(102, 172, 86, 22);
frame.getContentPane().add(txtNumara);

JLabel lblBolumu = new JLabel(“B\u00F6l\u00FCm\u00FC”);
lblBolumu.setBounds(10, 198, 58, 24);
frame.getContentPane().add(lblBolumu);

txtBolumu = new JTextField();
txtBolumu.setColumns(10);
txtBolumu.setBounds(102, 202, 258, 22);
frame.getContentPane().add(txtBolumu);
}

private static String dosyaOku(String dosyaAd) throws IOException
{
FileReader f = new FileReader(dosyaAd);
BufferedReader in = new BufferedReader(f);

String icerik=””;
icerik=in.readLine();
in.close();
return icerik;
}

}