Java經常使用基礎代碼

1.加載properties文件
Properties properties = new Properties(); 
properties.load(Properties.class.getResourceAsStream("/config.properties"));
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));
2.獲取絕對路徑
${pageContext.request.contextPath} 
 
3.獲取路徑
HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
String contextPath=request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/";
 
4.運行Junit註解配置
@RunWith(SpringJUnit4ClassRunner.class) // 整合
@ContextConfiguration(locations = "classpath:spring-bean-*.xml") // 加載配置 
 
5.加載spring文件,獲取bean

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
RegisterDAO registerDAO = (RegisterDAO)ac.getBean("RegisterDAO");web

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring.xml");
RedisTemplate redisTemplate = (RedisTemplate)context.getBean("redisTemplate");redis

若是是兩個以上:
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","dao.xml"});算法

或者用通配符:spring

ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/*.xml"); 

6.計算總頁碼數app

(totalCount + pageInfo.getLength() - 1) / pageInfo.getLength()jsp

7.分頁算法編碼

pageInfo.setStart((page-1)*pageSize);spa

8.對URL進行編碼code

URLDecoder.decode(str,"UTF-8");orm

URLEncoder.encode(str,"UTF-8");

 
9.將Set集合轉爲List,這樣得到的list並不能有序排列
List<Topic> topicList = new ArrayList<Topic>(user.getTopics());
 
10.將list有序排列 
Collections.sort(topicList, new Comparator<Topic>() {  
   public int compare(Topic arg0, Topic arg1) {  
       return arg0.getTopicId().compareTo(arg1.getTopicId()); // 按照id排列  
   }  
});  
11.CDATA
AND <![CDATA[ (ac.length>= #{start,jdbcType=INTEGER})  ]]>
12.DecimalFormat 

DecimalFormat df = new DecimalFormat("####.00");
Double joinRate=new Double(df.format(temp*100));

13.Spring異常

<!-- 異常處理 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="errorPage/systemError"/> <!-- 默認爲500,系統錯誤(error.jsp) -->
<property name="defaultStatusCode" value="500"/>
<!-- 配置多個statusCode -->
<property name="statusCodes">
<props>
<prop key="500">500</prop>
<prop key="404">404</prop>
</props>
</property>
<property name="exceptionMappings">
<props>
<!-- 這裏你能夠根據須要定義N多個錯誤異常轉發 -->
<prop key="com.Exception">errorPage/systemError</prop>
<prop key="com.alibaba.dubbo.rpc.RpcException">errorPage/netError</prop>
</props>
</property>
</bean>

 
public static void main(String[] args)throws Exception{    File file = new File("F:\\a.png");    String a=file.separator;    System.out.println(a);    String fileName=file.getName();    String prefix=fileName.substring(fileName.lastIndexOf("."));    String datePag = new SimpleDateFormat("yyyyMMdd").format(new Date());    String ctime = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());    System.out.println(ctime);    System.out.println(prefix);    String path="D:\\"+ctime;    String pathPag="D:\\"+datePag;    File data = new File(path);    File dataPag = new File(pathPag);    if(!dataPag.exists())        dataPag.mkdirs();    FileInputStream fis = new FileInputStream(file);    byte[] b = new byte[1024];    int len = 0;    FileOutputStream fos = new FileOutputStream(pathPag+"\\"+ctime+prefix);    while((len=fis.read(b))!=-1){        fos.write(b,0,len);    }    fos.close();    fis.close();}
相關文章
相關標籤/搜索