StringTokenizer object available in util package is used to split the String. It splits the string into tokens using spaces between them. A file is chosen using JFileChooser and is read as string readLine method of BufferedReader object. Result is displayed in JTextArea.
import java.sql.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class StringTokenizerTest {
JPanel panel;
JFrame jf;
JLabel label1,label2;
JButton Browse,Split;
JTextField textfield1,textfield2,textfield3;
JPasswordField passwordfield;
JTextArea textArea1;
String s="";
public StringTokenizerTest() {
initComponents();
handlingEvents();
}
public void initComponents() {
jf=new javax.swing.JFrame();
jf.setTitle("StringTokenizerTest");
jf.setLayout(null);
jf.setSize(800,500);
jf.show();
jf.setVisible(true);
JScrollPane scrollBar=new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jf.add(scrollBar);
label1=new javax.swing.JLabel("Choose File");
label1.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
label1.setBounds(50,80,200,40);
jf.add(label1);
textfield1=new javax.swing.JTextField();
textfield1.setFont(new Font("Monotype Corsiva", Font.BOLD, 18));
textfield1.setBounds(250,80,200,30);
jf.add(textfield1);
Browse=new javax.swing.JButton("Browse");
Browse.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
Browse.setBounds(480,80,140,30);
jf.add(Browse);
Split=new javax.swing.JButton("Split");
Split.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
Split.setBounds(250,350,140,30);
jf.add(Split);
textArea1=new javax.swing.JTextArea();
textArea1.setFont(new Font("Monotype Corsiva", Font.BOLD, 18));
textArea1.setLineWrap(true);
textArea1.setWrapStyleWord(true);
JScrollPane scrollBar1=new JScrollPane(textArea1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollBar1.setBounds(250,120,200,200);
jf.add(scrollBar1);
}
public void handlingEvents() {
Browse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (fc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) {
String inputFile=fc.getSelectedFile().getPath();
String filename=fc.getSelectedFile().getName();
textfield1.setText(inputFile);
String st1="";
try {
FileReader f = new FileReader(inputFile);
BufferedReader br = new BufferedReader(f);
while((st1 = br.readLine()) != null) {
s = s + st1;
}
f.close();
textArea1.setText(s);
}
catch (IOException e) {
System.out.println(e);
}
}
}
});
Split.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
textArea1.setText("Splitted File Content"+"\n");
int i = 0;
StringTokenizer st = new StringTokenizer(s);
i = st.countTokens();
while (st.hasMoreTokens()) {
textArea1.append(st.nextToken()+"\n");
}
textArea1.append("Total String Tokens="+i);
}
});
}
public static void main(String args[]) {
StringTokenizerTest log = new StringTokenizerTest();
}
}