package bai_4;
import java.awt.EventQueue;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class B4_1_DemSoTu {
private JFrame frmmST;
private JTextField txtSoTu;
private JTextField txtTenTapTin;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
B4_1_DemSoTu window = new B4_1_DemSoTu();
window.frmmST.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public B4_1_DemSoTu() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmmST = new JFrame();
frmmST.setTitle("\u0110\u1EBFm s\u1ED1 t\u1EEB trong t\u1EADp tin");
frmmST.setBounds(100, 100, 529, 401);
frmmST.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmmST.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("\u0110\u1EBFm s\u1ED1 t\u1EEB trong t\u1EADp tin");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 12));
lblNewLabel.setBounds(172, 33, 204, 14);
frmmST.getContentPane().add(lblNewLabel);
JLabel lblChnTpTin = new JLabel("Ch\u1ECDn t\u1EADp tin:");
lblChnTpTin.setBounds(37, 75, 92, 14);
frmmST.getContentPane().add(lblChnTpTin);
JButton btnChon = new JButton("...");
btnChon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//code here
FileInputStream fis = null;
try {
String path = null;
fis = new FileInputStream(path);
String data = "";
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
data = new String(buffer);
System.out.println(data);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileOutputStream fos = null;
try {
String path = null;
fos = new FileOutputStream(path);
String data;
fos.write(data.getBytes());
System.out.println("Ghi file thanh cong !");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
btnChon.setBounds(424, 71, 45, 23);
frmmST.getContentPane().add(btnChon);
JLabel lblNiDung = new JLabel("N\u1ED9i dung:");
lblNiDung.setBounds(37, 100, 92, 14);
frmmST.getContentPane().add(lblNiDung);
JTextArea txtNoiDung = new JTextArea();
txtNoiDung.setEditable(false);
txtNoiDung.setBackground(Color.WHITE);
txtNoiDung.setRows(5);
txtNoiDung.setBounds(122, 105, 347, 137);
frmmST.getContentPane().add(txtNoiDung);
JLabel lblST = new JLabel("S\u1ED1 t\u1EEB:");
lblST.setBounds(37, 270, 92, 14);
frmmST.getContentPane().add(lblST);
txtSoTu = new JTextField();
txtSoTu.setEditable(false);
txtSoTu.setBounds(122, 267, 347, 20);
frmmST.getContentPane().add(txtSoTu);
txtSoTu.setColumns(10);
JButton btnDoc = new JButton("\u0110\u1ECDc t\u1EADp tin");
btnDoc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//code here
private String docNoiDungFile(File file) throwsIOException{
FileReader fr = newFileReader(file);
char[] cbuf = newchar[(int)file.length()];
fr.read(cbuf);
String noiDung = String.valueOf(cbuf);
fr.close();
return noiDung;
}
}
});
btnDoc.setBounds(119, 313, 103, 23);
frmmST.getContentPane().add(btnDoc);
JButton btnDemsoTu = new JButton("\u0110\u1EBFm s\u1ED1 t\u1EEB");
btnDemsoTu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// code here
private int demSoTu(String text){
String[] ls = text.split("[\\s\\.\\,\\:\\;\\/\\|\\\\]+");
if(ls != null){
return ls.length;
}
return 0;
}
});
btnDemsoTu.setBounds(266, 313, 110, 23);
frmmST.getContentPane().add(btnDemsoTu);
txtTenTapTin = new JTextField();
txtTenTapTin.setBounds(122, 72, 294, 20);
frmmST.getContentPane().add(txtTenTapTin);
txtTenTapTin.setColumns(10);
} }
Mọi người giúp em với ạ, bài tập là chọn tập tin, hiển thị nội dung và đếm số từ . em làm mãi mà nó vẫn sai ạ . có ai khai sáng cho em ko ?