平时常用的一些java方法,请留意
平时常用的一些java方法,请留意。
package com.util; import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class SuperUitl { public static void main(String[] args) {
System.out.println();
} /**
* 全角转半角
* trr 要转换成半角的字符串
*/
public static String change(String str) {
String outStr="";
String test="";
byte[] code = null; for(int i=0;i<str.length();i++) {
try {
test = str.substring(i,i+1);
code = test.getBytes("unicode");
} catch(java.io.UnsupportedEncodingException e) {
}
if (code[3] == -1) {
code[2] = (byte)(code[2]+32);
code[3] = 0; try {
outStr = outStr + new String(code,"unicode");
} catch(java.io.UnsupportedEncodingException e) {
}
} else {
outStr = outStr + test;
}
}
return outStr;
} /**
* 根据key读取value
* filePath 要操作的properties文件路径
* key 要获得数据的key
*/
public static String readValue(String filePath,String key) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
props.load(in);
String value = props.getProperty (key);
return value;
} catch (Exception e) {
return null;
}
} /**
* 读取properties的全部信息
* filePath 要操作的properties文件路径
*/
public static Map readProperties(String filePath) {
Map map = new HashMap();
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
props.load(in);
Enumeration en = props.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = props.getProperty (key);
map.put(key,Property);
}
return map;
} catch (Exception e) {
return null;
}
} /**
* 写入properties信息
* filePath 要操作的properties文件路径
* key 要写入的key
* value 要写入的value
*/
public static boolean writeProperties(String filePath,String key,String value) {
Properties prop = new Properties();
try {
InputStream fis = new FileInputStream(filePath);
//从输入流中读取属性列表(键和元素对)
prop.load(fis);
//调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
//强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream fos = new FileOutputStream(filePath);
prop.setProperty(key,value);
//以适合使用 load 方法加载到 Properties 表中的格式,
//将此 Properties 表中的属性列表(键和元素对)写入输出流
prop.store(fos, "Update '" + key + "' value");
return true;
} catch (IOException e) {
return false;
}
} /**
* 返回标准系统时间
*/
public static String getDate() {
SimpleDateFormat ft=null;
Date date=null;
Calendar cl= Calendar.getInstance();
cl.setTime(new java.util.Date());
date=cl.getTime();
ft=new SimpleDateFormat("yyyy-MM-dd HH:mm");
String dateTime = ft.format(date);
return dateTime;
} /**
* 从指定的字符串中提取Email
* content 指定的字符串
*/
public static String parse(String content) {
String email = null;
if (content==null || content.length()<1) {
return email;
}
//找出含有@
int beginPos;
int i;
String token = "@";
String preHalf="";
String sufHalf = ""; beginPos = content.indexOf(token);
if (beginPos>-1) {
//前项扫描
String s = null;
i= beginPos;
while(i>0) {
s = content.substring(i-1,i);
if (isLetter(s))
preHalf = s+preHalf;
else
break;
i--;
}
//后项扫描
i= beginPos+1;
while( i<content.length()) {
s = content.substring(i,i+1);
if (isLetter(s))
sufHalf = sufHalf +s;
else
break;
i++;
}
//判断合法性
email = preHalf + "@" + sufHalf;
if (isEmail(email)) {
return email;
}
}
return null;
} /**
* 判断是不是合法Email
* email Email地址
*/
public static boolean isEmail(String email) {
try {
if (email==null || email.length()<1 || email.length()>256) {
return false;
} String check = "^([0-9a-zA-Z]+[_.0-9a-zA-Z-]+)@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2,3})$";
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher(email);
boolean isMatched = matcher.matches();
if(isMatched) {
return true;
} else {
return false;
}
} catch (RuntimeException e) {
return false;
}
} /**
* 判断是不是合法字符
* c 要判断的字符
*/
public static boolean isLetter(String c) {
boolean result = false; if (c==null || c.length()<0) {
return false;
}
//a-z
if (c.compareToIgnoreCase("a")>=0 && c.compareToIgnoreCase("z")<=0) {
return true;
}
//0-9
if (c.compareToIgnoreCase("0")>=0 && c.compareToIgnoreCase("9")<=0) {
return true;
}
//. - _
if (c.equals(".") || c.equals("-") || c.equals("_") ) {
return true;
}
return result;
} /**
* 删除整个目录的全部图片
* filePath 要删除的目录路径
*/
public static boolean deleteImage(String filePath) {
try {
File file = new File(filePath);
File[] files = file.listFiles();
for(int i=0;i<files.length;i++) {
try {
//系统文件不删除
if(!(files[i].getName()).equalsIgnoreCase("Thumbs.db")) {
if(files[i].isFile()) {
files[i].delete();
} else if(files[i].isDirectory()) {
files[i].delete();
} else {
files[i].delete();
}
}
} catch (RuntimeException e) {;
}
}
return true;
} catch (RuntimeException e) {
return false;
}
} /**
* 保存网络上的图片到指定目录
* filePath 要保存到本地服务器的目录
* imagePath 网络图片的UIL地址
*/
public static boolean saveImage(String filePath,String imagePath) {
try {
if(imagePath.length()>1024 || imagePath.equals("")) {
return false;
}
String fileName = imagePath.substring(imagePath.lastIndexOf("/")+1,imagePath.length());
filePath = filePath+fileName;
URL url = null;
try {
url = new URL(imagePath);
} catch(Exception e) {
return false;
}
FilterInputStream in=(FilterInputStream) url.openStream();
File fileOut=new File(filePath);
FileOutputStream out=new FileOutputStream(fileOut);
byte[] bytes=new byte[1024];
int c;
while((c=in.read(bytes))!=-1) {
out.write(bytes,0,c);
}
in.close();
out.close();
return true;
} catch(Exception e) {
return false;
}
} /**
* 写入日志
* filePath 日志文件的路径
* code 要写入日志文件的内容
*/
public static boolean print(String filePath,String code) {
try {
File tofile=new File(filePath);
FileWriter fw=new FileWriter(tofile,true);
BufferedWriter bw=new BufferedWriter(fw);
PrintWriter pw=new PrintWriter(bw); System.out.println(getDate()+":"+code);
pw.println(getDate()+":"+code);
pw.close();
bw.close();
fw.close();
return true;
} catch (IOException e) {
return false;
}
} /**
* 判断是不是合法手机
* handset 手机号码
*/
public static boolean isHandset(String handset) {
try {
if(!handset.substring(0,1).equals("1")) {
return false;
}
if (handset==null || handset.length()!=11) {
return false;
}
String check = "^[0123456789]+$";
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher(handset);
boolean isMatched = matcher.matches();
if(isMatched) {
return true;
} else {
return false;
}
} catch (RuntimeException e) {
return false;
}
}
}
平时常用的一些java方法,请留意的更多相关文章
- java中常用的包、类、以及包中常用的类、方法、属性----sql和text\swing
java中常用的包.类.以及包中常用的类.方法.属性 常用的包 java.io.*; java.util.*; java.lang.*; java.sql.*; java.text.*; java.a ...
- java十五个常用类学习及方法举例
<code class="language-java">import java.util.Scanner; import java.util.Properties; i ...
- java 获取键盘输入常用的两种方法
java 获取键盘输入常用的两种方法 方法1: 通过 Scanner Scanner input = new Scanner(System.in); String s = input.nextLine ...
- JAVA基础(二)—— 常用的类与方法
JAVA基础(二)-- 常用的类与方法 1 Math类 abs ceil floor 绝对值 大于等于该浮点数的最小整数 小于等于该浮点数的最大整数 max min round 两参数中较大的 两参数 ...
- Atitit java方法引用(Method References) 与c#委托与脚本语言js的函数指针
Atitit java方法引用(Method References) 与c#委托与脚本语言js的函数指针 1.1. java方法引用(Method References) 与c#委托与脚本语言js ...
- python基础:os模块中关于文件/目录常用的函数使用方法
Python是跨平台的语言,也即是说同样的源代码在不同的操作系统不需要修改就可以同样实现 因此Python的作者就倒腾了OS模块这么一个玩意儿出来,有了OS模块,我们不需要关心什么操作系统下使用什么模 ...
- os模块中关于文件/目录常用的函数使用方法
os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件名('. ...
- 18 os/os.path模块中关于文件/目录常用的函数使用方法 (转)
os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件名('. ...
- os、os.path模块中关于文件、目录常用的函数使用方法
os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举 ...
随机推荐
- 基于jquery 的分页插件,前端实现假分页效果
上次分享了一款jquery插件,现在依旧分享这个插件,不过上一次分享主要是用于regular框件,且每一页数据都是从后端获取过来的,这一次的分享主要是讲一次性获取完数据 然后手动进行分页.此需求基本上 ...
- Unity Debug类
静态变量 developerConsoleVisible 报告是否开发控制台是可见的.开发控制台不能出现使用: isDebugBuild 在构建设置对话框中有一个叫做"发展构建"复 ...
- EzHttp 使用Https协议时证书如何部署
今天为EzHttp增加了https支持, EzHttp介绍见这里:使用EzHttp框架 开发基于HTTP协议的CS轻应用 服务端启动时会创建自签名证书,并将其绑定到启动参数url对应的端口上. 服务端 ...
- HTML ——Flex弹性布局
弹性盒布局的使用 1.为父容器添加display:flex或inline-flex属性 (Webkit内核的浏览器,必须加上-webkit前缀.) 容器默认存在两根轴:主轴(main axis)和交叉 ...
- react+redux+generation-modation脚手架添加一个todolist
当我遇到问题: 要沉着冷静. 要管理好时间. 别被bug或error搞的不高兴,要高兴,又有煅炼思维的机会了. 要思考这是为什么? 要搞清楚问题的本质. 要探究问题,探究数据的流动. TodoList ...
- NodeJs之fs的读写删移监
NodeJs版本:4.4.4 fs 文件系统模块是一个封装了标准的 POSIX 文件 I/O 操作的集合.Node.js 文件系统(fs 模块)模块中的方法均有异步和同步版本. 图片的复制与粘贴 创建 ...
- Java中File
1.什么是流? Java中的流是个抽象的概念,当程序需要从某个数据源读入数据的时候,就会开启一个数据流,数据源可以是文件.内存或网络等等.2.使用File类操作文件或目录属性 public class ...
- vue视频学习笔记
video 7 vue问题: 论坛 http://bbs.zhinengshe.com------------------------------------------------UI组件 别人提供 ...
- [刷题]算法竞赛入门经典(第2版) 5-7/UVa12100 - Printer Queue
题意:一堆文件但只有一个打印机,按优先级与排队顺序进行打印.也就是在一个可以插队的的队列里,问你何时可以打印到.至于这个插队啊,题目说"Of course, those annoying t ...
- Maven学习-简介、安装
Maven是一个项目管理工具,它包含了一个项目对象模型,一组标准集合,一个项目声明周期,一个依赖管理系统和用来运行定义在生命周期阶段中插件目标的逻辑.Maven采用了约定优于配置这一基本原则.在没有自 ...