java代码删除功能 java代码删除文件

java 删除文件中的数据1、如果只是想要文件中的内容,可以使用如下代码:
FileOutputStream fs = new FileOutputStream(new File("C:\\buyterms.txt"));
2、如果是想要文件夹中的内容,可以使用如下代码:
package com.xx;
import java.io.File;
public class Test {
public static void main(String[] args) {
String fileRoot = "C:/Users/xx/Desktop/xx/xxx";
delFolder(fileRoot);
System.out.println("deleted");
}
//// 删除完文件后删除文件夹
//// 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) {
【java代码删除功能 java代码删除文件】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; itempList.length; i) {
if (path.endsWith(File.separator)) {
temp = new File(pathtempList[i]);
} else {
temp = new File(pathFile.separatortempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path"/"tempList[i]);// 先删除文件夹里面的文件
//delFolder(path"/"tempList[i]);// 再删除空文件夹
flag = true;
}
}
return flag;
}
}
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代码删除功能的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java代码删除文件、java代码删除功能的信息别忘了在本站进行查找喔 。

    推荐阅读