Results 1 to 4 of 4
  1. #1
    nabilabid is offline Member
    Join Date
    Dec 2015
    Posts
    9
    Rep Power
    0

    Default how to concatenate lines from different files?

    Hi every body,
    i am trying to write script to concatenate lines from different text files. the output will be a new text file.
    i take as example two files as input.

    file1:
    Java Code:
    ]>FJ747620
    aaaaaaaaaa
    >FJ747631
    aaaaaaaaaa
    >JF490085
    aaaaaaaaaa
    >JF490095
    aaaaaaaaaa
    file2:
    Java Code:
    >FJ747620
    bbbbbbbbbb
    >FJ747631
    bbbbbbbbbb
    >JF490085
    bbbbbbbbbb
    >JF490095
    bbbbbbbbbb
    i want to concatenate lines which not begin with '>'. the output must be as follows:
    Java Code:
    >FJ747620
    aaaaaaaaaabbbbbbbbbb
    >FJ747631
    aaaaaaaaaabbbbbbbbbb
    >JF490085
    aaaaaaaaaabbbbbbbbbb
    >JF490095
    aaaaaaaaaabbbbbbbbbb
    i wrote a script but when i run it, i obtain: just the file 1 has duplicated its lines
    Java Code:
    >FJ747620
    aaaaaaaaaa[B]aaaaaaaaaa[/B]
    >FJ747631
    aaaaaaaaaa[B]aaaaaaaaaa[/B]
    >JF490085
    aaaaaaaaaa[B]aaaaaaaaaa[/B]
    >JF490095
    aaaaaaaaaa[B]aaaaaaaaaa[/B]
    my script is:
    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FilenameFilter;
    import java.io.PrintWriter;
    
    
    
    public class Concatenate {
    	public static void main(String[] args) throws Exception{
    
    		FilenameFilter filter = new FilenameFilter() {
    		    public boolean accept(File dir, String name) {
    		        return name.endsWith(".txt");
    		    }
    		};
    
    		File folder = new File("C:/workspace/Abid/concatenate");
    		File[] listOfFiles = folder.listFiles(filter);
    		PrintWriter output = new PrintWriter(new File("ConcatenateSequence.txt"));
    		for (int i = 0; i < listOfFiles.length; i++) {
    		    File file = listOfFiles[i];
    		    FileReader fr = new FileReader(file);
    		    BufferedReader BufferedReader = new BufferedReader(fr);
    		    String str;
    		    while(BufferedReader.ready() && (str = BufferedReader.readLine()) != null){
    		    if(str.startsWith(">")){
    		    	output.println(str);
    		    }
    		    else{
    		    	output.println(str + str);
    		    }
    		    }
    		    BufferedReader.close();
    		    output.close();
    		}
    	}
    }
    if someone can tell me what's wrong in my script.
    Thank you.
    Last edited by JosAH; 12-10-2015 at 08:40 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: how to concatenate lines from different files?

    I changed your 'quote' tags to 'code' tags again. quote tags don't preserve program formatting, while code tags do ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: how to concatenate lines from different files?

    What do you think this does?

    Java Code:
    	output.println(str + str);
    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: how to concatenate lines from different files?

    You're only opening one file at a time.
    Just as a couple of comment,s you don't need to do BufferedReader.ready().
    Also your variable names should start with a lower case letter, so that should be bufferedReader. Otherwise it looks like you're trying to call a static method on the class itself.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 13
    Last Post: 12-08-2015, 01:19 AM
  2. APP: Concatenate images - H E L P
    By irreconhecivell in forum Advanced Java
    Replies: 8
    Last Post: 10-31-2014, 12:26 AM
  3. how to concatenate two arraylists in a third one
    By nanees in forum New To Java
    Replies: 14
    Last Post: 03-31-2011, 06:08 PM
  4. How to concatenate a number
    By jim01 in forum New To Java
    Replies: 4
    Last Post: 03-27-2011, 04:31 AM
  5. problem reading a files lines
    By j187 in forum New To Java
    Replies: 1
    Last Post: 12-17-2010, 01:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •