私は新しいJava開発者です。しかし、私はJunitのテストケースを書くことについて多くの知識を持っていません。すぐに就職テストをします。彼らは私にプログラムを書いて欲しいのです
- 任意のWebサイトからHTMLを読み取るには、「http://www.google.com」と言います(URLConnectionなどのJavaに組み込まれているAPIの任意のAPIを使用できます)
- 上記のURLからコンソールにHTMLを印刷し、ローカルマシンのファイル(web-content.txt)に保存します。
- 上記のプログラムのJUnitテストケース。
以下のように最初の2つのステップを完了しました。
import java.io.*;
import java.net.*;
public class JavaSourceViewer{
public static void main (String[] args) throws IOException{
System.out.print("Enter url of local for viewing html source code: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String url = br.readLine();
try {
URL u = new URL(url);
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
int code = uc.getResponseCode();
String response = uc.getResponseMessage();
System.out.println("HTTP/1.x " + code + " " + response);
InputStream in = new BufferedInputStream(uc.getInputStream());
Reader r = new InputStreamReader(in);
int c;
FileOutputStream fout=new FileOutputStream("D://web-content.txt");
while((c = r.read()) != -1){
System.out.print((char)c);
fout.write(c);
}
fout.close();
} catch(MalformedURLException ex) {
System.err.println(url + " is not a valid URL.");
} catch (IOException ie) {
System.out.println("Input/Output Error: " + ie.getMessage());
}
}
}
今、私は3番目のステップで助けが必要です。