Bufferedwriter 没有将*所有内容*保存到文件中

发布于 2024-12-04 10:34:18 字数 1525 浏览 11 评论 0原文

我对java有点陌生,希望有人能帮助我。我到处都找过了,但似乎找不到解决方案。 我正在尝试使用 bufferedwriter 将方法的结果保存到文件中。 bufferedwriter 本身的工作原理是保存一些其他字符串,但是当涉及到这个函数时,它只是显示“null”。是因为该方法的结果返回多个字符串吗?我该如何解决这个问题? 我的代码如下:

Bufferedwriter 代码:

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;
}

函数本身:

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();

            }
}

使用 bufferedwriter 将函数保存到文件

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

有人可以帮助我如何保存 netDetails到一个文件而不只是显示空?

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??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零時差 2024-12-11 10:34:18

(已编辑。)

这就是 getNetDetails() 中的问题:

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

换句话说,该方法总是netDetails 保留为 null,除非有一个例外。

如果 getNetDetails() 返回一个字符串而不是设置一个变量,那就更好了,并且假设它要返回文件的最后一行,它应该类似于:

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

You should also< /em> 在finally 块中关闭InputStreamReader,几乎可以肯定不会吞掉异常。

(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文