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 7897327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:08:21+00:00 2026-06-03T08:08:21+00:00

I need to be able to delete a line based on specific text in

  • 0

I need to be able to delete a line based on specific text in the line (not whole line).

so far this is what i have, but it is deleting the whole file!! i am probably missing a semi colon or something silly.. can some one please help out?

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;


public class DeleteStudent 
{

    public static void removeStudentData()
    {
        File inputFile = new File("StudentsII.txt");
        File tempFile = new File("myTempFile.txt");
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(inputFile));
        } catch (FileNotFoundException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(tempFile));
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        CharSequence sID = null;
        String lineToRemove = (String) sID;
        String currentLine;

        Scanner UI = new Scanner(System.in);
        boolean found = false;

        System.out.println("\n***DELETE STUDENT RECORD***\n");
        System.out.println("Enter a student ID: ");
        sID=UI.next();
        try {
            while((currentLine = reader.readLine()) != null)
            {
                String trimmedLine = currentLine.trim();
                if(trimmedLine.contains(sID))
                    continue;
                try {
                    writer.write(currentLine);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        boolean successful = tempFile.renameTo(inputFile);
        Menu.displayMenu();
    }
}

txt file contains following info…

Martha Stewart 123 Freshman
Cindi Lauper 234 Senior
Tina Turner 345 Freshman
  • 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-03T08:08:22+00:00Added an answer on June 3, 2026 at 8:08 am

    You need to close the streams and files after you’re done writing:

    writer.close();
    tempFile.close();
    //etc.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Java is the key here. I need to be able to delete files but
I have a really large excel file and i need to delete about 20,000
I need to be able to copy a text file to another location and
I usually don't expect help from outside but I need to solve this quickly
This sounds like a relatively simple question, but I haven't been able to get
I have a file contaning for just 2 numubers. One number on eash line.
I need to be able to delete files from S3 that are stored by
I have a file consisting of digits. Usually, each line contains one single number.
I need to be able to delete a user account from a database on
I need to be able to delete messages and to restore them until the

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.