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

  • SEARCH
  • Home
  • 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 8782919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:41:31+00:00 2026-06-13T20:41:31+00:00

Possible Duplicate: Replace first line of a text file in Java Java – Find

  • 0

Possible Duplicate:
Replace first line of a text file in Java
Java – Find a line in a file and remove

I am trying to find a way to remove the first line of text in a text file using java. Would like to use a scanner to do it…is there a good way to do it without the need of a tmp file?

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-13T20:41:33+00:00Added an answer on June 13, 2026 at 8:41 pm
    Scanner fileScanner = new Scanner(myFile);
    fileScanner.nextLine();
    

    This will return the first line of text from the file and discard it because you don’t store it anywhere.

    To overwrite your existing file:

    FileWriter fileStream = new FileWriter("my/path/for/file.txt");
    BufferedWriter out = new BufferedWriter(fileStream);
    while(fileScanner.hasNextLine()) {
        String next = fileScanner.nextLine();
        if(next.equals("\n")) 
           out.newLine();
        else 
           out.write(next);
        out.newLine();   
    }
    out.close();
    

    Note that you will have to be catching and handling some IOExceptions this way. Also, the if()... else()... statement is necessary in the while() loop to keep any line breaks present in your text file.

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

Sidebar

Related Questions

Possible Duplicate: Using Emacs to recursively find and replace in text files not already
Possible Duplicate: Why does javascript replace only first instance when using replace? I have
Possible Duplicate: Replace only first match using preg_replace I have a string as follows:
Possible Duplicate: Java Regex Replace with Capturing Group Is there any way to replace
Possible Duplicate: jQuery change input type How to replace input[type=submit] to input[type=text] using jQuery?
Possible Duplicate: Find and Replace more than 1 word? I want to use jQuery
Possible Duplicate: php preg_replace \ I am trying to replace all occurances of the
Possible Duplicate: How do I write a regex to find and replace मेरा नाम
Possible Duplicate: Regex exec only returning first match a1b2c3d.replace(/[0-9]/g,x) returns axbxdxd as expected. /[0-9]/g.exec(a1b2c3d)
Possible Duplicate: replace multiple placeholders with php? I've got a .txt-file working as a

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.