首页 > 编程语言 >java常用代码

java常用代码

时间:2022-10-08 19:02:26浏览次数:46  
标签:常用 java String 代码 System file println new out


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)

标签:常用,java,String,代码,System,file,println,new,out
From: https://blog.51cto.com/u_15812342/5738485

相关文章

  • java----小案例,幸运会员,打印正三角
    案例一1.接收一个4位数会员号2.生成随机数并乘以103.算出会员号中的百位数字的数字号与生成的随机数比较4.如果相等则是幸运会员,反之不是packagelearnday5;​import......
  • Java8中LocalDateTime与时间戳timestamp的互相转换及ChronoUnit工具类
    Java8中LocalDateTime与时间戳timestamp的互相转换及ChronoUnit工具类importjava.time.*;importjava.time.format.DateTimeFormatter;importjava.time.temporal.Chr......
  • Java服务发起HTTPS请求报错:PKIX path building failed: sun.security.provider.certpa
    Java服务发起HTTPS请求报错:PKIXpathbuildingfailed:sun.security.provider.certpath.SunCertPathBuilderException1.从域名的https导出下载证书文件下载证书第一步是......
  • 零基础如何学习入门Java编程
    如何学习在以前大部分人学习都是先去找本书,先看看,再试,要是不懂了在去网上去查,再在继续啃着书本。但现在向书学习和在网上学习这掌握的效果是不同的,要学会用适合自己的学习方......
  • 优维低代码:Context 上下文
    导语优维低代码技术专栏,是一个全新的、技术为主的专栏,由优维技术委员会成员执笔,基于优维7年低代码技术研发及运维成果,主要介绍低代码相关的技术原理及架构逻辑,目的是给广大......
  • Java中的无符号类型
    背景计算机科班出身大多学过离散数学,或者理工类专业也大多学习过C或C++语言,从中我们了解到基本类型的整形有short、int、long等,还分别有无符号(unsigned)和带符号(sign......
  • 【路径规划】基于粒子群和遗传算法求解充电量和时间窗约束下的多AGV路径规划问题复mat
    ✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。......
  • RNA-seq | 转录组标准分析流程和常用工具软件介绍
    笔记内容摘要:RNA-seq转录组基础知识与标准分析流程,简单记录学习过程。转录组分析是对样本转录产物RNA的深入挖掘研究。通常情况下,植物的表型差异可能由许多因素控制,其中......
  • java异常机制
     java中所有错误的超类为:Throwable。其下有两个子类:Error和Exception。Error的子类描述的都是系统错误,比如虚拟机内存溢出。Exception的子类描述的都是程序,比如空......
  • java_day08
    Java基础Java面向对象面向对象编程(Object-OrientedProgramming,OOP)面向对象编程的本质就是:以类的方式组织代码,以对象的组织(封装)数据抽象:把一类事物的公共属性提取......