Created
September 25, 2018 14:29
-
-
Save xyzshantaram/d786959d00d95084cf734950d7c2b225 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package FileSearch; | |
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import javax.swing.JOptionPane; | |
/** | |
* | |
* @author Student | |
*/ | |
public class FileIO { | |
static String fileName = "foo"; | |
public FileIO(String s) { | |
fileName = s; | |
} | |
static void SOP(String s) { | |
System.out.println(s); | |
} | |
static void dumpToFile(ArrayList<String> array) { | |
boolean success = false; | |
try { | |
BufferedWriter fw = new BufferedWriter(new FileWriter(fileName)); | |
int k = 0; | |
while (k < array.size()) { | |
fw.write(array.get(k)); | |
SOP(array.get(k)); | |
fw.newLine(); | |
k += 1; | |
} | |
fw.close(); | |
success = true; | |
if (success) { | |
SOP("Successfully saved file."); | |
} | |
else { | |
SOP("Save failed."); | |
} | |
} | |
catch (IOException e) { | |
SOP("IOException: " + e); | |
success = false; | |
} | |
} | |
static String getFromLineNo(int number, ArrayList<String> array) { | |
String obtainedLine; | |
obtainedLine = array.get(number); | |
return obtainedLine; | |
} | |
static void message(String s) { | |
JOptionPane.showMessageDialog(null, s); | |
} | |
static int searchForLineNumber(String s, ArrayList<String> array, int lines) { | |
int lnNo = 0; | |
while (lnNo <= lines) { | |
if (array.get(lnNo).equals(s)) { | |
break; | |
} | |
else { | |
} | |
lnNo++; | |
} | |
return lnNo; | |
} | |
static void readFileIntoArray(ArrayList<String> array, int lines) { | |
try { | |
FileReader fr = new FileReader(fileName); | |
BufferedReader br = new BufferedReader(fr); | |
while (br.readLine() != null) { | |
lines++; | |
} | |
br.close(); | |
fr.close(); | |
fr = new FileReader(fileName); | |
br = new BufferedReader(fr); | |
for (int j = 0; j < lines; j++) { | |
array.add(j, br.readLine()); | |
FileIO.SOP(array.get(j)); | |
} | |
br.close(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package FileSearch; | |
import java.util.ArrayList; | |
import javax.swing.JOptionPane; | |
/** | |
* | |
* @author Student | |
*/ | |
public class SearchFrame extends javax.swing.JFrame { | |
ArrayList<String> arr = new ArrayList(); | |
int lineCounter = 0; | |
FileIO IOReadWrite = new FileIO("filename.txt"); | |
public SearchFrame() { | |
initComponents(); | |
IOReadWrite.readFileIntoArray(arr, lineCounter); | |
} | |
boolean dumpSuccess = false; | |
/** | |
* This method is called from within the constructor to initialize the form. | |
* WARNING: Do NOT modify this code. The content of this method is always | |
* regenerated by the Form Editor. | |
*/ | |
@SuppressWarnings("unchecked") | |
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents | |
private void initComponents() { | |
searchField = new javax.swing.JTextField(); | |
searchBtn = new javax.swing.JButton(); | |
jLabel2 = new javax.swing.JLabel(); | |
addField = new javax.swing.JTextField(); | |
jLabel3 = new javax.swing.JLabel(); | |
addBtn = new javax.swing.JButton(); | |
saveBtn = new javax.swing.JButton(); | |
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); | |
searchBtn.setText("Search"); | |
searchBtn.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
searchBtnActionPerformed(evt); | |
} | |
}); | |
jLabel2.setText("Search"); | |
jLabel3.setText("Add Line"); | |
addBtn.setText("Add"); | |
addBtn.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
addBtnActionPerformed(evt); | |
} | |
}); | |
saveBtn.setText("Save"); | |
saveBtn.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
saveBtnActionPerformed(evt); | |
} | |
}); | |
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); | |
getContentPane().setLayout(layout); | |
layout.setHorizontalGroup( | |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() | |
.addGap(0, 80, Short.MAX_VALUE) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createSequentialGroup() | |
.addGap(14, 14, 14) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addComponent(jLabel3) | |
.addComponent(jLabel2)) | |
.addGap(34, 34, 34) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) | |
.addComponent(searchField, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addComponent(addField, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))) | |
.addGroup(layout.createSequentialGroup() | |
.addComponent(searchBtn) | |
.addGap(37, 37, 37) | |
.addComponent(addBtn) | |
.addGap(35, 35, 35) | |
.addComponent(saveBtn))) | |
.addGap(75, 75, 75)) | |
); | |
layout.setVerticalGroup( | |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createSequentialGroup() | |
.addGap(56, 56, 56) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
.addComponent(addField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addComponent(jLabel3)) | |
.addGap(18, 18, 18) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
.addComponent(searchField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addComponent(jLabel2)) | |
.addGap(18, 18, 18) | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
.addComponent(searchBtn) | |
.addComponent(addBtn) | |
.addComponent(saveBtn)) | |
.addContainerGap(47, Short.MAX_VALUE)) | |
); | |
pack(); | |
}// </editor-fold>//GEN-END:initComponents | |
private void searchBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchBtnActionPerformed | |
// TODO add your handling code here: | |
String searchString = searchField.getText(); | |
int k = IOReadWrite.searchForLineNumber(searchString, arr, lineCounter); | |
k = k + 1; | |
JOptionPane.showMessageDialog(this, "Match found on line " + k); | |
}//GEN-LAST:event_searchBtnActionPerformed | |
private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBtnActionPerformed | |
// TODO add your handling code here: | |
IOReadWrite.dumpToFile(arr); | |
System.exit(0); | |
}//GEN-LAST:event_saveBtnActionPerformed | |
private void addBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addBtnActionPerformed | |
// TODO add your handling code here: | |
String addFieldText = addField.getText(); | |
IOReadWrite.SOP(addFieldText); | |
arr.add(addFieldText); | |
}//GEN-LAST:event_addBtnActionPerformed | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String args[]) { | |
/* Set the Nimbus look and feel */ | |
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> | |
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. | |
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html | |
*/ | |
try { | |
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { | |
if ("Nimbus".equals(info.getName())) { | |
javax.swing.UIManager.setLookAndFeel(info.getClassName()); | |
break; | |
} | |
} | |
} catch (ClassNotFoundException ex) { | |
java.util.logging.Logger.getLogger(SearchFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (InstantiationException ex) { | |
java.util.logging.Logger.getLogger(SearchFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (IllegalAccessException ex) { | |
java.util.logging.Logger.getLogger(SearchFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (javax.swing.UnsupportedLookAndFeelException ex) { | |
java.util.logging.Logger.getLogger(SearchFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} | |
//</editor-fold> | |
/* Create and display the form */ | |
java.awt.EventQueue.invokeLater(new Runnable() { | |
public void run() { | |
new SearchFrame().setVisible(true); | |
} | |
}); | |
} | |
// Variables declaration - do not modify//GEN-BEGIN:variables | |
private javax.swing.JButton addBtn; | |
private javax.swing.JTextField addField; | |
private javax.swing.JLabel jLabel2; | |
private javax.swing.JLabel jLabel3; | |
private javax.swing.JButton saveBtn; | |
private javax.swing.JButton searchBtn; | |
private javax.swing.JTextField searchField; | |
// End of variables declaration//GEN-END:variables | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment