首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java 文件操作大全:從輸入讀取到目錄遞歸刪除

     編輯:Java 相關知識

一、獲取控制台用戶輸入

public String getInputMessage() throws IOException {
    System.out.println("請輸入您的命令:");
    byte[] buffer = new byte[1024];
    int count = System.in.read(buffer);
    char[] ch = new char[count - 2]; // 去除結束符
    for (int i = 0; i < count - 2; i++) ch[i] = (char) buffer[i];
    return new String(ch);
}

不支持中文輸入,需改進。

二、文件複製

1. 文件流方式(支持中文、多格式)

public void copyFile(String src, String dest) throws IOException {
    FileInputStream in = new FileInputStream(src);
    File file = new File(dest);
    if (!file.exists()) file.createNewFile();
    FileOutputStream out = new FileOutputStream(file);
    int c; byte[] buffer = new byte[1024];
    while ((c = in.read(buffer)) != -1) {
        for (int i = 0; i < c; i++) out.write(buffer[i]);
    }
    in.close(); out.close();
}

三、寫文件

1. PrintStream 寫文本

public void PrintStreamDemo() {
    try {
        FileOutputStream out = new FileOutputStream("D:/test.txt");
        PrintStream p = new PrintStream(out);
        for (int i = 0; i < 10; i++) p.println("This is " + i + " line");
    } catch (FileNotFoundException e) { e.printStackTrace(); }
}

2. StringBuffer 指定編碼寫入(解決中文問題)

public void StringBufferDemo() throws IOException {
    File file = new File("/root/sms.log");
    if (!file.exists()) file.createNewFile();
    FileOutputStream out = new FileOutputStream(file, true); // 追加模式
    for (int i = 0; i < 10000; i++) {
        StringBuffer sb = new StringBuffer();
        sb.append("這是第" + i + "行:...");
        out.write(sb.toString().getBytes("utf-8"));
    }
    out.close();
}

四、文件重命名

public void renameFile(String path, String oldname, String newname) {
    if (!oldname.equals(newname)) {
        File oldfile = new File(path + "/" + oldname);
        File newfile = new File(path + "/" + newname);
        if (newfile.exists()) System.out.println(newname + "已經存在!");
        else oldfile.renameTo(newfile);
    }
}

五、轉移文件目錄

public void changeDirectory(String filename, String oldpath, String newpath, boolean cover) {
    if (!oldpath.equals(newpath)) {
        File oldfile = new File(oldpath + "/" + filename);
        File newfile = new File(newpath + "/" + filename);
        if (newfile.exists()) {
            if (cover) oldfile.renameTo(newfile);
            else System.out.println("在新目錄下已經存在:" + filename);
        } else oldfile.renameTo(newfile);
    }
}

六、讀文件

1. FileInputStream 讀取

public String FileInputStreamDemo(String path) throws IOException {
    File file = new File(path);
    if (!file.exists() || file.isDirectory()) throw new FileNotFoundException();
    FileInputStream fis = new FileInputStream(file);
    byte[] buf = new byte[1024];
    StringBuffer sb = new StringBuffer();
    while ((fis.read(buf)) != -1) {
        sb.append(new String(buf));
        buf = new byte[1024];
    }
    return sb.toString();
}

2. BufferedReader 高效讀取

public String BufferedReaderDemo(String path) throws IOException {
    File file = new File(path);
    if (!file.exists() || file.isDirectory()) throw new FileNotFoundException();
    BufferedReader br = new BufferedReader(new FileReader(file));
    String temp; StringBuffer sb = new StringBuffer();
    while ((temp = br.readLine()) != null) sb.append(temp + " ");
    return sb.toString();
}

3. dom4j 讀取 XML

public Document readXml(String path) throws DocumentException, IOException {
    File file = new File(path);
    BufferedReader br = new BufferedReader(new FileReader(file));
    SAXReader sax = new SAXReader();
    Document doc = (Document) sax.read(br);
    br.close();
    return doc;
}

七、創建文件/目錄

public void createDir(String path) { new File(path).mkdir(); }
public void createFile(String path, String filename) throws IOException {
    File file = new File(path + "/" + filename);
    if (!file.exists()) file.createNewFile();
}

八、刪除文件/目錄

1. 刪除文件

public void delFile(String path, String filename) {
    File file = new File(path + "/" + filename);
    if (file.exists() && file.isFile()) file.delete();
}

2. 遞歸刪除目錄

public void delDir(String path) {
    File dir = new File(path);
    if (dir.exists()) {
        for (File tmp : dir.listFiles()) {
            if (tmp.isDirectory()) delDir(path + "/" + tmp.getName());
            else tmp.delete();
        }
        dir.delete();
    }
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved