1, (8 分 ) 使用程序从网上下载 pdf, 网址为
http://files.saas.hand-china.com/java/target.pdf,保存在本地,
编程时使用带缓冲的读写,将需要保证保存后的 pdf 文件能正常
打开.
解析:只能用字节流来读写,用字符流写入pdf后 文件打不开,程序还与待优化
package com.fansi.com; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class Url_File { public static void main(String[] args) { try { URL url = new URL("http://files.saas.hand-china.com/java/target.pdf"); URLConnection conn = url.openConnection(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); File file = new File("target.pdf"); FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(fos); ]; ){ bos.write(b); } bos.close(); fos.close(); bis.close(); is.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
2, (8 分)实现一个 Socket 服务器端和一个 Socket 客户端,先启动服
务器端,当客户端连接上来后,将第一题中得到的 target.pdf 通过
Stream 发送给客户端,客户端接收并保存为 pdf 文件,并保证 pdf
文件能被正常打开.
服务器端代码,Server.java (字节流)
package com.fansi; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) { try { ServerSocket serversocket = ); while(true){ Socket socket = serversocket.accept(); System.out.println("有客户端连上了"); File file = new File("down.pdf"); FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream()); ]; ){ bos.write(b); } bos.close(); bis.close(); fis.close(); } } catch (IOException e) { e.printStackTrace(); } } }
客户端代码,Client.java(字节流)
package com.fansi; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; public class Client { public static void main(String[] args) { try { Socket socket = ); BufferedInputStream bis =new BufferedInputStream(socket.getInputStream()); File file = new File("new.pdf"); FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(fos); ]; ){ bos.write(b); } bos.flush(); bos.close(); fos.close(); bis.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
3, (8 分)
Sina 股票数据接口,以大秦铁路(股票代码:601006)为
例,如果要获取它的最新行情,只需访问新浪的股票数据
接口:http://hq.sinajs.cn/list=sh601006 这个 url 会返回
一串文本,例如:
var hq_str_sh601006="大秦铁路, 27.55, 27.25, 26.91,
27.55, 26.20, 26.91, 26.92,
22114263, 589824680, 4695, 26.91, 57590, 26.90, 14700,
26.89, 14300,
26.88, 15100, 26.87, 3100, 26.92, 8900, 26.93, 14230,
26.94, 25150, 26.95, 15220, 26.96, 2008-01-11, 15:05:32";
这个字符串由许多数据拼接在一起,不同含义的数据用逗
号隔开了,顺序号从 0 开始。
0:”大秦铁路”,股票名字;name
1:”27.55″,今日开盘价;open
2
2:”27.25″,昨日收盘价;close
3:”26.91″,当前价格;current
4:”27.55″,今日最高价;high
5:”26.20″,今日最低价;low
编程访问 http://hq.sinajs.cn/list=sz300170 ,解释所得到的数
据,并将生成 XML 数据文件和 JSON 文件,并保存到本地,如:
xml 数据:
xxxxx
< open >xxxxx
……
json 数据:
{name:”xxx”,open:xxxx……}
Json_Xml.java (字符流)
package com.fansi; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import com.google.gson.JsonObject; public class Json_Xml { public static void main(String[] args) { try { URL url = new URL("http://hq.sinajs.cn/list=sz300170"); URLConnection conn = url.openConnection(); InputStreamReader bis = new InputStreamReader(conn.getInputStream()); BufferedReader br = new BufferedReader(bis); String line; String[] res = ]; while((line=br.readLine())!=null){ System.out.println(line); res = line.split(","); } // for(int i=0;i<res.length;i++){ // System.out.println(res[i]); // } res[]=res[].substring(); //System.out.println(res[0]); br.close(); bis.close(); //-----------------json创建 JsonObject object = new JsonObject(); ]); ]); ]); ]); ]); ]); //System.out.println(object.toString()); File f1 = new File("json.txt"); FileOutputStream fis1 = new FileOutputStream(f1); OutputStreamWriter osw1 = new OutputStreamWriter(fis1, "utf-8"); BufferedWriter bw1 = new BufferedWriter(osw1); bw1.write(object.toString()); bw1.close(); osw1.close(); fis1.close(); //---------------------------xml的写 try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument(); Element root = document.createElement("hand"); Element r1 = document.createElement("name"); r1.setTextContent(res[]); root.appendChild(r1); Element r2 = document.createElement("open"); r2.setTextContent(res[]); root.appendChild(r2); Element r3 = document.createElement("current"); r3.setTextContent(res[]); root.appendChild(r3); Element r4 = document.createElement("high"); r4.setTextContent(res[]); root.appendChild(r4); Element r5 = document.createElement("low"); r5.setTextContent(res[]); root.appendChild(r5); document.appendChild(root); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.transform(new DOMSource(document), new StreamResult(new File("xml.txt") )); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }