Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java code, Try to put header in all text file in folder using textarea, please help me

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Java code, Try to put header in all text file in folder using textarea, please help me

    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..


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Java code, Try to put header in all text file in folder using textarea, please help me

    in output files print header all in one line
    It might be that the newline character is being written, but not being displayed properly by whatever program you are using to read the file later. The Windows program Notepad is like this and will often "run lines together".

    The text area (like any JTextComponent instance) has a write() method. You could try using that.

    //bfAll.write(header1.toString());
    text.write(bfAll);
    bfAll.newLine();

    ---

    Here's an example to illustrate the difference:

    import java.io.IOException;
    import java.io.StringWriter;
     
    import javax.swing.JTextArea;
     
    public class NewLineEg {
        public static void main(String[] args) throws IOException {
            JTextArea foo = new JTextArea();
            foo.setText("abc\ndef");
     
            System.out.print("Using getText(): ");
            writeChars(foo.getText());
            System.out.println();
     
            System.out.print("Using write(): ");
            StringWriter sw = new StringWriter();
            foo.write(sw);
            writeChars(sw.toString());
            System.out.println();
        }
     
        private static void writeChars(String str) {
            for(char ch :str.toCharArray()) {
                System.out.print((int)ch);
                System.out.print(" ");
            }
        }    
    }

    Note how the first method represents the newline as (char)10, but the second represents it as the sequence (char)13 (char)10. Notepad will render the first sequence correctly, but not the second. What you get using write() depends on what platform you are using: Linux or Windows etc.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    jayraj (June 15th, 2013)

  4. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java code, Try to put header in all text file in folder using textarea, please help me

    Thanks you so much. I got.. it Thanks you so much once again.. now i understand.. thanks you so much for your explanation.. that's good!!

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Java code, Try to put header in all text file in folder using textarea, please help me

    You're welcome.

Similar Threads

  1. Java code Split text file into multiple text file
    By jayraj in forum What's Wrong With My Code?
    Replies: 26
    Last Post: April 19th, 2013, 06:14 AM
  2. Need help with write to .dat file code from textarea
    By miller4103 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 2nd, 2013, 10:29 PM
  3. Help required in creating header file using java
    By Phoenix23 in forum Java Native Interface
    Replies: 8
    Last Post: March 28th, 2013, 08:05 AM
  4. JAVA code for deleting spaces from Text file
    By Maheshgx in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 17th, 2012, 06:26 AM
  5. Accessing a random text file from within a folder?
    By RaustBD in forum Java Theory & Questions
    Replies: 1
    Last Post: November 29th, 2011, 01:13 PM