java常用代码

来源:互联网 发布:erp软件推荐 编辑:程序博客网 时间:2024/05/22 02:00

1/写一个java类充当java bean,一个jsp作为界面,在jsp中使用后台java赋值,需在jsp 添加
<jsp:useBean id="user" class="test.UserData" scope="session"/> class 引用类所在的路径,id 相当于新建了一个该类的实例。在jsp中 使用<%= user.getName() %> 来获取值

2/ 将字符串写入文件中
    public static void main(String[] args)
    {
        File file = new File("a.txt");   //根据已经存在的路径创建一个file对象
        System.out.println(file.getAbsolutePath());//file对象的绝对路径,为什么和刚刚创建file的路径不一定相同?
        file.delete();
        System.out.println("file.exists?" + file.exists());
        String info = "username = administrator" + System.getProperty("line.separator") + "password=123456";
        OutputStream out = null;
        try
        {
            out = new FileOutputStream(file);
            out.write(info.getBytes());
            out.flush();
            System.out.println("User set admin's id and password succeed");
            System.out.println("file.exists?" + file.exists());
        }
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
    }
 
 
3/ jsp 单击关闭窗口 onClick="window.close();// 获得sessionID
        String sessionID = request.getSession().getId();
  
  
4/  键盘输入  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
              while(str != null)
     {
         str = in.readLine();
     }
     in.close();
    
 文件读入  BufferedReader in = new BufferedReader(new FileReader("infilename"));
           while((str=in.readLine())!=null)
     {
     打印
     }
     in.close();
    
  BufferedReader in = new BufferedReader(
            new InputStreamReader(new FileInputStream("infilename"), "UTF8"));
        String str = in.readLine();
  
    
5/ 写到文件中
    BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
 out.write("aString");
 out.close();
 
 Writer out = new BufferedWriter(new OutputStreamWriter(
            new FileOutputStream("outfilename"), "UTF8"));
        out.write(aString);
        out.close();

6/ 在程序中不硬性编码与平台相关的任何常量
    System.getProperty("file.separator"); //文件分隔符   windows \  Linux  /
 System.getProperty("path.separator"); // 路径分隔符  windows ; Linux  :
 System.getProperty("line.separator"); //换行符      windows \r\n Linux  \n
 System.getProperty("user.dir"); //当前工程路径
 
7/ 远程调试JAVA_ARGS中添加  -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=6332,server=y,suspend=n 

8/ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 引用jstl标签需要导入该句,还需要导入一个jar包:jstl.jar(WebRoot\WEB-INF\lib目录下)

9/ 功能同String.spilt()
    StringTokenizer s = new StringTokenizer("this is a test");
        while (s.hasMoreTokens())
            System.out.println(s.nextToken());
   
10/ 将数组变成List  , Arrays.asList(shuzuming);


11/  使用js ,jsp 与后台java 交互参数的方法
     在js中将变量放入request的session中,request.getSession().setAttribute("msis_dn",request.getParameter("msisdn"));
  后台取用变量 request.getSession().getAttribute("msis_dn");
 
12/
 Set<String> strSet = map.keySet(); // 得到map中的key值
 String[] keyStrs = strSet.toArray(new String[strSet.size()]);  // 将set 转换为数组
 Arrays.sort(keyStrs, String.CASE_INSENSITIVE_ORDER); // 数组中排序,按字典顺序排序(忽略大小写)
 Collections.reverse(Arrays.asList(keyStrs))  ;  // 将集合反转排序。最后结果,数组keyStrs 是排好了序的
 
 
 // 将map放入list中,取其中的键值打印出来
 ArrayList<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(map.entrySet());
 for (Entry<String, Integer> e : list)
 {
  System.out.println(e.getKey() + ":" + e.getValue());
 }
 // 使用集合排序
 Collections.sort(list, new Comparator<Object>()
 {
  public int compare(Object e1, Object e2)
  {
   int v1 = Integer.parseInt(((Entry<String, Integer>)e1).getValue().toString());
   int v2 = Integer.parseInt(((Entry)e2).getValue().toString());
   return v1 - v2;
  }
 });
 System.out.println("##############");
 for (Entry<String, Integer> e : list)
 {
  System.out.println(e.getKey() + ":" + e.getValue());
 }
 
13/ 匹配正则表达式
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(inputStr);
        if (matcher.matches())
        {
            outputStr.append("YES");
        }

  Pattern.matches("[0-9]+",num);
 
14/ 在校验之前对输入的字符串做归一化处理
    s = Normalizer.normalize(s,Form.NFKC);

15/ 罗列目录下的文件名
        File dir = new File(System.getProperty("user.dir"));
        if (!dir.isDirectory())
        {
            System.out.println("Not a directory");
        }
        else
        {
            for (String file : dir.list())
            {
                System.out.println(file);
            }
        }
16/ 对文件路径进行标准化
 File f = new File("d:\\a\\b\\c\\d" + ".\\..\\..\\dmc");
    String canonicalPath = f.getCanonicalPath();  // D:\a\b\dmc
 
  

17/ 当前时间转换,插入时间到数据库
 String pattern="yyyy-MM-dd HH:mm:ss";
 SimpleDateFormat sdf = new SimpleDateFormat(pattern);
 Date now = new Date();
 sdf.format(now);   将当前时间转换为模式对应的格式

18/ 大小写转换
 operType.toLowerCase(Locale.US)


  
 

 

0 0
原创粉丝点击