//创建读取接口中数据的方法
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();
}
}
}

Java读取接口中的数据,并保存到txt文件中!的更多相关文章

  1. Windows/Ubuntu下,将所有文件名字列举出来并保存到txt文件中

    Windows下 使用如下的DOS命令来实现: dir /s /b > lists.txt 可以将当前路径下的所有文件的"文件路径+文件名"存储在lists.txt中. 其中 ...

  2. 第二个爬虫之爬取知乎用户回答和文章并将所有内容保存到txt文件中

    自从这两天开始学爬虫,就一直想做个爬虫爬知乎.于是就开始动手了. 知乎用户动态采取的是动态加载的方式,也就是先加载一部分的动态,要一直滑道底才会加载另一部分的动态.要爬取全部的动态,就得先获取全部的u ...

  3. 根据导出的查询结果拼接字符串,生成sql语句并保存到txt文件中

    import os os.chdir("C:/") path = os.getcwd() print(path) f = open("sql.csv") # p ...

  4. Python 过滤a文件中每一行内容,保存到b文件中

    #coding=utf-8print 1#初始化文件crash_log.log with open('e:/1/crash_log.log','w')as f: f.close() def fw(se ...

  5. 爬虫-----爬取所有国家的首都、面积 ,并保存到txt文件中

    # -*- coding:utf-8 -*- import urllib2import lxml.htmlfrom lxml import etree def main(): file = open( ...

  6. np.savetxt()——将array保存到txt文件,并保持原格式

    问题:1.如何将array保存到txt文件中?2.如何将存到txt文件中的数据读出为ndarray类型? 需求:科学计算中,往往需要将运算结果(array类型)保存到本地,以便进行后续的数据分析. 解 ...

  7. SQL C# nvarchar类型转换为int类型 多表查询的问题,查询结果到新表,TXT数据读取到控件和数据库,生成在控件中的数据如何存到TXT文件中

    在数据库时候我设计了学生的分数为nvarchar(50),是为了在从TXT文件中读取数据插入到数据库表时候方便,但是在后期由于涉及到统计问题,比如求平均值等,需要int类型才可以,方法是:Conver ...

  8. 使用scrapy爬取的数据保存到CSV文件中,不使用命令

    pipelines.py文件中 import codecs import csv # 保存到CSV文件中 class CsvPipeline(object): def __init__(self): ...

  9. [matlab]改变矩阵的大小并保存到txt文件

    要完成的任务是,加载一个保存在txt文件中的矩阵, 并把它扩大10倍,并且要再次保存回去 %加载txt文件 >load('Matrix.txt'); %扩大10倍 repmat(Matrix,r ...

随机推荐

  1. cf441 f组合数。。单调指针

    e没学过做不出来.. 处理合法的区间很麻烦,但是处理非合法的区间很容易 答案就是所有的取法-不合法的区间 这题一定要双边界推进处理!!!! 一开始用单边界向右推进,结果后来发现错了,拿样例1就可以证明 ...

  2. C++ Primer 笔记——模板与泛型编程

    1.编译器用推断出的模板参数来为我们实例化一个特定版本的函数. 2.每个类型参数前必须使用关键字class或typename.在模板参数列表中,这两个关键字含义相同,可以互换使用,也可以同时使用. t ...

  3. 开启或停止website

    1.添加:Microsoft.Web.Administration 2.代码: static void Main(string[] args) { var server = new ServerMan ...

  4. python is和==的区别

    # ==和is # ==用来判断值是否相等# is是用看来判断是不是指定了同一个东西,判断是不是指向了同一个地址等 a = [11,22,33]b = [11,22,33] a == b # True ...

  5. WCF三种通信方式

    一.概述 WCF在通信过程中有三种模式:请求与答复.单向.双工通信.以下我们一一介绍. 二.请求与答复模式 描述: 客户端发送请求,然后一直等待服务端的响应(异步调用除外),期间处于假死状态,直到服务 ...

  6. Linux查找当前目录5天的文件并打包

    find . -name "*.sh" -mtime -5 |xargs tar zcvf /tmp/log.tar.gz 解释: *.sh是查找以.sh结尾的文件,也可以是其他如 ...

  7. git报错处理

    今天又遇到了这个问题,记录一下. 报错 原因及解决办法: 本文作者starof,因知识本身在变化,作者也在不断学习成长,文章内容也不定时更新,为避免误导读者,方便追根溯源,请诸位转载注明出处:http ...

  8. 搭建自己的docker仓库

    https://docs.docker.com/registry/deploying/#run-a-local-registry https://docs.docker.com/registry/in ...

  9. PrintDocument打印、预览、打印机设置和打印属性的方法

    WindowsForm 使用 PrintDocument打印.预览.打印机设置和打印属性的方法. private void Form1_Load(object sender, System.Event ...

  10. Application Initialization UI for IIS 7.5

    IIS Application Initialization for IIS 7.5 enables website administrators to improve the responsiven ...