#how delete a specific line in a file in java

177 messages · Page 1 of 1 (latest)

velvet bramble
#

how delete a specific line in a file in java

sturdy tangleBOT
#

This post has been reserved for your question.

Hey @velvet bramble! Please use /close or the Close Post button 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.

rotund ore
#

this thread has a few examples

#

make sure u read the comments

velvet bramble
#

Ok then Thank you Sir!

sturdy tangleBOT
# velvet bramble 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.

pastel spoke
#

you basically copy from the file to change to a temporary file

velvet bramble
#

Check #general

pastel spoke
#

nah, this channel is fine

velvet bramble
#

oh lol

#

ok gonna resend

pastel spoke
#

so you just copy all lines from the input file to the temporary file except the one you want to remove

velvet bramble
#

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 ?

pastel spoke
velvet bramble
#

bruh i ping the dude 2 times 😭

pastel spoke
#

The goal is to create a new file with the target content

#

and then you replace the original file

velvet bramble
#

oh

#

this only way?

#

can't i just delete the line

pastel spoke
#

not the only way

pastel spoke
#

the OS doesn't have that concept

#

The operating system requires files to be continuous

velvet bramble
#

Bufferreader is the OS?

pastel spoke
#

no

velvet bramble
#

oh

pastel spoke
#

OS=operating system

velvet bramble
#

Os is operating system

pastel spoke
#

Java runs on top of the OS

velvet bramble
#

got it

pastel spoke
#

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

velvet bramble
#

Werid way

pastel spoke
#

alternatively, you could find the position of the file to remove

#

and then move everything up

velvet bramble
#

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

pastel spoke
#

I'm working on something with RandomAccessFile

velvet bramble
#

oh

#

og

#

oh

#

This Cool

#

No need bufferreader and bufferwriter

pastel spoke
#
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
}
velvet bramble
#

The guy typing easy

#

oh

#

i see

#

let me read frist

#

why we need byte[] buffer = new byte[1024];

pastel spoke
velvet bramble
#

oh

pastel spoke
#

let me add a few comments

velvet bramble
#

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?

pastel spoke
#

(I forgot the ! there - I added it to the code now)

velvet bramble
#

yep

#

i added .equals

pastel spoke
#

I think the first loop is clear, right?

velvet bramble
pastel spoke
velvet bramble
#

like

#

i will make if

#

if they see the line have pvp:

#

change it do Pvp: true

#

so

#

oh

velvet bramble
#

i see

#

i understanding now

pastel spoke
#

after the first line, you have this situation

#

ignore the one arrow pointing in the wrong direction

velvet bramble
#

right?

pastel spoke
#

the position of the start and end of the line

velvet bramble
#

Oh

#

I See

#

this weird

#

in python you could remove and add text in like 3 lines Skull_pet_RIP

#

Ok anwyays

#

We Now

#

find line

#

skiped line

#

good

pastel spoke
#

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

velvet bramble
#

I see

#

Amazing

pastel spoke
#

then you take that buffer and move it back to the beginning of the line

velvet bramble
#

file.write(buffer, 0, read);?

#

oh

pastel spoke
#

the variable read stores the actual number of bytes that have been read

velvet bramble
#

and this oldPosition+=read; newPosition+=read;

pastel spoke
#

this is the next step

velvet bramble
#

;o

#

what is the next step

pastel spoke
#

so you have these lines on the right

velvet bramble
#

yep

#

buffer

#

wait

#

no

#

i mean

#

old and new postion

pastel spoke
#

you move these positions

velvet bramble
#

ooh

#

ok

#

we check all lines

#

skip line

pastel spoke
#

so that oldPosition points to the end of the stuff you have written

velvet bramble
#

add it so buffer bypte

#

then make new file

#

and add it there

#

i see

pastel spoke
#

and newPosition points where you stopped reading

#

if you have moved 1024 bytes, you add 1024 bytes to both

velvet bramble
#

yep

#

now

#

we removed the line :P

pastel spoke
#

if you have moved 512 bytes, you add 512 bytes to both

#

but then you are not finished

velvet bramble
#

like

#

werid

pastel spoke
velvet bramble
#

bruh

#

so small

pastel spoke
#

you also need to move the remainder of the file

pastel spoke
velvet bramble
pastel spoke
#

so you have 1024*8 bits of memory at once

velvet bramble
pastel spoke
#

and then you move the next part of the file

velvet bramble
#

i see

#

now what next step

pastel spoke
velvet bramble
#

oh

#

we delete the other file?

pastel spoke
#

yeah you just continue

#

and repeat

#

you move the rest of the file up

#

sry I got to go

velvet bramble
#

I see

#

now you removed the line

#

:D

#

Thank you For explain everything

sturdy tangleBOT
# velvet bramble Thank you For explain everything

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.