String fileName="insert.sql"
String filePath="C:\Temp\insert.sql"
请教怎么物理删除磁盘C:\Temp\insert.sql的文件!
还有怎么读取文件的内容啊!
大哥们帮帮忙啊!

解决方案 »

  1.   

    package mytestfiles; 
    import java.io.BufferedReader; 
    import java.io.File; 
    import java.io.FileReader; 
    import java.io.FileWriter; 
    import java.io.IOException; 
    import java.io.PrintWriter;  
     @author zhangqinglin 
     To change the template for this generated type comment go to 
     WindowPreferencesJavaCode GenerationCode and Comments 
     
    public class Files 

    public static void main(String[] args) throws IOException 

    Files f = new Files(); 
     System.out.println(f.readFile(fLinkFile.java)); 
     f.readAllFile(f,LinkFile.java); 
     f.readLineFile(f,LinkFile.java); 
     System.out.println(f.fileIsNull(f,122.txt)); 
     f.readFolderByFile(FPDF); 
     System.out.println(f.createAndDeleteFolder(ss,f)); 
     System.out.println(f.createAndDeleteFile(fss,TestFile.dat)); 
    String[] ss = new String[50]; 
    for(int i=0;iss.length;i++) 

    ss[i] = 信息技术和互联网(计算机软硬件,通讯) +i; 

    f.writeFile(fss,TestFile.txt,ss); 

     
     文件的写入 
     @param filePath(文件路径) 
     @param fileName(文件名) 
     @param args[] 
     @throws IOException 
     
    public void writeFile(String filePath,String fileName,String[] args) throws IOException 

    FileWriter fw = new FileWriter(filePath+fileName); 
    PrintWriter out=new PrintWriter(fw); 
    for(int i=0;iargs.length;i++) 

    out.write(args[i]); 
    out.println(); 
    out.flush(); 

    fw.close(); 
    out.close(); 

     
     文件的写入 
     @param filePath(文件路径) 
     @param fileName(文件名) 
     @param args 
     @throws IOException 
     
    public void writeFile(String filePath,String fileName,String args) throws IOException 

    FileWriter fw = new FileWriter(filePath+fileName); 
    fw.write(args); 
    fw.close(); 

     
     创建与删除文件 
     @param filePath 
     @param fileName 
     @return 创建成功返回true 
     @throws IOException 
     
    public boolean createAndDeleteFile(String filePath,String fileName) throws IOException 

    boolean result = false; 
    File file = new File(filePath,fileName); 
    if(file.exists()) 

    file.delete(); 
    result = true; 
    System.out.println(文件已经删除!); 

    else 

    file.createNewFile(); 
    result = true; 
    System.out.println(文件已经创建!); 

    return result; 

     
     创建和删除目录 
     @param folderName 
     @param filePath 
     @return 删除成功返回true 
     
    public boolean createAndDeleteFolder(String folderName,String filePath) 

    boolean result = false; 
    try 

    File file = new File(filePath+folderName); 
    if(file.exists()) 

    file.delete(); 
    System.out.println(目录已经存在,已删除!); 
    result = true; 

    else 

    file.mkdir(); 
    System.out.println(目录不存在,已经建立!); 
    result = true; 


    catch(Exception ex) 

    result = false; 
    System.out.println(CreateAndDeleteFolder is error+ex); 

    return result; 

     
     输出目录中的所有文件及目录名字 
     @param filePath 
     
    public void readFolderByFile(String filePath) 

    File file = new File(filePath); 
    File[] tempFile = file.listFiles(); 
    for(int i = 0;itempFile.length;i++) 

    if(tempFile[i].isFile()) 

    System.out.println(File  +tempFile[i].getName()); 

    if(tempFile[i].isDirectory()) 

    System.out.println(Directory  +tempFile[i].getName()); 



     
     检查文件中是否为一个空 
     @param filePath 
     @param fileName 
     @return 为空返回true 
     @throws IOException 
     
    public boolean fileIsNull(String filePath,String fileName) throws IOException 

    boolean result = false; 
    FileReader fr = new FileReader(filePath+fileName); 
    if(fr.read() == -1) 

    result = true; 
    System.out.println(fileName+ 文件中没有数据!); 

    else 

    System.out.println(fileName+ 文件中有数据!); 

    fr.close(); 
    return result; 

     
     读取文件中的所有内容 
     @param filePath 
     @param fileName 
     @throws IOException 
     
    public void readAllFile(String filePath,String fileName) throws IOException 

    FileReader fr = new FileReader(filePath+fileName); 
    int count = fr.read(); 
    while(count != -1) 

    System.out.print((char)count); 
    count = fr.read(); 
    if(count == 13) 

    fr.skip(1); 


    fr.close(); 

     
     一行一行的读取文件中的数据 
     @param filePath 
     @param fileName 
     @throws IOException 
     
    public void readLineFile(String filePath,String fileName) throws IOException 

    FileReader fr = new FileReader(filePath+fileName); 
    BufferedReader br = new BufferedReader(fr); 
    String line = br.readLine(); 
    while(line != null) 

    System.out.println(line); 
    line = br.readLine(); 

    br.close(); 
    fr.close(); 

    }
      

  2.   

    文件的建立/检查与删除 
    <%@ page contentType="text/html;charset=gb2312"%> 
    <%@ page import="java.io.*"%> 
    <html> 
    <head> 
    <title>文件的建立、检查与删除</title> 
    </head> 
    <body> 
    <% 
    String path=request.getRealPath(""); 
    //out.println(path); 
    File f=new File(path,"File.txt"); 
    //out.println(f); 
    //out.println(f.exists()); if(f.exists()){//检查File.txt是否存在 
    f.delete();//删除File.txt文件 
    out.println(path + "\\File.txt 存在,已删除。"); 
    }else{ 
    f.createNewFile();//在当前目录下建立一个名为File.txt的文件 
    out.println(path + "\\File.txt 不存在,已建立。");//输出目前所在的目录路径 

    %> 
      

  3.   

    /**
     * 读取文本文件的内容到一个向量中
     *
     *  @param strFileName是绝对路径文件
     *  @return Vector,每一个元素是文件的一行
     */
    public static Vector readTextFile(String strFileName)
    throws FileNotFoundException, IOException
    {
    Vector content = new Vector(); FileReader objFile = new FileReader(strFileName);
    BufferedReader objBuffer = new BufferedReader(objFile);
    String strLine = objBuffer.readLine();
    while ( strLine!=null ) {
    content.add(strLine);
    strLine = objBuffer.readLine();
    }

    objBuffer.close();
    objFile.close(); return content;
    }
    /**
     * 删除文件夹下所有内容,包括此文件夹
     */
    public static boolean delAll(File objFile) throws IOException { // 文件夹不存在不存在
    if (!objFile.exists()) {
    //log.error("指定目录不存在:" + objFile.getName());
    return false;
    } // 保存中间结果
    boolean rslt = true;
    if (!(rslt = objFile.delete())) { //先尝试直接删除
    //若文件夹非空。枚举、递归删除里面内容
    File subs[] = objFile.listFiles();
    for (int i = 0; i <= subs.length - 1; i++) {
    if (subs[i].isDirectory())
    delAll(subs[i]);
    //递归删除子文件夹内容
    rslt = subs[i].delete(); //删除子文件夹本身
    }
    rslt = objFile.delete(); //删除此文件夹本身
    } if (!rslt) {
    //log.error("无法删除:" + objFile.getName());
    return false;
    } return true;
    }
      

  4.   

    探究一下jdk文档吧
    java.io包
      

  5.   

    要删除文件呢,就要File类!
    如果要读取文件内容可以有很多类支持!
    FileInputStream FileReader RandomAccessFile等都行!
    还有BufferedReader能让你更方便的操作文件!
    我这里有按行模式的文件的工具(包括增\删\改\查等功能)!如果需要请给e-mail
    [email protected]
      

  6.   

    今天刚讲完J2EE的流,好多学生还是觉得比较难!不过还好都能做出来,
    推荐看下面这篇文章。http://www.zhihuigu.com/article/WebForm1.aspx?articleId=458欢迎光临http://www.zhihuigu.com讨论技术问题,免费Jsp,asp,aspx空间,电子书下载
      Java设计模式(中文版CHM)  
     
      Java编程思想(中文版CHM) 
     
       Java 2 图形设计卷Ⅱ:SWING(中文版CHM)  
     
       实战EJB(中文版pdf格式)  
     
       Hibernate原理与配置快速入门(中文版pdf) 
     
       hibernate2安装手记(中文版pdf)  
     
       JSP2.0技术手册(中文版pdf)  
     
       Hibernate开发指南(中文版pdf) 
     
       Hibernate中文参考文档(中文版pdf)  
     
       精通EJB第三版(英文版pdf)