栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java常用工具类 IP、File文件工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java常用工具类 IP、File文件工具类

本文实例为大家分享了java常用工具类的具体实现代码,供大家参考,具体内容如下

IP工具类

package com.jarvis.base.util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;


public class IpMacUtil {

 
 public static String hideIp(String ip) {
 if (StringHelper.isEmpty(ip)) {
 return "";
 }

 int pos = ip.lastIndexOf(".");
 if (pos == -1) {
 return ip;
 }

 ip = ip.substring(0, pos + 1);
 ip = ip + "*";
 return ip;
 }

 
 // public static String getIpAddr(HttpServletRequest request)
 // {
 // String ip = request.getHeader("x-forwarded-for");
 // if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
 // {
 // ip = request.getHeader("Proxy-Client-IP");
 // }
 // if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
 // {
 // ip = request.getHeader("WL-Proxy-Client-IP");
 // }
 // if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
 // {
 // ip = request.getRemoteAddr();
 // }
 // return ip;
 // }

 
 public static boolean isIP(String ipStr) {
 String ip = "(25[0-5]|2[0-4]\d|1\d\d|\d\d|\d)";
 String ipDot = ip + "\.";
 return ipStr.matches(ipDot + ipDot + ipDot + ip);
 }

 
 public static String getMACAddress(String ip) {
 String str = "";
 String macAddress = "";
 try {
 Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
 InputStreamReader ir = new InputStreamReader(p.getInputStream());
 LineNumberReader input = new LineNumberReader(ir);
 for (int i = 1; i < 100; i++) {
 str = input.readLine();
 if (str != null) {
  if (str.indexOf("MAC Address") > 1) {
  macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length());
  break;
  }
 }
 }
 } catch (IOException e) {
 return "";
 }
 return macAddress;
 }

}

File文件工具类

package com.jarvis.base.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.StringTokenizer;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;

import org.apache.commons.io.FilenameUtils;
import org.dom4j.document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;


