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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:51:31+00:00 2026-06-14T15:51:31+00:00

This code is reading a bunch of .java files and finding public [classname] or

  • 0

This code is reading a bunch of .java files and finding “public [classname]” or “private [classname]” and adding “System.out.println([classname])” to that line.

The problem is When I write that line back in I end up with a blank file

Can anyone see what I am doing wrong?

private static void work(ArrayList<File> fileList) {
    for (int i = 0; i < fileList.size(); i++) {
        replaceLines(fileList.get(i));
    }

}

public static void replaceLines(File file) {
    String path = file.getPath();
    String fileNameLong = file.getName();
    String fileName = null;
    if (fileNameLong.contains(".java")) {
        fileName = fileNameLong.substring(0, file.getName().indexOf("."));
    }
    if (fileName != null && fileName != "") {
        System.out.println(fileName);
        try {
            //prepare reading
            FileInputStream in = new FileInputStream(path);
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(in));
            //prepare writing
             FileWriter fw = new FileWriter(file);
             PrintWriter out = new PrintWriter(fw);

            String strLine;
            while ((strLine = br.readLine()) != null) {
                // Does it contain a public or private constructor?
                boolean containsPrivateCon = strLine.contains("private "
                        + fileName);
                boolean containsPublicCon = strLine.contains("public "
                        + fileName);

                if (containsPrivateCon || containsPublicCon) {
                    int lastIndexOfBrack = strLine.lastIndexOf("{");

                    while (lastIndexOfBrack == -1) {
                        strLine = br.readLine();
                        lastIndexOfBrack = strLine.lastIndexOf("{");
                    }

                    if (lastIndexOfBrack != -1) {
                        String myAddition = "\n System.out.println(\""
                                + fileName + ".java\"); \n";
                        String strLineModified = strLine.substring(0,
                                lastIndexOfBrack + 1)
                                + myAddition
                                + strLine.substring(lastIndexOfBrack + 1);
                        strLine = strLineModified;
                    }
                }
                out.write(strLine);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

}
  • 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-14T15:51:32+00:00Added an answer on June 14, 2026 at 3:51 pm

    If you want to write to the same file you’re reading from, you should either write to a copy of the file (different filename) and then rename the output file, or use RandomAccessFile interface to edit a file in-place.

    Usually, the first solution will be much easier to implement than the second one; unless the files are huge (which is probably not the case with .java files), there is no real reason to use the second.

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

Sidebar

Related Questions

I'm reading code from GraphServer . And there is this function that has weird
I have a bunch of code that looks like this: with tempfile.NamedTemporaryFile() as tmpfile:
I'm reading this code sample : And since I don't know C#, I decided
i was reading this code, where setRegions is called after RootViewController is released :
i am reading an excel file using this code i found over the net:
I am reading On lisp and encountered this code (I simplified it a bit).
Note: This code actually codes from a tutorial book I'm reading at the moment,
I was reading a blog where the writer said this Code doesn’t exist unless
I'm reading Programming Perl , and I found this code snippet: sub new {
While I'm reading boost/shared_ptr.hpp, i saw this code: // generated copy constructor, destructor are

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.