zoukankan      html  css  js  c++  java
  • Java读取接口中的数据,并保存到txt文件中!

    //创建读取接口中数据的方法
    	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()方法,当读取流读取数据时使用,当读到
    、
    时,会跟着换行,
    //			同时会以字符串的形式返回这一行,当读取完所有数据时,会返回null
    			while ((line = reader.readLine()) != null) {
    				System.out.println("导入中。。。");
    				sb.append(line + "
    ");
    			}
    			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
  • 相关阅读:
    hdu4675 GCD of Sequence 莫比乌斯+组合数学
    hdu4746 Mophues 莫比乌斯
    BZOJ2820 YY的GCD 莫比乌斯+系数前缀和
    bzoj2005 能量采集 莫比乌斯或者普通容斥
    GCD 莫比乌斯反演 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对(x,y)有多少对.
    spoj7001 Visible Lattice Points 莫比乌斯反演+三维空间互质对数
    hdu1695 GCD 莫比乌斯反演做法+枚举除法的取值 (5,7),(7,5)看做同一对
    bzoj2440 完全平方数 莫比乌斯值+容斥+二分
    Problem b 莫比乌斯反演+枚举除法的取值
    Codeforces839D Winter is here 容斥
  • 原文地址:https://www.cnblogs.com/peachh/p/13658069.html
Copyright © 2011-2022 走看看