程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Jakarta-Common-IO使用筆記

Jakarta-Common-IO使用筆記

編輯:關於JAVA

代碼如下:

A.文件內容拷貝

package demo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

public class FileCopyExample ...{

  public static void main(String[] args) ...{
    try ...{
      File src = new File("test.dat");
      File dest = new File("test.dat.bak");

      FileUtils.copyFile(src, dest);
    } catch (IOException ioe) ...{
      System.out.println("Problem copying file.");
    }

    try ...{
      File src = new File("test.dat");
      File dir = new File("./temp");

      FileUtils.copyFileToDirectory(src, dir);
    } catch (IOException ioe) ...{
      System.out.println("Problem copying file to dir.");
    }

    try ...{
      String string = "Blah blah blah";
      File dest = new File("test.tmp");

      FileUtils.writeStringToFile(dest, string, "ISO-8859-1");
    } catch (IOException ioe) ...{
      System.out.println("Error writing out a String.");
    }

    try ...{
      Writer writer = new FileWriter("test.dat");
      InputStream inputStream = FileCopyExample.class.getClass()
          .getResourceAsStream("/demo/web.xml");
      IOUtils.copy(inputStream, writer);
      writer.close();
      inputStream.close();
    } catch (IOException e) ...{
      System.out.println("Error copying data");
    }

    try ...{
      File src = new File("test.txt");
      OutputStream output = new FileOutputStream(src);
      InputStream inputStream = FileCopyExample.class.getClass()
          .getResourceAsStream("/demo/web.xml");
      IOUtils.copy(inputStream, output);
      // writer.close();
      inputStream.close();
      output.close();
    } catch (IOException e) ...{
      System.out.println("Error copying data");
    }
  }
}

B.文件刪除

package demo;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class FileDeleteExample ...{

  public static void main(String[] args) ...{

    File file = new File(FileDeleteExample.class.getClass().getResource("/demo/web.xml").getFile());
    String display = FileUtils.byteCountToDisplaySize(file.length());
    System.out.println("web.xml is " + display);
    try ...{
      FileUtils.forceDelete(file);
    } catch (IOException e) ...{
      e.printStackTrace();
    }
  }
}

C.讀取取文本中的每一行:

package demo;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;

public class FileReadExample ...{

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

    File file = new File((FileReadExample.class.getClass().getResource("/demo/web.xml")).getFile());
    List<?> lines = FileUtils.readLines(file, "UTF-8");
    for(int i=0;i<lines.size();i++)
    System.out.println(lines.get(i));
  }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved