Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8603103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:14:43+00:00 2026-06-12T02:14:43+00:00

I mean , I want to delete line from my text on android. How

  • 0

I mean , I want to delete line from my text on android. How can I delete?
I do not want to read one txt and create another with removing line. I want to delete line from my existing txt.
thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:14:44+00:00Added an answer on June 12, 2026 at 2:14 am

    This is a pretty tricky problem, despite it looking a trivial one. In case of variable lines length, maybe your only option is reading the file line by line to indentify offset and length of the target line. Then copying the following portion of the file starting at offset, eventually truncating the file lenght to its original size minus the the target line’s length. I use a RandomAccessFile to access the internal pointer and also read by lines.

    This program requires two command line arguments:

    • args[0] is the filename
    • args[1] is the target line number (1-based: first line is #1)
    public class RemoveLine {
        public static void main(String[] args) throws IOException {
            // Use a random access file
            RandomAccessFile file = new RandomAccessFile(args[0], "rw");
            int counter = 0, target = Integer.parseInt(args[1]);
            long offset = 0, length = 0;
    
            while (file.readLine() != null) {
                counter++;
                if (counter == target)
                    break; // Found target line's offset
                offset = file.getFilePointer();
            }
    
            length = file.getFilePointer() - offset;
    
            if (target > counter) {
                file.close();
                throw new IOException("No such line!");
            }
    
            byte[] buffer = new byte[4096];
            int read = -1; // will store byte reads from file.read()
            while ((read = file.read(buffer)) > -1){
                file.seek(file.getFilePointer() - read - length);
                file.write(buffer, 0, read);
                file.seek(file.getFilePointer() + length);
            }
            file.setLength(file.length() - length); //truncate by length
            file.close();
        }
    }
    

    Here is the full code, including a JUnit test case. The advantage of using this solution is that it should be fully scalable with respect to memory, ie since it uses a fixed buffer, its memory requirements are predictable and don’t change according to the input file size.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I mean I have one line and I want to put 2 spinner in
I want to run a python script from within another. By within I mean
How can I make layout almost fullscreen ? I mean I dont want there
How can I return the key?, mean if I want to allow only integer
I want to replace all displaying text with something like @@@@. It mean user
In a mini blog app, I want to create a delete function, so that
I want to know on Indexes. I want to create an index on one
I want to delete category from multilevel menu with recursion function. To understand situation
I want to draw a multiline (I mean some consecutive lines that share one
I want to know what does RETURN_VALUE mean! I'm stuck at this thing. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.