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
  • 相关阅读:
    外校培训前三节课知识集合纲要(我才不会告诉你我前两节只是单纯的忘了)
    floyd算法----牛栏
    bfs开始--马的遍历
    (DP 线性DP 递推) leetcode 64. Minimum Path Sum
    (DP 线性DP 递推) leetcode 63. Unique Paths II
    (DP 线性DP 递推) leetcode 62. Unique Paths
    (DP 背包) leetcode 198. House Robber
    (贪心 复习) leetcode 1007. Minimum Domino Rotations For Equal Row
    (贪心) leetcode 452. Minimum Number of Arrows to Burst Balloons
    (字符串 栈) leetcode 921. Minimum Add to Make Parentheses Valid
  • 原文地址:https://www.cnblogs.com/peachh/p/13658069.html
Copyright © 2011-2022 走看看