我已经编写了一个java代码来打印我的输出文件,该文件位于文档中,但是在执行代码文件后仍然是空的。有人犯了我的错误。我正在使用netbeans来创建这个程序
码
import java.net.HttpURLConnection;
import java.net.URL;
import java.lang.*;
import java.io.*;
public class NewClass1 {
static long start = 0;
static long finish = 0;
static long time;
public static void main(String[] args) throws Exception {
BufferedWriter bw = null;
FileWriter fw = null;
for (int i = 0; i <= 5; i++) {
String stack_url = "https://www.google.lk/";
try {
URL url = new URL(stack_url);
HttpURLConnection httpUrlConnect = (HttpURLConnection) url.openConnection();
httpUrlConnect.setConnectTimeout(10);
start = System.currentTimeMillis();
httpUrlConnect.connect();
if (httpUrlConnect.getResponseCode() == 200) {
finish = System.currentTimeMillis();
System.out.println(stack_url + " - " + httpUrlConnect.getResponseMessage() + " took " + (finish - start) + " Milli Seconds.");
}
if (httpUrlConnect.getResponseCode() == httpUrlConnect.HTTP_NOT_FOUND) {
System.out.println(stack_url + " - " + httpUrlConnect.getResponseMessage() + " - " + httpUrlConnect.HTTP_NOT_FOUND);
}
} catch (Exception e) {
}
time = finish - start;
finish = System.currentTimeMillis();
System.out.println("Total Time for page load - " + (finish - start));
try {
Integer _time = (int) (long) time;
String content = Integer.toString(_time);
fw = new FileWriter("time.sql");
bw = new BufferedWriter(fw);
bw.write(content);
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Thread.sleep(1000);
}
}
答案 0 :(得分:1)
第一件事:
你的程序没有花费全部时间来执行,所以你必须增加时间:
httpUrlConnect.setConnectTimeout(1000);
//--------------------------------^^
第二件事:
不要在代码中创建文件,只需在之前创建它,这样就不得不将这两行移到循环之外:
fw = new FileWriter("time.sql");
bw = new BufferedWriter(fw);
第三件事:
不要在每次迭代中关闭文件,在完成后关闭文件,因此必须在循环后删除finally块并关闭文件。
bw.close();
fw.close();
您的代码应如下所示:
由用户1516873编辑
@Test
public void test44325932() {
final String stackUrl = "https://www.google.lk/";
// BufferedWriter is closable, it will close automatically (and close underline stream) in finally block
try (BufferedWriter bw = new BufferedWriter(new FileWriter("time.sql"))) {
for (int i = 0; i <= 5; i++) {
String responseStatus;
long start = System.currentTimeMillis();
long finish;
try {
URL url = new URL(stackUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000); // 5 sec connect timeout
connection.connect();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
responseStatus = "OK";
} else { // any non 200 status counts as error
responseStatus = connection.getResponseCode() + " : " + connection.getResponseMessage();
}
// full time should be calculated after completely reading response, not after get status code, but it is depends of requirements
// I put it here as example
// IOUtils.readFully(connection.getInputStream(), new byte[1024]);
connection.disconnect();
} catch (IOException e) {
responseStatus = "Exception : " + e.getClass().getSimpleName();
} finally {
finish = System.currentTimeMillis();
}
long time = finish - start;
System.out.println("Server response: " + time + " ms, status: " + responseStatus);
bw.write(String.valueOf(time)); // i think you should write status here, too.
bw.newLine();
Thread.sleep(1000);
}
System.out.println("Done");
} catch (Exception ex) {
ex.printStackTrace();
}
}
答案 1 :(得分:-1)
您应该使用try-with-resource,如:
try (FileWriter fw = new FileWriter("D:\\time.sql"))
作为main方法的第一行,最后阻止你,你可以删除fw.close()
调用
如果您愿意,可以直接使用fw.write(content);
而不是缓冲的作家。
如果您使用try with resource:
,您可以删除以下代码(因为您正在循环并且每次构造一个不需要的新对象时):fw = new FileWriter("time.sql");
bw = new BufferedWriter(fw);
bw.write(content);
而是使用:
fw.write(content);
我确实做了以上更改,它对我有用。
HTH, AJ