1. java如何判断数据类型
给你一个封装好的方法,只要把excel中的cell放入就会返回对应的值,里面有类型检测
public static String getExcelCellValue(HSSFCell cell) {
String ret = "";
// if (HSSFDateUtil.isCellDateFormatted(cell)) {
// Date date = cell.getDateCellValue();
// ret = "" + date.getTime();
// } else
try {
if (cell == null) {
ret = "";
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
ret = cell.getStringCellValue().trim();
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
ret = "" + cell.getNumericCellValue();
String temp = ret.substring(ret.indexOf(".") + 1, ret.length());
try {
if (Integer.parseInt(temp) == 0) {
ret = ret.substring(0, ret.indexOf("."));
}
} catch (Exception ex) {
}
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
ret = cell.getCellFormula();
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_ERROR) {
ret = "" + cell.getErrorCellValue();
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
ret = "" + cell.getBooleanCellValue();
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
ret = "";
}
} catch (Exception ex) {
ex.printStackTrace();
ret = "";
}
return ret;
}
2. java 判断文件类型
package com.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Ch2 {
private static final String RAGEX="[0x00-0x07]";
public static void main(String[] args) throws IOException {
File file=new File("c:/abc");
File[] files=file.listFiles();
for(int i=0;i<files.length;i++){
System.out.println(files[i].getName()+"\t"+getCheck(files[i]));
}
}
public static boolean getCheck(File f) throws IOException{
BufferedReader br=new BufferedReader(new FileReader(f));
String temp="";
while((temp=br.readLine())!=null){
for(int i=0;i<temp.length();i++){
if((temp.charAt(i)+"").matches(RAGEX)){
return true;
}
}
}
br.close();
return false;
}
}
//c:/abc是文件目录.里面可以随意放几个文件.但不要有文件夹.没做验证.
//根据楼上所说解答.分给楼上就行了,谢谢楼上!
2846

被折叠的 条评论
为什么被折叠?



