Okay as of now I am working on my string builder in my program. If you've read my other posts I have been working away on a copy method.
textField and textField1 are the selected files from my copy method, the source is textField and the target is textField1.
I need it to be
c:\\blahblah\\blahblah\\blah\\
since
c:\blahblah\blah\blah\
wont work.
I've been searching through the stringBuilder.insert commands and I am a bit confused, how would I go about doing what I am wanting to do?
public void copyMethod() { // JOptionPane.showMessageDialog(null, "Copying has commenced, watch the magic happen!"); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.insert(int ); FileInputStream from = null; FileOutputStream to = null; //Size of buffer (too much will crash) int buffSize = 4000; //Start try { from = new FileInputStream(textField.toString()); //Stream from file to = new FileOutputStream(textField1.toString()); //Stream to copy file byte[] charBuffer = new byte[buffSize]; int numRead; //to keep track of where you are in the file //Loop through the file taking in buffSize bytes and copying them to textField1 while((numRead = from.read(charBuffer)) != -1 ) // -1 so no out of bounds to.write(charBuffer, 0, numRead); //This will copy code } catch(Exception Ex){ Ex.printStackTrace(); } }}
Here is my whole program while starting the stringBuilder.
package synch; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class Synch extends JPanel { private JButton setSource = new JButton("Set Source"); private JButton setTarget = new JButton("Set Target"); private JButton copyButton = new JButton("Copy!"); private JTextField textField = new JTextField(" "); private JTextField textField1 = new JTextField(" "); private JFileChooser fc = new JFileChooser(); private JMenuItem Exit, oneWaySynch, twoWaySynch, Help, About, HelpMe; public Synch(){ JFrame prog = new JFrame("Chris's file synchronization"); prog.setSize(500,500); prog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar jmb = new JMenuBar(); prog.setJMenuBar(jmb); JMenu fileMenu = new JMenu("File"); jmb.add(fileMenu); fileMenu.add(oneWaySynch = new JMenuItem(" One way Synch ")); fileMenu.add(twoWaySynch = new JMenuItem(" Two way Synch ")); fileMenu.add(Exit = new JMenuItem("Exit")); JMenu helpMenu = new JMenu("Help"); jmb.add(helpMenu); helpMenu.add(HelpMe = new JMenuItem("Help")); helpMenu.add(About = new JMenuItem("About")); prog.setLayout(new BorderLayout()); JPanel outterPanel = new JPanel(); outterPanel.setLayout(new GridLayout(2,1)); //Set up the top panel of the outer panel JPanel topPannel = new JPanel(); topPannel.add(textField); textField.setPreferredSize(new Dimension(300,30)); topPannel.add(setSource); //Set up the bottom panel of the outer panel JPanel bottomPanel = new JPanel(); bottomPanel.add(textField1); textField1.setPreferredSize(new Dimension(300,30)); bottomPanel.add(setTarget); //Add the top and bottom panels to the outer panel outterPanel.add(topPannel); outterPanel.add(bottomPanel); prog.add(outterPanel, BorderLayout.NORTH); JPanel Pane3 = new JPanel(); fc.setControlButtonsAreShown(false); fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); Pane3.add(fc, FlowLayout.LEFT); prog.add(Pane3, BorderLayout.CENTER); JPanel Pane4 = new JPanel(); Pane4.add(copyButton, FlowLayout.LEFT); prog.add(Pane4, BorderLayout.SOUTH); Exit.addActionListener(new ActionListener(){ public void actionPerformed (ActionEvent e){ exitMethod(); } }); oneWaySynch.addActionListener(new ActionListener(){ public void actionPerformed (ActionEvent e){ oneWayMethod(); } }); twoWaySynch.addActionListener(new ActionListener(){ public void actionPerformed (ActionEvent e){ twoWayMethod(); } }); HelpMe.addActionListener(new ActionListener(){ public void actionPerformed (ActionEvent e){ helpMethod(); } }); About.addActionListener(new ActionListener(){ public void actionPerformed (ActionEvent e){ aboutMethod(); } }); setSource.addActionListener(new ActionListener(){ public void actionPerformed (ActionEvent e){ setSourceMethod(); } }); setTarget.addActionListener(new ActionListener(){ public void actionPerformed (ActionEvent e){ setTargetMethod(); } }); copyButton.addActionListener(new ActionListener(){ public void actionPerformed (ActionEvent e){ copyMethod(); } }); prog.setVisible(true); //Display the window to the user! } //end of method Synch() public void exitMethod() { if(JOptionPane.showConfirmDialog(null,"Are you sure you want it to close?", "Confirm Exit", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION) System.exit(0); } public void oneWayMethod() { //Read the next file in the source directory //See if it exists in the target directory // if it does, check to see if it is EXACTLY the same file // if not copy the file JOptionPane.showMessageDialog(null, "One way sync has been activated."); } public void twoWayMethod() { JOptionPane.showMessageDialog(null, "Two way sync has been activated."); } public void aboutMethod() { JOptionPane.showMessageDialog(null, "This is a file synchronization program, to speed things up, and over all make it easier for you."); } public void helpMethod() { JOptionPane.showMessageDialog(null, "If you are needing assistance please contact me via email: egamespaypal@gmail.com or by phone 1-901-619-9413."); } public void setSourceMethod() { //read the directory that the file chooser is currently sitting on textField.setText(fc.getSelectedFile().toString()); JOptionPane.showMessageDialog(null, "The source has been locked in!"); } public void setTargetMethod() { //read the directory that the file chooser is currently sitting on textField1.setText(fc.getSelectedFile().toString()); JOptionPane.showMessageDialog(null, "The target location has been locked in!"); } public void copyMethod() { // JOptionPane.showMessageDialog(null, "Copying has commenced, watch the magic happen!"); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.insert(int ); FileInputStream from = null; FileOutputStream to = null; //Size of buffer (too much will crash) int buffSize = 4000; //Start try { from = new FileInputStream(textField.toString()); //Stream from file to = new FileOutputStream(textField1.toString()); //Stream to copy file byte[] charBuffer = new byte[buffSize]; int numRead; //to keep track of where you are in the file //Loop through the file taking in buffSize bytes and copying them to textField1 while((numRead = from.read(charBuffer)) != -1 ) // -1 so no out of bounds to.write(charBuffer, 0, numRead); //This will copy code } catch(Exception Ex){ Ex.printStackTrace(); } }}