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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:20:50+00:00 2026-05-25T15:20:50+00:00

I am somewhat new to java and was hoping that someone could help me.

  • 0

I am somewhat new to java and was hoping that someone could help me. I have looked everywhere, but cannot seem to finder a solution.
I’m trying to save the result of a method into a file using bufferedwriter. The bufferedwriter itself works as it is saving some other strings, but when it comes to this function is just displays ‘null’. Is is because the result of this method returns more than one string? How do I resolve this?
My code is as following:

Bufferedwriter code:

public static boolean saveStringToFile (String fileName, String saveString)
{
    boolean saved = false;
    BufferedWriter bw = null;
    try
    {
    bw = new BufferedWriter(new FileWriter(fileName));
    try 
    {
            bw.write(saveString);
            saved = true;
    }
    finally
    {
            bw.close();
    }

    }
    catch (IOException ex)
    {
        ex.printStackTrace();
    }
    return saved;
}

The function itself:

public static void getNetDetails()
{
    try {
        Process net = Runtime.getRuntime().exec("lsof -i -n -P");
        BufferedReader netInput = new BufferedReader(
                new InputStreamReader(net.getInputStream()));
    while ((netDetails = netInput.readLine()) !=null)
    {
        System.out.println(netDetails);
        System.out.println("\n");
    }

        }
        catch(IOException e) {
               System.out.println("exception happened - here are the details: ");
                e.printStackTrace();

            }
}

Saving function to file using bufferedwriter

public static void generateNetReport()
{
    saveStringToFile("Net.txt","here is the thing.." + "\n" + netDetails );
}

can someone please help with how I can save netDetails onto a file without it just displaying null??

  • 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-25T15:20:51+00:00Added an answer on May 25, 2026 at 3:20 pm

    (Edited.)

    This is the problem, in getNetDetails():

    while ((netDetails = netInput.readLine()) !=null)
    

    In other words, the method will always leave netDetails as null, unless there’s an exception.

    It would be better if getNetDetails() returned a string instead of setting a variable, and assuming it’s meant to return the final line of the file, it should be something like:

    String line = null;
    String nextLine;
    while ((nextLine = netInput.readLine()) != null) {
        line = nextLine;
    }
    return line;
    

    You should also close the InputStreamReader in a finally block, and almost certainly not swallow the exception.

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

Sidebar

Related Questions

I am somewhat new to the Java Generics feature. Can you please help me
At a new job I started, we have both a Java application that handles
I am somewhat new to JAVA. I've been working with it in college, but
I am somewhat new to LINQ and have a quick question regarding deleting. Say,
I am somewhat new to R, and i have this piece of code which
I am somewhat new to jQuery and I have a problem with something I
I'm somewhat new to SQL and need help with query syntax. My issue involves
I'm still somewhat new to Java and trying to insert data into a database.
The following simple code in Java behaves somewhat in a strange way that I
A very simple Vector in Java that produces the output that is somewhat difficult

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.