public class FileHelper {

 
 private final static int KB_1 = 1024;

 
 public static String getFileCRCCode(File file) throws Exception {
 FileInputStream is = new FileInputStream(file);
 CRC32 crc32 = new CRC32();
 CheckedInputStream cis = new CheckedInputStream(is, crc32);
 byte[] buffer = null;
 buffer = new byte[KB_1];
 while (cis.read(buffer) != -1) {
 }
 is.close();
 buffer = null;
 return Long.toHexString(crc32.getValue());
 }

 
 public static String getStringCRCCode(String string) throws Exception {
 ByteArrayInputStream inputStream = new ByteArrayInputStream(string.getBytes());
 CRC32 crc32 = new CRC32();
 CheckedInputStream checkedinputstream = new CheckedInputStream(inputStream, crc32);
 byte[] buffer = null;
 buffer = new byte[KB_1];
 while (checkedinputstream.read(buffer) != -1) {
 }
 inputStream.close();
 buffer = null;
 return Long.toHexString(crc32.getValue());
 }

 
 public static String concat(String basePath, String fullFilenameToAdd) {
 return FilenameUtils.concat(basePath, fullFilenameToAdd);
 }

 
 public static String getbaseName(String filename) {
 return FilenameUtils.getbaseName(filename);
 }

 
 public static String getFileName(String filename) {
 return FilenameUtils.getName(filename);
 }

 
 public static String getFullPath(String filename) {
 return FilenameUtils.getFullPath(filename);
 }

 
 public static String getFullPathNoEndSeparator(String filename) {
 return FilenameUtils.getFullPathNoEndSeparator(filename);
 }

 
 public static boolean isExtension(String filename, String extension) {
 return FilenameUtils.isExtension(filename, extension);
 }

 
 public static boolean isExtension(String filename, String[] extensions) {
 return FilenameUtils.isExtension(filename, extensions);
 }

 
 public static String normalize(String filename) {
 return FilenameUtils.normalize(filename);
 }

 
 public static String normalizeNoEndSeparator(String filename) {
 return FilenameUtils.normalizeNoEndSeparator(filename);
 }

 
 public static String separatorsToUnix(String path) {
 return FilenameUtils.separatorsToUnix(path);
 }

 
 public static String separatorsToWindows(String path) {
 return FilenameUtils.separatorsToWindows(path);
 }

 
 public static String separatorsToSystem(String path) {
 return FilenameUtils.separatorsToSystem(path);
 }

 
 public static String getExtension(String filename) {
 return FilenameUtils.getExtension(filename);
 }

 
 public static String removeExtension(String filename) {
 return FilenameUtils.removeExtension(filename);
 }

 
 public static boolean cleanDirectory(File directory) {
 try {
 org.apache.commons.io.FileUtils.cleanDirectory(directory);
 return true;
 } catch (IOException ex) {
 ex.printStackTrace();
 System.err.println("清除目录出错");
 }
 return false;
 }

 
 public static boolean copyDirectory(File srcDir, File destDir) {
 return copyDirectory(srcDir, destDir, true);
 }

 
 public static boolean copyDirectory(String srcDir, String destDir) {
 return copyDirectory(new File(srcDir), new File(destDir));
 }

 
 public static boolean copyDirectory(File srcDir, File destDir, boolean preserveFileDate) {
 try {
 org.apache.commons.io.FileUtils.copyDirectory(srcDir, destDir, preserveFileDate);
 return true;
 } catch (IOException ex) {
 ex.printStackTrace();
 System.err.println("复制目录出错");
 }
 return false;
 }

 
 public static boolean copyDirectoryToDirectory(File srcDir, File destDir) {
 try {
 org.apache.commons.io.FileUtils.copyDirectoryToDirectory(srcDir, destDir);
 return true;
 } catch (IOException ex) {
 ex.printStackTrace();
 System.err.println("复制目录出错");
 }
 return false;
 }

 
 public static boolean copyDirectoryToDirectory(String srcDir, String destDir) {
 return copyDirectoryToDirectory(new File(srcDir), new File(destDir));
 }

 
 public static boolean copyFile(File srcFile, File destFile) {
 return copyFile(srcFile, destFile, true);
 }

 
 public static boolean copyFile(String srcFile, String destFile) {
 return copyFile(new File(srcFile), new File(destFile));
 }

 
 public static boolean copyFile(File srcFile, File destFile, boolean preserveFileDate) {
 try {
 org.apache.commons.io.FileUtils.copyFile(srcFile, destFile, preserveFileDate);
 return true;
 } catch (IOException ex) {
 ex.printStackTrace();
 System.err.println("复制文件出错");
 }
 return false;
 }

 
 public static boolean copyFileToDirectory(File srcFile, File destDir) {
 try {
 org.apache.commons.io.FileUtils.copyFileToDirectory(srcFile, destDir);
 return true;
 } catch (IOException ex) {
 ex.printStackTrace();
 System.err.println("复制文件出错");
 }
 return false;
 }

 
 public static boolean copyFileToDirectory(String srcFile, String destDir) {
 return copyFileToDirectory(new File(srcFile), new File(destDir));
 }

 
 public static boolean deleteDirectory(String directory) {
 try {
 org.apache.commons.io.FileUtils.deleteDirectory(new File(directory));
 return true;
 } catch (IOException ex) {
 ex.printStackTrace();
 System.err.println("删除目录出错");
 }
 return false;
 }

 
 public static boolean deleteFile(String file) {
 try {
 org.apache.commons.io.FileUtils.forceDelete(new File(file));
 return true;
 } catch (IOException ex) {
 ex.printStackTrace();
 System.err.println("删除文件出错");
 }
 return false;
 }

 
 public static boolean createDirectory(String directory) {
 try {
 org.apache.commons.io.FileUtils.forceMkdir(new File(directory));
 return true;
 } catch (IOException ex) {
 ex.printStackTrace();
 System.err.println("创建目录出错");
 }
 return false;
 }

 
 public static byte[] readFileToByteArray(String file) {
 try {
 byte[] bytes = org.apache.commons.io.FileUtils.readFileToByteArray(new File(file));
 return bytes;
 } catch (IOException ex) {
 ex.printStackTrace();
 System.err.println("读取文件出错");
 }
 return null;
 }

 
 public static String readFileToString(String file, String encoding) {
 try {
 if (StringHelper.isEmpty(encoding)) {
 encoding = "GBK";
 }
 String content = org.apache.commons.io.FileUtils.readFileToString(new File(file), encoding);
 return content;
 } catch (IOException ex) {
 ex.printStackTrace();
 System.err.println("读取文件出错");
 }
 return "";
 }

 
 public static String readFileToString(String file) {
 return readFileToString(file, "GBK");
 }

 
 @SuppressWarnings("rawtypes")
 public static List readLines(String file) {
 return readLines(file, "GBK");
 }

 
 @SuppressWarnings("rawtypes")
 public static List readLines(String file, String encoding) {

 try {
 if (StringHelper.isEmpty(encoding)) {
 encoding = "GBK";
 }
 List lineList = org.apache.commons.io.FileUtils.readLines(new File(file), encoding);
 return lineList;
 } catch (IOException ex) {
 ex.printStackTrace();
 System.err.println("读取文件出错");
 }
 return null;

 }

 
 public static long sizeOfDirectory(String directory) {
 return org.apache.commons.io.FileUtils.sizeOfDirectory(new File(directory));
 }

 
 public static boolean writeToFile(String file, byte[] data) {
 try {
 org.apache.commons.io.FileUtils.writeByteArrayToFile(new File(file), data);
 return true;
 } catch (IOException ex) {
 ex.printStackTrace();
 System.err.println("写文件出错");
 }
 return false;
 }

 
 public static boolean writeToFile(String file, String data) {
 return writeToFile(file, data, "GBK");
 }

 
 public static boolean writeToFile(String file, String data, String encoding) {
 try {
 if (encoding == null || "".equals(encoding)) {
 encoding = "GBK";
 }
 org.apache.commons.io.FileUtils.writeStringToFile(new File(file), data, encoding);
 return true;
 } catch (IOException ex) {
 ex.printStackTrace();
 System.err.println("写文件出错");
 }
 return false;
 }

 
 public static boolean createNewFile(String filePathName) {
 String filePath = FileHelper.getFullPath(filePathName);
 // 若目录不存在,则建立目录
 if (!FileHelper.exists(filePath)) {
 if (!createDirectory(filePath)) {
 return false;
 }
 }

 try {
 File file = new File(filePathName);
 return file.createNewFile();
 } catch (IOException ex) {
 ex.printStackTrace();
 System.err.println("创建文件出错");
 return false;
 }
 }

 
 public static boolean exists(String filePath) {
 File file = new File(filePath);
 return file.exists();
 }

 
 public static boolean isFile(String filePath) {
 File file = new File(filePath);
 return file.isFile();
 }

 
 public static boolean isDirectory(String filePath) {
 File file = new File(filePath);
 return file.isDirectory();
 }

 
 public static boolean renameTo(String srcFile, String destFile) {
 File file = new File(srcFile);
 return file.renameTo(new File(destFile));
 }

 
 public static boolean WriteToXMLFile(String fileName, document document, String encoding) {
 createNewFile(fileName);
 boolean success = false;
 
 OutputFormat format = OutputFormat.createPrettyPrint();
 
 format.setEncoding(encoding);
 XMLWriter writer = null;
 try {
 
 writer = new XMLWriter(new FileOutputStream(new File(fileName)), format);
 writer.write(document);
 writer.flush();
 success = true;
 
 } catch (Exception ex) {
 ex.printStackTrace();
 System.err.println("写文件出错");
 } finally {
 if (writer != null) {
 try {
  writer.close();
  writer = null;
 } catch (IOException e) {
  e.printStackTrace();
  System.err.println("Convert code Error");
 }
 }
 }
 return success;
 }

  
 public String getFileSuffix(String fileName) throws Exception { 
  return fileName.substring(fileName.lastIndexOf(".") + 1, 
    fileName.length()).toUpperCase(); 
 } 
 
  
 public void createMultilevelDir(String path) { 
  try { 
   StringTokenizer st = new StringTokenizer(path, "/"); 
   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(); 
 
   } 
  } catch (Exception e) { 
   System.out.println("目录创建失败" + e); 
   e.printStackTrace(); 
  } 
 
 } 
 
  
 public void deleteAll(String dirpath) { 
  if (dirpath == null) { 
   System.out.println("目录为空"); 
  } else { 
   File path = new File(dirpath); 
   try { 
    if (!path.exists()) 
     return;// 目录不存在退出 
    if (path.isFile()) // 如果是文件删除 
    { 
     path.delete(); 
     return; 
    } 
    File[] files = path.listFiles();// 如果目录中有文件递归删除文件 
    for (int i = 0; i < files.length; i++) { 
     deleteAll(files[i].getAbsolutePath()); 
    } 
    path.delete(); 
 
   } catch (Exception e) { 
    System.out.println("文件/目录 删除失败" + e); 
    e.printStackTrace(); 
   } 
  } 
 } 
 
  
 public void renameDir(String oldPath, String newPath) { 
  File oldFile = new File(oldPath);// 文件或目录 
  File newFile = new File(newPath);// 文件或目录 
  try { 
   boolean success = oldFile.renameTo(newFile);// 重命名 
   if (!success) { 
    System.out.println("重命名失败"); 
   } else { 
    System.out.println("重命名成功"); 
   } 
  } catch (RuntimeException e) { 
   e.printStackTrace(); 
  } 
 
 } 
 
  
 public static boolean newDir(String path) throws Exception { 
  File file = new File(path); 
  return file.mkdirs();//创建目录 
 } 
  
  
 public static boolean deleteDir(String path) throws Exception { 
  File file = new File(path); 
  if (!file.exists()) 
   return false;// 目录不存在退出 
  if (file.isFile()) // 如果是文件删除 
  { 
   file.delete(); 
   return false; 
  } 
  File[] files = file.listFiles();// 如果目录中有文件递归删除文件 
  for (int i = 0; i < files.length; i++) { 
   deleteDir(files[i].getAbsolutePath()); 
  } 
  file.delete(); 
   
  return file.delete();//删除目录 
 } 
 
  
 public static boolean updateDir(String path, String newPath) throws Exception { 
  File file = new File(path); 
  File newFile = new File(newPath); 
  return file.renameTo(newFile); 
 }
 
 // 删除文件夹 
 // param folderPath 文件夹完整绝对路径 
 public static void delFolder(String folderPath) { 
  try { 
   delAllFile(folderPath); // 删除完里面所有内容 
   String filePath = folderPath; 
   filePath = filePath.toString(); 
   java.io.File myFilePath = new java.io.File(filePath); 
   myFilePath.delete(); // 删除空文件夹 
  } catch (Exception e) { 
   e.printStackTrace(); 
  } 
 } 
 
 // 删除指定文件夹下所有文件 
 // param path 文件夹完整绝对路径 
 public static boolean delAllFile(String path) { 
  boolean flag = false; 
  File file = new File(path); 
  if (!file.exists()) { 
   return flag; 
  } 
  if (!file.isDirectory()) { 
   return flag; 
  } 
  String[] tempList = file.list(); 
  File temp = null; 
  for (int i = 0; i < tempList.length; i++) { 
   if (path.endsWith(File.separator)) { 
    temp = new File(path + tempList[i]); 
   } else { 
    temp = new File(path + File.separator + tempList[i]); 
   } 
   if (temp.isFile()) { 
    temp.delete(); 
   } 
   if (temp.isDirectory()) { 
    delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件 
    delFolder(path + "/" + tempList[i]);// 再删除空文件夹 
    flag = true; 
   } 
  } 
  return flag; 
 } 
 
 
 
  
 public static void main(String d[]) throws Exception{ 
  //deleteDir("d:/ff/dddf"); 
  updateDir("D:\TOOLS\Tomcat 6.0\webapps\BCCCSM\nationalExperiment/22222", "D:\TOOLS\Tomcat 6.0\webapps\BCCCSM\nationalExperiment/224222"); 
 } 
  
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/138083.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号