文章目录
split导致的索引越界问题
String s = "1,2,";
String[] arr = s.split(",");
我以为上述代码:arr = ["1","2"," "]
,而实际为:arr = ["1","2"]
。在遍历arr时,出现索引越界报错
造成原因:s.split(",")
等同于s.split(",", 0)
,会丢弃字符串末尾的空值,但不会丢弃字符串中间的空值,split()
的第2个参数大于0代表分割字符串后数组的最大长度,小于0代表获取数组所有制,不丢弃末尾的空值
解决方案:String[] arr = s.split(",", -1);
通过Java代码设置生成的文件夹、文件的权限
- 通过PosixFilePermission进行操作
public static void chmod755(File file ) throws IllegalStateException {
//设置权限
Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
perms.add(PosixFilePermission.OWNER_READ); //设置所有者的读取权限
perms.add(PosixFilePermission.OWNER_WRITE); //设置所有者的写权限
perms.add(PosixFilePermission.OWNER_EXECUTE); //设置所有者的执行权限
perms.add(PosixFilePermission.GROUP_READ); //设置组的读取权限
perms.add(PosixFilePermission.GROUP_EXECUTE); //设置组的读取权限
perms.add(PosixFilePermission.OTHERS_READ); //设置其他的读取权限
perms.add(PosixFilePermission.OTHERS_EXECUTE); //设置其他的读取权限
try {
//设置文件和文件夹的权限
Path pathParent = Paths.get(file.getParentFile().getAbsolutePath());
Path pathDest = Paths.get(file.getAbsolutePath());
Files.setPosixFilePermissions(pathParent, perms); //修改文件夹路径的权限
Files.setPosixFilePermissions(pathDest, perms); //修改图片文件的权限
} catch (Exception e) {
e.printStackTrace();
}
}
或者直接:Files.setPosixFilePermissions(文件路径, "rwxrwxrwx");
2. 直接使用File操作
File file = new File(path)
file.setExecutable(true);
file.setReadable(true);
file.setWritable(true);
Oracle数据库中date类型数据比大小、timestamp类型数据比大小时报错
解决方案:分别使用to_Date(‘中文格式’,‘英文格式’),to_timeStamp(‘中文格式’,‘英文格式’)
CST时间转换
使用new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(data)
对时间格式进行转换时报错(忘了报的啥错了),一看data是Tue May 31 00:00:00 CST 2022
,
解决方案:转换时指定new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US)
public static String checkDate(String dateStr){
if(StringUtils.isBlank(dateStr)){
return "";
}
if(!dateStr.contains("CST")){
return "";
}
SimpleDateFormat sdf2 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
Date data = null;
try {
data = sdf2.parse(dateStr);
String formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(data);
System.out.println(formatDate);
return formatDate;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
为什么public方法不能被调用,加了static之后才能被调用?
因为方法在类中,new一下类创建类的实例,方法才会激活,才能被调用,而static修饰的方法是静态的,会先运行,不用生成实例对象方法就提前激活了,可以直接调用
DecimalFormat.format()导致报错:Cannot format given Object as a Number
问题:使用DecimalFormat将BigDecimal类型的数据转为保留两位小数的格式时,传入了字符串,结果报错java.lang.IllegalArgumentException: Cannot format given Object as a Number
。为什么在new DecimalFormat实例时可以使用字符串类型,但是具体使用时却不行?查看源码发现DecimalFormat的format中有很多包装类,但没有String类和自定义类型,所以,它没办法解释格式化String类型的数据。
DecimalFormat继承了抽象类NumberFormat,NumberFormat继承了抽象类Format,里面各有format方法,我们来看看Format类中的format方法,源码如下:
JAVA通过文件路径分隔符分割文件路径
问题:一般为了避免windows系统和linux系统之间路径分隔符的差异,我们会使用File.separator
来代替windows下的“\”或者Linux下的“/”,来表示文件路径。如果需要分割路径来获取文件(夹)名,会用到String.split(String regex)
方法,直接使用File.separator
作为参数会抛异常:java.util.regex.PatternSyntaxException: Unexpected internal error near index 1 ...
原因:这是因为在Windows下,File.separator
是“\”,经过转义识别为“\”,而String.split
方法的参数应该是一个正则表达式字符串,传入“\”就会抛这个异常。
解决方案:使用正则表达式/|\\\\
直接匹配windows系统和linux系统的路径分隔符
String separator = "/|\\\\";
String path = "E:\\test\\file1\\file2";
for (String str : Arrays.asList(path.split(separator))) {
System.out.println(str);
}