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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:00:53+00:00 2026-05-23T16:00:53+00:00

private static void deleteProxy(File proxyOld, String host, int port) { try { String lines,

  • 0
private static void deleteProxy(File proxyOld, String host, int port) {
        try {
            String lines, tempAdd;
            boolean removeLine = false;
            File proxyNew = new File("proxies_" + "cleaner$tmp");
            BufferedReader fileStream = new BufferedReader(new InputStreamReader(new FileInputStream(proxyOld)));
            BufferedWriter replace = new BufferedWriter(new FileWriter(proxyNew));
            while ((lines = fileStream.readLine()) != null) {
                tempAdd = lines.trim();
                if (lines.trim().equals(host + ":" + port)) {
                    removeLine = true;
                }
                if (!removeLine) {
                    replace.write(tempAdd);
                    replace.newLine();
                }
            }
            fileStream.close();
            replace.close();
            proxyOld.delete();
            proxyNew.renameTo(proxyOld);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Calling the function:

File x = new File("proxies.txt");//is calling a new file the reason why it's being flushed out?
deleteProxy(x, host, port);

Before I run the program the file proxies.txt had data inside of it. However when I run the program it appears to be flushed out. It becomes empty.

I noticed while the program is running, if I move my mouse over the file proxies.txt, Windows displays the "Date Modified" and the time it displays is the current time, or last time the function deleteProxy(...) was executed.

Does anyone know what I’m doing wrong? And why won’t the list update instead of appearing to be empty?

Updated code:

private static void deleteProxy(File proxyOld, String host, int port) {
        try {
            String lines, tempAdd;
            boolean removeLine = false;
            File proxyNew = new File("proxies_" + "cleaner$tmp");
            FileInputStream in = new FileInputStream(proxyOld);
            InputStreamReader read = new InputStreamReader(in);
            BufferedReader fileStream = new BufferedReader(read);
            FileWriter write = new FileWriter(proxyNew);
            BufferedWriter replace = new BufferedWriter(write);

            while ((lines = fileStream.readLine()) != null) {
                tempAdd = lines.trim();
                if (lines.trim().equals(host + ":" + port)) {
                    removeLine = true;
                }
                if (!removeLine) {
                    replace.write(tempAdd);
                    replace.newLine();
                }
            }
            in.close();
            read.close();
            fileStream.close();
            write.close();
            replace.close();

            if (proxyOld.delete()) {
                throw new Exception("Error deleting " + proxyOld);
            }

            if (proxyNew.renameTo(proxyOld)) {
                throw new Exception("Error renaming " + proxyOld);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Running the updated code it deletes proxies.txt just fine but it fails to make the new file:\
Maybe I should find a new method to update a text file, do you have any suggestions?

  • 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-05-23T16:00:54+00:00Added an answer on May 23, 2026 at 4:00 pm

    Your rename operation may not work, as per the File.renameTo() documentation:

    Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

    So basically, you’re wiping your old file, and you’re not guaranteed the new file will take its place. You must check the return value of File.renameTo():

    if(proxyNew.renameTo(proxyOld)){
        throw new Exception("Could not rename proxyNew to proxyOld");
    }
    

    As for why your renameTo may be failing: you’re not closing the nested set of streams that you open to read from the old file, so the operating system may still consider an abstract pathname to exist. Try making sure you close all of the nested streams you open.

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

Sidebar

Related Questions

I have this code: private static void saveMetricsToCSV(String fileName, double[] metrics) { try {
I am generating some files (simplified version) private static void GenFiles(int numbersToGenerate, string directory)
private static void SaveOrRemove<T>(string key, T value) { if (value == null) { Console.WriteLine(Remove:
This code compiles: private static void Main(string[] args) { bool? fred = true; if
I'm currently using the following code (C#): private static void PlayLoop(string filename) { Audio
private static void Main(string[] args) { for (;;) { TemporaryCityTool.TemporaryCityTool.AddCity(); Console.WriteLine(waiting...); Thread.Sleep(3600); } }
Does anyone know how to get enum values to string? example: private static void
private static void writeData( byte[] file, HttpServletResponse response ) throws IOException { .... //
private static void changeString(String s) { s = new String(new string); } public static
I have a method for copying files: private static void copy(final String source, final

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.