⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tools.java

📁 文件打包解包文件打包解包文件打包解包文件打包解包文件打包解包
💻 JAVA
字号:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;


/**
 * 
 * @author jerry
 *  辅助工具类
 */
public class Tools {

	/**
	 * 写文件
	 * @param filePath
	 * @param str
	 */
	public static void printFile(String filePath, byte[] str) {
		try {
			System.out.println("into printFile path::"+filePath);
			File file = new File(filePath);
			File fileP = new File(file.getParent());
			if(!fileP.exists()) {
				fileP.mkdirs();
				System.out.println("mkdirs filePath ==>"+fileP);
			}

			if (file.exists())
				file.delete();
			file.createNewFile();
			// 创建输出流
			FileOutputStream fo = new FileOutputStream(file);
			fo.write(str);
			fo.flush();
			fo.close();
		} catch (IOException io) {
			io.printStackTrace();
		}
	}
	

	/**
	 * 将int 型转化为一维 byte数组
	 * @param i 为int型
	 * @return
	 */
	public static byte[] getByte1FromInt(int i) {
		byte[] b = new byte[1];
		b[0] = (byte) (i & 0xff);
		return b;
	}
	
	/**
	 * 将int 型转化为二维 byte数组
	 * @param i 为int型
	 * @return
	 */
	public static byte[] getByte2FromInt(int i) {
		byte[] b = new byte[2];
		b[0] = (byte) (i & 0xff);
		b[1] = (byte) (i >> 8 & 0xff);
		return b;
	}

	/**
	 * 将int 型转化为四维 byte数组
	 * @param i 为int型
	 * @return
	 */
	public static byte[] getByte4FromInt(int i) {
		byte[] b = new byte[4];
		b[0] = (byte) (i & 0xff);
		b[1] = (byte) (i >> 8 & 0xff);
		b[2] = (byte) (i >> 16 & 0xff);
		b[3] = (byte) (i >> 24 & 0xff);

		return b;
	}
	
	/**
	 * 将两个int 型按高低位转化为四维 byte数组
	 * @param i 为int型, j 为int型
	 * @return
	 */
	public static byte[] getByte4FromInt(int i,int j) {
		byte[] b = new byte[4];
		b[0] = (byte) (i & 0xff);
		b[1] = (byte) (i >> 8 & 0xff);
		b[2] = (byte) (j & 0xff);
		b[3] = (byte) (j >> 8 & 0xff);
		return b;
	}

	
	/**
	 * 对十六进制的字符串进行转化,转为大小为4的byte数组
	 * @param str 为十六进制字符串
	 * @return
	 */
	public static byte[] getB4fromString(String str) {
		int i=Integer.valueOf(str,16);
		byte[] B = getByte4FromInt(i);
		return B;
	}
	
	/**
	 * 对十六进制的字符串进行转化,转为大小为2的byte数组
	 * @param str 为十六进制字符串
	 * @return
	 */
	public static byte[] getB2fromString(String str) {
		int i=Integer.valueOf(str,16);
		byte[] B = getByte2FromInt(i);
		return B;
	}

	/**
	 * 将四维的字节数组转化为字符串
	 * @param B 为byte型数组
	 * @return
	 */
	public static String getStringfromB4(byte[] B) {
		int i=getFromByte4(B);
		String str=Integer.toHexString(i);
		return str;
	}
	
	
	/**
	 * 将数组大小为2的byte转化为int型
	 * @param B 为byte型
	 * @return
	 */
	public static int getFromByte2(byte[] B)
    {
        int ans;
        ans = ((int)B[0] & 0xff)<<0;
        ans +=((int)B[1] & 0xff)<<8;
        return ans;
    }

   
	
	public static int getFromByte4(byte[] B) {
		int ans;
		ans = ((int) B[0] & 0xff) << 0;
		ans += ((int) B[1] & 0xff) << 8;
		ans += ((int) B[2] & 0xff) << 16;
		ans += ((int) B[3] & 0xff) << 24;
		return ans;
	}
	
	/**
	   * 读取文件
	   * @param filePath
	   * @return
	   */
	  public static String readFile(String filePath){
		  String buf="";
		  try {
			  File f=new File(filePath);
			  if(f.exists()){
//				  FileInputStream inputtextfile=new FileInputStream(filePath); 
//				  int len = inputtextfile.available();		
//				  byte[] buffer=new byte[len]; 
//				  inputtextfile.read(buffer); 							
//				  inputtextfile.close(); 
//				  buf=new String(buffer);
				  
				  // 从字符流中获取文本并进行缓冲
				  BufferedReader br=readUTF8(filePath);//new BufferedReader(isr);
				  // 声明并建立 StringBuffer 变量,用于存储全部文本文件内容
				  StringBuffer sbContent=new StringBuffer();
				  // 声明 String 变量,用于临时存储文本行内容
				  String sLine;
				  // 循环读取文本文件每行内容
				  while((sLine=br.readLine())!=null){
				    // 去掉回车和换行符,去掉文本行前后空格,连接全部文本文件内容
				    sbContent=sbContent.append(sLine.replace("\n","").replace("\r","").trim());
				  }
				  // 输出文本文件内容
				  buf=new String(sbContent);
			  }
		  } catch (IOException e) {
				e.printStackTrace();
			} 
			return buf;
	  }
	  /**
	   * 读取文件
	   * @param filePath
	   * @return
	   */
	  public static byte[] readFile2(String filePath){
		  byte[] buffer=null;
		  try {
			  File f=new File(filePath);
			  if(f.exists()){
				  FileInputStream inputtextfile=new FileInputStream(filePath); 
				  int len = inputtextfile.available();		
				  buffer=new byte[len]; 
				  inputtextfile.read(buffer);
				  inputtextfile.close(); 
			  }
		  } catch (IOException e) {
				e.printStackTrace();
			} 
			return buffer;
	  }

	  /**
	   * 按行读取文件<br/>
	   * @param path 文件路径<br/>
	   * @param it 内容介绍Intro对象<br/>
	   */
	  public static List<String> readFileByLines(String path) {
			File file = new File(path);
			BufferedReader reader = null;
			List<String> lineList=new ArrayList<String>();
			try {
				reader = readUTF8(path);//new BufferedReader(new FileReader(file));
				String tempString = null;
				while ((tempString = reader.readLine()) != null) {
					// 每行的数据信息
					lineList.add(tempString);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (reader != null) {
					try {
						reader.close();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
			return lineList;
	}
	  
	public static boolean createFold(String path) {
		boolean bl=false;
		try{
			File file=new File(path);
			if(!file.exists()&&!file.isDirectory()){
				file.mkdir();
				bl=true;
				System.out.println("createFold successful");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return bl;
	}
	public static BufferedReader readUTF8(String file){
		 BufferedReader reader=null;
		try {
			reader = new BufferedReader(new InputStreamReader(
			    new UTF8BOMSkipper(new FileInputStream(file)), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return reader;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -