#how delete a specific line in a file in java
177 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @velvet bramble! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
this thread has a few examples
make sure u read the comments
Ok then Thank you Sir!
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
you basically copy from the file to change to a temporary file
Check #general
nah, this channel is fine
so you just copy all lines from the input file to the temporary file except the one you want to remove
But i Don't get it
He open 2 files reader and writer
then what
oh
Ok i think i gonna open file reader
when loop until fine the line
then delete the line
right @pastel spoke | Daniel ?
and then you replace the file to change
bruh i ping the dude 2 times 😭
Yep
this what i saying here ig
not deleting the line but skipping it
The goal is to create a new file with the target content
and then you replace the original file
not the only way
No, you can't
the OS doesn't have that concept
The operating system requires files to be continuous
Bufferreader is the OS?
no
oh
OS=operating system
Os is operating system
Java runs on top of the OS
got it
if these are the files
and you want to remove a line
then you need to move all lines after that
the easiest approach is the one from Stack Overflow
Werid way
alternatively, you could find the position of the file to remove
and then move everything up
this will take alot of time to load ;/
Java Is Taking all Class and Load it right not like python that check every line?
ok then
ok
So this how the code gonna work
we Create File
with same name
Then We Copy All Contant
of the old file
to new one
but when we find the line that we want change
we just skip it
and put the new line
I'm working on something with RandomAccessFile
try(RandomAccessFile file = new RandomAccessFile(yourFilePath, "rw")){
String line;
long oldPosition;
long newPosition=0;
//find the removed line, we need the start and end of that line
do{
oldPosition = newPosition;//start of the line
line = file.readLine();
newPosition=file.getFilePointer();//end of the line
}while(!isThisTheLineToRemove(line));//continue reading without changing anything until you reach the line you want to remove
//move content of the file
byte[] buffer = new byte[1024];//temporary buffer where be put the stuff to move
int read;//number of bytes read, this is -1 when the end of the file is reached
while((read = file.read(buffer))!=-1){//loop until you reach the end of the file and read the content into buffer
file.seek(oldPosition);//go to the position where you stopped writing - at the beginning, this is the start of the line to remove
file.write(buffer, 0, read);//take the read data and write it back at the correct position
oldPosition+=read;//increment both positions
newPosition+=read;//increment both positions
file.seek(newPosition);//move to the position you are reading
}
file.setLength(oldPosition);//remove the end of the file
}
The guy typing easy
oh
i see
let me read frist
why we need byte[] buffer = new byte[1024];
so you are doing this
oh
let me add a few comments
byte should be 1024?
Ok!
take your time
btw isThisTheLineToRemove( could i just write small part of the line like line = hi= nice could i just write hi=
and it delete the rest of the line?
Sir?
If you want to remove the first line containing REMOVEME, this could be something like !"REMOVEME".equals(line)
(I forgot the ! there - I added it to the code now)
I think the first loop is clear, right?
not frist line like 6 line
ah yes
?
like
i will make if
if they see the line have pvp:
change it do Pvp: true
so
oh
this skip line
i see
i understanding now
after the first line, you have this situation
ignore the one arrow pointing in the wrong direction
toremove = line will remove
old postion = old file
new postion = new file
right?
lol
no, these are positions in the file
the position of the start and end of the line
Oh
I See
this weird
in python you could remove and add text in like 3 lines 
Ok anwyays
We Now
find line
skiped line
good
the next step is you copy the next 1024 bytes after the line to a buffer
this is the byte[]
it doesn't need to be 1024 bytes
I just chose that
then you take that buffer and move it back to the beginning of the line
this is exactly what this image does
the variable read stores the actual number of bytes that have been read
and this oldPosition+=read; newPosition+=read;
this is the next step
so you have these lines on the right
you move these positions
so that oldPosition points to the end of the stuff you have written
i see
and newPosition points where you stopped reading
if you have moved 1024 bytes, you add 1024 bytes to both
if you have moved 512 bytes, you add 512 bytes to both
but then you are not finished
what is bytes
like
werid
a byte is 8 bits of memory
you also need to move the remainder of the file
this is why we are moving 1024 bytes at once
so you have 1024*8 bits of memory at once
eh
and then you move the next part of the file
yeah you just continue
and repeat
you move the rest of the file up
sry I got to go
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.