//创建读取接口中数据的方法
public static String read() {
URL url = null;
BufferedReader reader = null;
HttpURLConnection connection = null;
InputStreamReader ins = null;
try {
// 设置url地址
url = new URL("https://***.***.com/api/getStudent");
System.out.println("已完成20%。。。");
// 获取连接通道
connection = (HttpURLConnection) url.openConnection();
// 设置连接响应时间
connection.setConnectTimeout(2 * 1000);
// 设置读取响应时间
connection.setReadTimeout(2 * 1000);
// 连接
connection.connect();
System.out.println("已完成50%。。。");
// 输入流
ins = new InputStreamReader(connection.getInputStream(), "UTF-8");
// 读取
reader = new BufferedReader(ins);
// 创建可变字符串
StringBuffer sb = new StringBuffer();
System.out.println("已完成80%。。。");
String line;
// readLine()方法,当读取流读取数据时使用,当读到\n、\r时,会跟着换行,
// 同时会以字符串的形式返回这一行,当读取完所有数据时,会返回null
while ((line = reader.readLine()) != null) {
System.out.println("导入中。。。");
sb.append(line + "\n");
}
return sb.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Error GetURL:" + e);
System.out.println("Error GetURL:" + url);
e.printStackTrace();
} finally {
if (ins != null) {
try {
ins.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection != null) {
connection.disconnect();
}
}
return null;
}
//写文件处理
public static void main(String[] args) {
System.out.println("导入开始!");
File file = new File("F:/love.txt");
if(file.exists()) {
System.err.println("F盘下已存在love.txt的文件,将更新文件内容");
file.delete();
}
if(!file.exists()) {
try {
file.createNewFile();
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file),"GB2312");
BufferedWriter bw = new BufferedWriter(osw);
bw.write(read());
System.out.println("已完成100%");
System.out.println("导入结束!");
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
原文:https://www.cnblogs.com/wjup/p/10576109.html
原文:https://www.cnblogs.com/peachh/p/13658069.html