Hello,

here is a code for retrieving some lines from a text file.
i'd like to extract data from line2 to line4 ---> this is working
but in the same time i want to data from Line10 to line12

here is the code, my conditions is not working:

Thank you

import java.io.*;

public class ReadSpecificLine
{
    public static void main(String[] args)
    {
        StringBuffer buffer = new StringBuffer();
        String line = "";
        int lineNo=0;
            try
            {
            LineNumberReader ln = new LineNumberReader(new FileReader("C://Source/Source.txt"));
            int count = 0;
            while (ln.readLine() != null) {
                count++;
            }
            ln.close();
            FileReader fr = new FileReader("C://Source/Source.txt");
            BufferedReader br = new BufferedReader(fr);
            for (lineNo = 1; lineNo <= count; lineNo++){
                if (lineNo == 2)
                {
                    for (lineNo = 2; lineNo <= 4; lineNo++) && (lineNo = 10; lineNo<=14; lineNo++)
                  
                    {
                        buffer.append(br.readLine());
                        //buffer.append("\n");
                    }

                    

                } else
                    br.readLine();
            }
           
        BufferedWriter bw=new BufferedWriter(new FileWriter(new File("C://Target/Target.txt"),true));
        bw.write(buffer.toString());
        bw.newLine();
        bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

I'm confused about what you are actually asking, but why are you opening the file, looping through all the elements to get the line number:

while (ln.readLine() != null) {
    count++;
}

closing the file, then opening it and then looping thorugh every line again (missing the first one):

for (lineNo = 1; lineNo <= count; lineNo++){
    //...
}

Just simply do a

//pseudo code
while(readline)
  switch(++count)
    case 2:
    case 3:
    case 4:
        do something and break
    case 10:
    case 11:
    case 12:
        do something break
    default:

Ok but i f i remove LineNo++ in for loop
i have en error message like:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at java.util.Arrays.copyOf(Arrays.java:2882)
        at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
        at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:390)
        at java.lang.StringBuffer.append(StringBuffer.java:224)
        at ReadSpecificLine.main(ReadSpecificLine.java:27)
Java Result: 1

i think i have to use && or add another block with if-else

thank you

Lets see your implementation of that while/switch.

here is the code but :

import java.io.*;

public class ReadSpecificLine
{
    public static void main(String[] args)
    {
        StringBuffer buffer = new StringBuffer();
        String line = "";
        int lineNo=0;
            try
            {
            LineNumberReader ln = new LineNumberReader(new FileReader("C://Source/Source.txt"));
            int count = 0;
            while (ln.readLine() != null) 
                switch
            
                (count++);
           
            {
                
            
            ln.close();
            FileReader fr = new FileReader("C://Source/Source.txt");
            BufferedReader br = new BufferedReader(fr);
            for (lineNo = 1; lineNo <= count; lineNo++){
               case 1:
                         (lineNo == 2)
                {
                    for (lineNo = 2; lineNo <= 4; lineNo++)
                  
                    {
                        buffer.append(br.readLine());
                        //buffer.append("\n");
                    }
                  
                } 
                
            case 2:
                
                
                if (lineNo == 10)
                {
                    for (lineNo = 10; lineNo <= 14; lineNo++)
                  
                    {
                        buffer.append(br.readLine());
                        //buffer.append("\n");
                    }
                  
                } 
                
         
                default:
                    br.readLine();
            }

        BufferedWriter bw=new BufferedWriter(new FileWriter(new File("C://Target/Target.txt"),true));
        bw.write(buffer.toString());
        bw.newLine();
        bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

What do you think the 2,3,4 and 10,11,12 are? Especially when combined with the description from the first post? And why do you still have a for loop there? Where is there a for loop shown in the pseudocode? IOW, those "do something" blocks need only be the buffer.append stuff (and a break, of course) and all that other bs you can get rid of.

it not working, i give up.


thanks

import java.io.*;
import java.lang.String.*;

public class ReadSpecificLine
{
    public static void main(String[] args)
    {
        StringBuffer buffer = new StringBuffer();
        String line = "";
        int lineNo=0;
            try
            {
            LineNumberReader ln = new LineNumberReader(new FileReader("C://Source/Source.txt"));
            int count = 0;
            while (br.readLine())
            

            {
                (count++);
            }

                


            ln.close();
            FileReader fr = new FileReader("C://Source/Source.txt");
            BufferedReader br = new BufferedReader(fr);

            switch (lineNo = 1; lineNo <= count; lineNo++)
            {
               case 1:
                   if (lineNo == 2)
                {
                    for (lineNo = 2; lineNo <= 4; lineNo++)

                    {
                        buffer.append(br.readLine());
                        //buffer.append("\n");
                    }

                }

                break;
            case 2:


                    if (lineNo == 10)
                {
                    for (lineNo = 10; lineNo <= 14; lineNo++)

                    {
                        buffer.append(br.readLine());
                        //buffer.append("\n");
                    }

                }
                break;

                default:
                    br.readLine();
            }

        BufferedWriter bw=new BufferedWriter(new FileWriter(new File("C://Target/Target.txt"),true));
        bw.write(buffer.toString());
        bw.newLine();
        bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 }

Finally, after sometimes i finished to find the solution.
Here is it:

import java.io.*;

public class ReadSpecificLine
{
    public static void main(String[] args)
    {
        StringBuffer buffer = new StringBuffer();
        String line = "";
        int lineNo=0;
            try
            {
            LineNumberReader ln = new LineNumberReader(new FileReader("C://Temp/FileA.txt"));
            int count = 0;
            while (ln.readLine() != null)
            {
                count++;
            }
            ln.close();
            FileReader fr = new FileReader("C://Temp/FileA.txt");
            BufferedReader br = new BufferedReader(fr);
            for (lineNo = 1; lineNo <= count; lineNo++)
            {
                if (lineNo == 2)
                {
                    for (lineNo = 2; lineNo <= 4; lineNo++)
                    {
                        buffer.append(br.readLine());
                        buffer.append("\n");
                    }
               
                }

                if (lineNo == 10)
                {
                    for (lineNo = 10; lineNo <= 14; lineNo++)
                    {
                        buffer.append(br.readLine());
                       buffer.append("\n");
                    }
                }                
                else
                    br.readLine();
            }

        BufferedWriter bw=new BufferedWriter(new FileWriter(new File("C://Temp2/Save.txt"),true));
        bw.write(buffer.toString());
        bw.newLine();
        bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

but i have another with "\n" this line put all data in one line. why?
I want a line by line copy like a text.

Thank you

Because readLine hacks off the newline (as the API docs state). See the API docs for BufferedWriter for a solution. And, reading the file twice is still extremely ineffecient. Also, the solution with the switch doesn't need if statements, at all, and you read the file only once.

Ok Masijade but if we put "\r\n" it works well.
Thank you very much for your Help.

And that is the wrong thing to do. What is wrong with the newLine method of BufferedWriter (which is what you're using)? I told you to look at the API docs for a reason. Why didn't you? You need to not only learn how to use the API docs (and tutorials, etc), but to also actually use them.

Hi Masijade,

the problem with newLine is when it copies for example one line it didn't go to the new line, the program
puts the second line juster after the first in one line.
So when i try to copy several blocks of lines on different place of my text file, in the output file all datas are stored in one line.

And another problem is, my text file contents 20000 files its size is 1250Ko.
When i tried to copy 10 blocks of 12 lines on differents place of my text file, the output contains nothing and its size is 1Ko.

ex : i try copy data from 4655 to 6000
8000 to 15000
....etc


But if i tried to copy a block of 12 lines within 500 lines i mean i don't try to read the file until 20000 lines if i read before 100 lines it works well.

I think i have a memory allocation problem or something like that.

What i don't get is my text file ' size is only 1250Ko it's not really big but it contains 20000 lines it 's just simple text with characters and numbers.
it's strange for me coz i'm beginner i suppose.

i 'm looking for the javadoc as you mentioned.

Thank you

You do

writer.write(string);
writer.newLine();

line for line.
Is that how you did it? Somehow, I don't think so.

If you insist on "caching" the output in a String and writing it all at once rather than simply writing it as you read it, then at least use

System.getProperty("line.separator")

rather than hard-coding a platform dependent line-ending.

Edit: BTW, your "memory allocation problem" is solved if you write as you read. There is no need to cache the info. The API docs.

Hi Masijade,

yes i did as you mentioned for
writer.write(string);
writer.newLine();

And for the API it's very big there is too many thing to see, i don't know with what i have to start
to read in the APi for solving my problem.

In java I/O maybe i have to use the java.nio
i don't know give a clue please.

And even i don't know if my code is right for what i want, maybe i should go for something just to read a file and delete lines which start with a specific character or number.
it could be easier...

because my actual code is not really nice to see.

Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.