java删除功能代码 java删除程序

java删除项目中的文件代码FIle file = new File("/image/123.jpg");
if (file.exists()){
file.delete();
}
使用File对象操作删除,会判断是否存在,如存在就删了 。
如果想找路径,使用File类的getAbsolutePath()方/法就能得到/绝/对/路/径/的字符串表示 。
例如上面的对、象file,使用
String str = file.getAbsolutePath();
System.out.println(str);
你在/控/制/台co/ns/ole/窗口就能看到了 。
Java中怎样实现批量删除操作?进行编写编程代码就能实现批量删除操作 。
具体代码如下:
[java]SPAN style="WHITE-SPACE: pre" /SPANpublic Connection con=null;
public PreparedStatement pstmt=null;
/**
* 得到连接对象
*/
public void getConnection(){
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/zufang?
user=rootpassword=rootuseUnicode=truecharacterEncoding=GB2312";
try {
Class.forName(driver);
con=DriverManager.getConnection(url,"root","root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public Connection con=null;
public PreparedStatement pstmt=null;
/**
* 得到连接对象
*/
public void getConnection(){
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/zufang?
user=rootpassword=rootuseUnicode=truecharacterEncoding=GB2312";
try {
Class.forName(driver);
con=DriverManager.getConnection(url,"root","root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
[java]SPAN style="WHITE-SPACE: pre" /SPAN/**
* 批量删除信息表中的信息
* @param sql
* @param param
* @return
*/
public boolean updateBatchDel(String sql,String[] param){
boolean flag = false;
getConnection();
try {
con.setAutoCommit(false);
pstmt = con.prepareStatement(sql);
for(int i =0 ;iparam.length;i++){
pstmt.setString(1,param[i].trim());
pstmt.addBatch();
}
pstmt.executeBatch(); //批量执行
con.commit();//提交事务
flag = true;
} catch (SQLException e) {
try {
con.rollback(); //进行事务回滚
} catch (SQLException ex) {
ex.printStackTrace();
}
}finally {
closeAll(null,pstmt,con);
}
return flag;
}
/**
* 批量删除信息表中的信息
* @param sql
* @param param
* @return
*/
public boolean updateBatchDel(String sql,String[] param){
boolean flag = false;
getConnection();
try {
con.setAutoCommit(false);
pstmt = con.prepareStatement(sql);
for(int i =0 ;iparam.length;i++){
pstmt.setString(1,param[i].trim());
pstmt.addBatch();
}
pstmt.executeBatch(); //批量执行
con.commit();//提交事务
flag = true;
} catch (SQLException e) {
try {
con.rollback(); //进行事务回滚
} catch (SQLException ex) {
ex.printStackTrace();
}
}finally {
closeAll(null,pstmt,con);
}
return flag;
上面是进行批量删除的编程码 。
删除JAVA集合中元素的实现代码 经常我们要删除集合中的某些元素 有些可能会这么写
复制代码代码如下: public void operate(List list){ for (Iterator it = list iterator(); it hasNext();) { String str = (String)it next(); if (str equals("chengang")){ list remove(str); } } }
这种写法一运行就会报如下异常
Exception in thread "main" java util ConcurrentModificationException at java util AbstractList$Itr checkForComodification(AbstractList java: )
因为list在循环中的时候是不可以删除它的元素的 后来我是这样做的 一种很笨的方法 思路是这样的 创建一个List专门存放要被删除的元素 循环过后 用List removeAll方法把元素删除 代码如下

推荐阅读