trying to write code to put header in all the text files in folder and create output text file.. in different folder.. I wrote the code.. using textarea put header in it and using string actually its working the only thing is not working is when i put two or more lines in header i mean in textarea. so in output files print header all in one line.. please help me..
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.text.Caret; public class header3 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub // File folder = new File("Letters" + File.separator + "LettersIn"); //System.out.println(folder); //System.out.println(BFFile); final JFrame frame = new JFrame("Enter Header"); JPanel panel = new JPanel(); final JTextArea text = new JTextArea(20,40); JButton button = new JButton("Enter"); frame.add(panel); panel.add(text); panel.add(button); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { BufferedReader br = null; BufferedWriter bfAll = null; File folder = new File("FileIn"); File[] BFFile = folder.listFiles(); //Caret header1 = text.getCaret(); String header1 = text.getText(); System.out.println(header1); text.setLineWrap(true); text.setWrapStyleWord(true); try { for (File file : BFFile) { br = new BufferedReader(new FileReader(file)); String filename = file.getName(); String[] fileInitialName = filename.split("\\."); System.out.println(folder); System.out.println(BFFile); String outFileAllcount = ("FileOut" + File.separator + "out" + fileInitialName[0] + ".txt"); bfAll = new BufferedWriter(new FileWriter(outFileAllcount)); String line; bfAll.write(header1.toString()); bfAll.newLine(); while ((line = br.readLine()) != null) { bfAll.write(line.toString()); bfAll.newLine(); } bfAll.newLine(); bfAll.flush(); bfAll.close(); frame.dispose(); } } catch (Exception e1) { System.out.println("Excepton occured !! : " + e1.getMessage()); } } }); frame.setSize(500,400); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //String header1 = text.getText(); //String header1 = JOptionPane.showInputDialog("here"); } }
here is my code please help me..