Bufferedwriter 没有将*所有内容*保存到文件中
我对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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(已编辑。)
这就是
getNetDetails()
中的问题:换句话说,该方法总是将
netDetails
保留为 null,除非有一个例外。如果
getNetDetails()
返回一个字符串而不是设置一个变量,那就更好了,并且假设它要返回文件的最后一行,它应该类似于:You should also< /em> 在finally 块中关闭
InputStreamReader
,几乎可以肯定不会吞掉异常。(Edited.)
This is the problem, in
getNetDetails()
: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:You should also close the
InputStreamReader
in a finally block, and almost certainly not swallow the exception.