Hi,

i know how to read specific line form a text file but if we want to delete lines by specifying the line number.

here is the code, but it's not working:
i want to delete line 1 and 2 of the text file.

import java.io.*;

public class ReadSpecificLine
{

public static void main(String[] args){
String line = "";
int lineNo;

try
 {
FileReader fr = new FileReader("C://Temp/File.txt");
BufferedReader br = new BufferedReader(fr);
for(lineNo=1;lineNo<3;lineNo++)
{

while (lineNo = 1&&2, br.readLine() !=null);
 
}

br.readLine();
}
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println("Line: " + line);
}
}

Thank you

Recommended Answers

All 7 Replies

I tried to compile your code, but as I expected line 17 complains. besides, = is not a comparison operator, I assumed you meant ==. check that first!

I replaced Line17 by this

while (lineNo == 1)&&(lineNo == 2);
br.readLine =null ;

but still i got errors.

you need an || (or operator) inside your loop, not an and operator. plus, in this code you posted you need two more sets of brackets, although I think you can do without the curly ones:

while ((lineNo == 1) || (lineNo == 2)){
br.readLine =null ;
}

try this know, and see if it works

thank you Bibiki,
here is my modified code now.
i can read some specific line from my file.
it's working good, but now how could i delete them.

Here i can read text from line 15 to 20
and form line 30 to 40.

these lines must be removed completely from my file.

import java.io.*;

public class ReadSpecificLine2

{
public static void main(String[] args) throws IOException
{
	BufferedReader in = new BufferedReader (new FileReader("C://Temp/File.txt"));

	String info = "";
	int startLine = 15;
	int endLine = 20;
        int startLine2 = 30;
        int endLine2 = 40;
	
	for (int i = 0; i < startLine; i++)
        {
            info = in.readLine();
        }

	for (int i = startLine; i < endLine + 1; i++)
        {
		info = in.readLine();
		System.out.println(info);
	}

        
        for (int i = 0; i < startLine2; i++)
        {
            info = in.readLine();
        }

	for (int i = startLine2; i < endLine2 + 1; i++)
        {
		info = in.readLine();
		System.out.println(info);
	}




	in.close();
}}

the goal for me is to delete these lines automatically
actually i delete lines manually but the file content more than 20000 lines.
and i have to delete 10 lines at 10 specific place in the text, maybe my code is not nice to see.

thank you Bibiki

I don't think there is any way to delete lines in the middle of a file. What you can do is to copy the file to a new file, only writing the lines you want to keep.

i think each line is read as a single string. as a result, you could temporarily store your file in a String array, each line, one element on the array. and then, you put back the strings from your array to the file, excluding the ones you do not need on the file. that just means that you need first to open your file, count how many lines it has. initialize an array with that many elements on it. store file lines on the array. reopen your file, but now on writable mode (but not appendable), and put the strings from your array back on the file. hope this helps.

ok thank you bibiki,
i think i have the solution from an old thread of:
Re: Redirecting console output to file from NPH on May 19th, 2005

i will put the solution if t's good
Thanks a lot.

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.