




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、机务维修系统工一对字符串的操作(字符串操作类) 1.public String concat(String str); 将指定的字符串链接到此字符串结尾,如果参数字符串的长度为 0,则返回此 String 对象。可用于系统中对单号的结尾补零操作。 例:String new_number=”OR_201603_078”.contat(“000”) ; 返回值:new_number= “OR_201603_078000”; 2.public static StringBuffer connetString(String str1,String str2,String partStr1) thows
2、 NullPointerException StringBuffer s1 = new StringBuffer(str1); Pattern p = Ppile(partStr1); Matcher m = p.matcher(s1.toString();if(m.find() s1.insert(m.start(), str2); return s1; 该方法是对两个字符串的融合操作,返回的是一个组合后的字符串,可用于系统中对单号生成的操作 例:String new_number=connetString(”OR_201603_078” ,”000”,”OR”); 返回值:new_numb
3、er=”000OR_201603_078” 3.public String subString(int beginIndex,int endIndex); 返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。可以用于系统中对单号的截取操作 例:String new_number=”OR_201603_078” .subString(2,6) ; 返回值:new_number= “_201”; 4.public String split(String str);根据给定的字符串来拆分此字符串,如果此字符串不
4、包含给定字符串则还是返回此字符串,如果包含则返回一个字符串数组。可用于对系统中的单号进行一定规则的拆分。例:String new_number=”OR_201603_078” .split(String str);返回值(遍历):new_number=OR,201603,078; 5.public boolean isEmpty(); 判断某个字符串是否为空,当且仅当字符串的 length() 为 0 时返回 true。 例:boolean flag=”OR_201603_078” .isEmpty(); 返回值:flag=false; 6.public String toUpperCase(
5、); 使用默认的语音环境的规则将此字符串所有的字符都转化成大写。 例:String new_number=”OR_201603_078” .toUpperCase(); 返回值:new_number=”OR_201603_078”; 7.public String toLowerCase(); 使用默认的语音环境的规则将此字符串所有的字符都转化成小写。 例:String new_number=”OR_201603_078” .toUpperCase(); 返回值:new_number=”or_201603_078”; 8.public String replaceAll(“ ”,”); 字符串
6、替换,去掉字符串中所有的空格,返回该字符串副本。 例:order_number on =” OR_201 60 3_078 ” ; String new_number=on.replaceAll(); 返回值:new_number=”OR_201 60 3_078”; 9.public String format(Locale l, String format, Object. args); 使用指定的语言环境、格式字符串和参数返回一个格式化字符串。 例:order_number on =”OR_201603_078” ; String new_number=on.format(“生成的订单编
7、号是:s%”,”on”)返回值: new=”生成的订单编号是:OR_201603_078”;注:不同转换符实现不同数据类型到字符串的转换转 换 符说 明 示 例%s字符串类型"mingrisoft"%c字符类型'm'%b布尔类型true%o整数类型(八进制)77%f浮点类型99.99%e指数类型9.38e+5%g通用浮点类型(f和e类型中较短的) %百分比类型%n换行符 %tx日期与时间类型(x代表不同的日期与时间转换符 。
8、 10.public static Date getDate(String str) throws Exception SimpleDateFormat format=new SimpleDateFormat("MM,yyyy/dd"); Date date = format.parse(str); return date; 将指定的字符串通过指定的格式转化成需要的日期格式,返回值将以日期返回例:String str=”08,2016/23”; Date date=getDate(str);返回值:date=Tue Aug 23 00:00:00 GMT+08:00 201
9、62 对时间和日期的操作(时间日期操作类) 1.public String format(Date date) throws Exception;将给定的日期的按照给定的格式进行格式化,返回一个格式化之后的日期字符串。 2. public static String getSomeDay() Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE,int count); return new SimpleDateFormat("yyyy-MM-dd").format(calendar.ge
10、tTime(); 根据日历的规则,为给定的日历字段添加或减去指定的时间量。返回的是一个计算后的日 期的字符串。 例:取当前环境下的日历规则 返回值(5天前的日期):String str=”2016-07-17”; 3.public static String getCalendar() Calendar calendar=Calendar.getInstance() calendar.setTime( new Date(); / 显示年份 int year = calendar.get(Calendar.YEAR); return year; / 显示月份 (从0开始, 实际显示要加一) in
11、t month = calendar.get(Calendar.MONTH); return month; / 本周几 int week = calendar.get(Calendar.DAY_OF_WEEK); return week; / 今年的第 N 天 int DAY_OF_YEAR = calendar.get(Calendar.DAY_OF_YEAR); return DAY_OF_YEAR; 主要是日历Calendar中的方法,可以将当前的时间通过不同的方式进行操作,便于在系统开发中针对不同需求对时间日期的操作 4.public static long DateToLong(Da
12、te time) throws Exception long timeL = time.getTime(); return timeL 将时间对象转成时间戳,参数是一个日期,返回值是一个long的基础数据对象 5.public static Date valueOf(String str); 将 JDBC 日期转义形式的字符串转换成 Date 值。 6.public int compareTo(Date anotherDate);比较两个日期的先后顺序,参数表示要比较的一个日期说明:参数 Date 等于此 Date,则返回值 0;如果此 Date 在 Date 参数之前,则返回小于 0 的值;
13、如果此 Date 在 Date 参数之后,则返回大于 0 的值。3 文件操作工具类 目录操作工具类 说明:1,建立目的目录。 2,遍历源目录。 3,遍历过程中,创建文件或者文件夹。 原理:其实就是改变了源文件或者目录的目录头。 1. public class CopyDir private File sDir, dDir, newDir; public CopyDir(String s, String d) this(new File(s), new File(d); CopyDir(File sDir, File dDir) this.sDir = sDir;
14、this.dDir = dDir; public void copyDir() throws IOException String name = sDir.getName(); newDir = dDir; newDir.mkdir(); listAll(sDir); /调用下面的方法遍历源文件夹 说明:将遍历目录封装成方法,在遍历过程中创建文件或目录 1.1 private void listAll(File dir) throws IOException File files = dir.listFiles(); for (int x = 0; x < files.length; x
15、+) if (filesx.isDirectory() createDir(filesx); / 调用创建目录的方法。 listAll(filesx); / 在继续进行递归。进入子级目录。 else createFile(filesx); / 调用创建文件的方法。 说明: copy目录。通过源目录在目的目录创建新目录。 1.2 private void createDir(File dir) File d = replaceFile(dir); d.mkdir(); 说明: copy文件。 1.3 private void createFile(File file) throws IOExce
16、ption File newFile = replaceFile(file); / copy文件是一个数据数据传输的过程。需要通过流来完成。 FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(newFile); byte buf = new byte1024 * 2; int num = 0; while (num = fis.read(buf) != -1) fos.write(buf, 0, num); fos.close(); fis.close();
17、说明:替换路径。 1.4 private File replaceFile(File f) String path = f.getAbsolutePath(); String newPath = path.replace(sDir.getAbsolutePath(), newDir .getAbsolutePath(); /将源文件或文件夹的绝对路径替换成目的路径 File newFile = new File(newPath); /将新的目录封装成file对象 return newFile; 文件目录部分处理工具类 说明:文件目录的部分处理 2.public class DealDir 说明
18、:获取文件的后缀名并转化成大写 2.1 public String getFileSuffix(String fileName) throws Exception return fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length().toUpperCase(); 说明:创建多级目录 2.2 public void createMultilevelDir(String path) throws Exception StringTokenizer st = new StringTokenizer(p
19、ath, "/"); String path1 = st.nextToken() + "/" String path2 = path1; while (st.hasMoreTokens() path1 = st.nextToken() + "/" path2 += path1; File inbox = new File(path2); if (!inbox.exists() inbox.mkdir(); 说明:删除文件/目录(递归删除文件/目录) 2.3 public void deleteAll(String dirpath) t
20、hrows Excpetion if (dirpath = null) System.out.println("目录为空"); else File path = new File(dirpath); if (!path.exists() return; / 目录不存在退出 if (path.isFile() / 如果是文件删除 path.delete(); return; File files = path.listFiles(); / 如果目录中有文件递归删除文件 for (int i = 0; i < files.length; i+) deleteAll(fil
21、esi.getAbsolutePath(); path.delete(); 说明:文件/目录 重命名(参数类型:旧的绝对路径,新的路径) 2.4 public void renameDir(String oldPath, String newPath) File oldFile = new File(oldPath); / 文件或目录 File newFile = new File(newPath); / 文件或目录 boolean success = oldFile.renameTo(newFile); / 重命名 if (!success) System.out.println("
22、;重命名失败"); else System.out.println("重命名成功"); 删除文件夹工具类说明:删除文件夹的操作 3.public class DeleteFolder 说明:删除文件夹 3.1 public static void delFolder(String folderPath) delAllFile(folderPath); / 删除完里面所有内容 String filePath = folderPath; filePath = filePath.toString(); java.io.File myFilePath = new java
23、.io.File(filePath); myFilePath.delete(); / 删除空文件夹 说明: 删除指定文件夹下所有文件(参数为文件夹完整的绝对路径) 3.2 public static boolean delAllFile(String path)throws Exception boolean flag = false; File file = new File(path); if (!file.exists() return flag; if (!file.isDirectory() return flag; String tempList = file.list(); Fi
24、le temp = null; for (int i = 0; i < tempList.length; i+) if (path.endsWith(File.separator) temp = new File(path + tempListi); else temp = new File(path + File.separator + tempListi); if (temp.isFile() temp.delete(); if (temp.isDirectory() delAllFile(path + "/" + tempListi); / 先删除文件夹里面的文
25、件 delFolder(path + "/" + tempListi); / 再删除空文件夹 flag = true; return flag; FTP文件工具类 说明:FTP链接 4.public class FtpUtil private String localfilename; / 本地文件名private String remotefilename; / 远程文件名private FtpClient ftpClient; / FTP客户端说明: 服务器连接 param ip 服务器IP param port 服务器端口 param user 用户名 param p
26、assword 密码 param path 服务器路径 4.1public void connectServer(String ip, int port, String user,String password, String path) throws FtpProtocolException try ftpClient = FtpClient.create(ip);ftpClient.login(user, null, password);/ 设置成2进制传输ftpClient.setBinaryType();System.out.println("login success!&q
27、uot;);if (path.length() != 0) / 把远程系统上的目录切换到参数path所指定的目录ftpClient.changeDirectory(path);ftpClient.setBinaryType(); catch (IOException ex) ex.printStackTrace();throw new RuntimeException(ex); 说明:关闭链接4.2public void closeConnect() try ftpClient.close();System.out.println("disconnect success")
28、; catch (IOException ex) System.out.println("not disconnect");ex.printStackTrace();throw new RuntimeException(ex); 说明:上传文件 param localFile 本地文件 param remoteFile 远程文件4.3 public void upload(String localFile, String remoteFile)throws FtpProtocolException this.localfilename = localFile;this.re
29、motefilename = remoteFile;TelnetOutputStream os = null;FileInputStream is = null;try / 将远程文件加入输出流中os = (TelnetOutputStream) ftpClient.putFileStream(this.remotefilename, true);/ 获取本地文件的输入流File file_in = new File(this.localfilename);is = new FileInputStream(file_in);/ 创建一个缓冲区byte bytes = new byte1024;int c;while (c = is.read(bytes) != -1) os.write(bytes, 0, c);System.out.println("upload success"); catch (IOException ex) System.out.println("not upload");ex.printStackTrace();throw new RuntimeExcepti
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年福建事业单位考试中的学习动力与主动性提升方法试题及答案
- 焦作国资面试题目及答案
- 坚持不懈2024年福建事业单位考试试题及答案
- 2024年农艺师考试学习材料的选择与归纳总结试题及答案
- 卖凉面摆摊测试题及答案
- 2024年高校辅导员岗位胜任性试题及答案
- 农艺师考试考纲解读试题及答案
- 农业职业经理人考试的心理准备与应对方法试题及答案
- led销售安装合同样本
- 中学园艺师2024年考试中的实际操作试题及答案
- 红楼梦阅读单选题100道及答案解析
- 2024-2030年中国转子发动机行业市场深度调研及发展趋势与投资前景研究报告
- 牧场物语-矿石镇的伙伴们-完全攻略
- 汽车营销知识竞赛题库及答案(295题)
- 医学教材单克隆抗体药物在肾脏疾病中的应用
- 肾病综合征的实验室检查
- 2024年河北省邢台市中考一模理综物理试题(解析版)
- 实习护生社会焦虑情况调查量表
- SL-T+712-2021河湖生态环境需水计算规范
- 深基坑专项方案论证流程
- 《创业基础》课件-第五章 创业计划
评论
0/150
提交评论