`

Java Programming Tricks 2

    博客分类:
  • Java
阅读更多

1. Properties ResourceBundle类的路径问题

  System.out.println(System.getProperty("user.dir"));  //这个是去工程的绝对路径的
   System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));//这个是去当      前classpath的uri的!
   new Properties().load(new FileInputStream("test.properties"));//这里说明test文件在工程的跟目录下!

  new Properties().load(new FileInputStream("test/test.properties"));//说明,在工程底下的test的文件夹底下的文件test.properties!
   ResourceBundle rs = ResourceBundle.getBundle("org.hello");//这里说明,hello文件就是在于classpath底下的,org包底下有这个文件hello.properties~也就是说.../bin/这个是classpath绝对路径!

 

2、一个http请求的头部

GET /index.html HTTP/1.1

Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*

Accept-Language: en-us,zh-cn;q=0.5

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 2.0.50727)

Host: localhost:8080

Connection: Keep-Alive

 

 

 

 

 

out.println("GET http://fxcm.fx-performance.com/SystemTickets.aspx?bkr=1"

 

+ "&SystemID=" + systemID + "&InstrumentID=" + instrumentID

+ "&FromDate=20090101&ToDate=20090701 HTTP/1.0");

out.println("Host: " + "fxcm.fx-performance.com");

out.println("User-Agent: " + "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9");

out.println("Accept: " + "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");

out.println("Accept-Language: " + "en-us,zh-cn;q=0.5");

//out.println("Accept-Encoding: " + "gzip,deflate");

out.println("Accept-Charset: " + "ISO-8859-1,utf-8;q=0.7,*;q=0.7");

out.println("Keep-Alive: " + "300");

out.println("Proxy-Connection: " + "keep-alive");

//out.println("Cookie: " + "BCSI-CSA9B5F27E=2; BCSI-CSA9B5CE25=2");

out.println("Cache-Control: " + "max-age=0");

out.println("");// 这句很关键,告诉server这段话说完了。然后server端的in.readLine的while循环才能跳出向后执行。

 

private void fetchData() {

  Socket socket = null;
  PrintWriter out = null;
  BufferedReader in = null;
  String fromServer = "";

  while (1 == 1) {

   try {
    
    Thread.sleep(10000);

    socket = new Socket("webproxy.ssmb.com", 8080);
    out = new PrintWriter(socket.getOutputStream(), true);
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    out.println("GET http://www.dailyfx.com.hk/informer/fxcm_pricedde_sc.php?temp=1");
    out.println("Host:
www.dailyfx.com.hk");
    out.println("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    out.println("Accept-Language: en-us");
    out.println("Accept-Encoding: gzip,deflate");
    out.println("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    out.println("Keep-Alive: 300");
    out.println("Proxy-Connection: keep-alive");
    out.println("");

    while ((fromServer = in.readLine()) != null) {
     System.out.print(fromServer);
    }
    System.out.println();

   } catch (Exception e) {
    e.printStackTrace();
   } finally {
    try {
     out.close();
     in.close();
     socket.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 } 

 

 

 

 

 

 

 

3、inputstream to string

 

input是一个InputStream的对象。

public void parse() { // Read a set of characters from the socket StringBuffer request = new StringBuffer(2048); int i; byte[] buffer = new byte[2048]; try { i = input.read(buffer); } catch (IOException e) { e.printStackTrace(); i = -1; } for (int j = 0; j < i; j++) { request.append((char) buffer[j]); } System.out.print(request.toString()); uri = parseUri(request.toString()); }


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    JAVA-TricksoftheJavaProgrammingGurus.zip_java programming_tricks

    以上只是一部分可能包含在"Tricks of the Java Programming Gurus"中的知识点。这些技巧可以帮助开发者成为真正的Java编程大师,提升代码质量,降低维护成本。深入学习和实践这些技巧,将使你在Java开发道路上...

    TRICKS OF THE JAVA™ PROGRAMMING GURUS

    TRICKS OF THE JAVA™ PROGRAMMING GURUS

    Java.Crash.Course.1541019245.epub

    "Java Crash Course" contains a multitude of tips and tricks, examples and exercises you can do to grow your Java programming skills to unprecedented levels. We chose the content of this book carefully...

    Java Crash Course [2016]

    "Java Crash Course" contains a multitude of tips and tricks, examples and exercises you can do to grow your Java programming skills to unprecedented levels. We chose the content of this book carefully...

    hands-automation-testing-java-beginners(2018)

    Write any Java program logic with strategies, tips, and tricks Leverage advanced topics in Java collections to solve Java-related problems Understand and use objects, classes, methods, and functions ...

    Manning Java Reflection In Action

    英文经典原著 Java Reflection in Action Java Reflection in Action is unique in presenting a clear account of all the cool things you can do with reflection, and...author of Concurrent Programming in Java

    Java 9 Concurrency Cookbook - Second Edition

    Writing concurrent and parallel programming applications is an integral skill for any Java programmer. Java 9 comes with a host of fantastic features, including significant performance improvements ...

    学java该看的一些书~

    - **《Java Tips and Tricks》**:该书通过一系列的小贴士和技术要点帮助读者解决常见问题,并掌握一些高效编程的方法。 - **O'Reilly 出版的《Java I/O》**:深入介绍了 Java 输入输出流的概念和使用方法,对于理解...

    Java Reflection in Action

    sometimes perplexing programming techniques along with enough background to understand how to extend and vary them. This book overcomes reflection’s reputation as a mysterious and esoteric philo- ...

    Agile.Java.Development.with.Spring.Hibernate.and.Eclipse

    Also, Extreme Programming (XP), Agile Model Driven Development (AMDD) and refactoring are methods that can expedite the software development projects by reducing the amount of up front requirements ...

    Agile.Java.Development.with.Spring.Hibernate.and.Eclipse-part3

    Also, Extreme Programming (XP), Agile Model Driven Development (AMDD) and refactoring are methods that can expedite the software development projects by reducing the amount of up front requirements ...

    Android.Programming.The.Easy.Way

    This step-by-step guide has examples on every page and is filled with bonus tips and tricks to make your app stand out. If you follow this guide you will have a completed app in as little as one day. ...

    Groovy in action

    Groovy in Action is a comprehensive guide to Groovy programming, introducing Java developers to the new dynamic features that Groovy provides. To bring you Groovy in Action, Manning again went to the...

    翻写拍砖游戏--说明Windows编程原理

    并且有一本《Tricks of the Windows Game Programming Gurus》英文(或者中文)书籍正在阅读。 说明:当看到André Lamothe大师所著游戏编程书籍时非常兴奋。本人在学习《Tricks of the Windows Game Programming ...

    [Wrox] C# 5.0 Programmer's Reference

    Author Rod Stephens is a well-known programming authority and has written more than 25 programming books covering C#, Java, VB, and other languages. ☆ 出版信息:☆ [作者信息] Rod Stephens [出版...

    PHP.and.Algorithmic.Thinking.for.the.Complete.Beginner.1503015

    This book is for anyone who wants to learn computer programming and knows absolutely nothing about it. Of course, if you are wondering whether this book is going to teach you how to create amazing ...

    C# 5.0 Programmer’s Reference

    Author Rod Stephens is a well-known programming authority and has written more than 25 programming books covering C#, Java, VB, and other languages. His books have sold more than 60,000 copies in ...

Global site tag (gtag.js) - Google Analytics