激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java編程中最基礎的文件和目錄操作方法詳解

Java編程中最基礎的文件和目錄操作方法詳解

2020-01-20 11:38小李飛刀8 JAVA教程

這篇文章主要介紹了Java編程中最基礎的文件和目錄操作方法詳解,是Java入門學習中的基礎知識,需要的朋友可以參考下

文件操作

平常經常使用JAVA對文件進行讀寫等操作,這里匯總一下常用的文件操作。

1、創建文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public static boolean createFile(String filePath){
  boolean result = false;
  File file = new File(filePath);
  if(!file.exists()){
    try {
      result = file.createNewFile();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
   
  return result;
}

2、創建文件夾

?
1
2
3
4
5
6
7
8
9
public static boolean createDirectory(String directory){
  boolean result = false;
  File file = new File(directory);
  if(!file.exists()){
    result = file.mkdirs();
  }
   
  return result;
}

3、刪除文件

?
1
2
3
4
5
6
7
8
9
public static boolean deleteFile(String filePath){
  boolean result = false;
  File file = new File(filePath);
  if(file.exists() && file.isFile()){
    result = file.delete();
  }
   
  return result;
}

4、刪除文件夾

遞歸刪除文件夾下面的子文件和文件夾

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static void deleteDirectory(String filePath){
  File file = new File(filePath);
  if(!file.exists()){
    return;
  }
   
  if(file.isFile()){
    file.delete();
  }else if(file.isDirectory()){
    File[] files = file.listFiles();
    for (File myfile : files) {
      deleteDirectory(filePath + "/" + myfile.getName());
    }
     
    file.delete();
  }
}

5、讀文件

(1)以字節為單位讀取文件,常用于讀二進制文件,如圖片、聲音、影像等文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public static String readFileByBytes(String filePath){
  File file = new File(filePath);
  if(!file.exists() || !file.isFile()){
    return null;
  }
   
  StringBuffer content = new StringBuffer();
   
  try {
    byte[] temp = new byte[1024];
    FileInputStream fileInputStream = new FileInputStream(file);
    while(fileInputStream.read(temp) != -1){
      content.append(new String(temp));
      temp = new byte[1024];
    }
     
    fileInputStream.close();
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }
   
  return content.toString();
}

 (2)以字符為單位讀取文件,常用于讀文本,數字等類型的文件,支持讀取中文

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public static String readFileByChars(String filePath){
  File file = new File(filePath);
  if(!file.exists() || !file.isFile()){
    return null;
  }
   
  StringBuffer content = new StringBuffer();
  try {
    char[] temp = new char[1024];
    FileInputStream fileInputStream = new FileInputStream(file);
    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK");
    while(inputStreamReader.read(temp) != -1){
      content.append(new String(temp));
      temp = new char[1024];
    }
     
    fileInputStream.close();
    inputStreamReader.close();
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }
   
  return content.toString();
}

(3)以行為單位讀取文件,常用于讀面向行的格式化文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public static List<String> readFileByLines(String filePath){
  File file = new File(filePath);
  if(!file.exists() || !file.isFile()){
    return null;
  }
   
  List<String> content = new ArrayList<String>();
  try {
    FileInputStream fileInputStream = new FileInputStream(file);
    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK");
    BufferedReader reader = new BufferedReader(inputStreamReader);
    String lineContent = "";
    while ((lineContent = reader.readLine()) != null) {
      content.add(lineContent);
      System.out.println(lineContent);
    }
     
    fileInputStream.close();
    inputStreamReader.close();
    reader.close();
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }
   
  return content;
}

6、寫文件

字符串寫入文件的幾個類中,FileWriter效率最高,BufferedOutputStream次之,FileOutputStream最差。

(1)通過FileOutputStream寫入文件

?
1
2
3
4
5
6
7
8
public static void writeFileByFileOutputStream(String filePath, String content) throws IOException{
  File file = new File(filePath);
  synchronized (file) {
    FileOutputStream fos = new FileOutputStream(filePath);
    fos.write(content.getBytes("GBK"));
    fos.close();
  }
}

(2)通過BufferedOutputStream寫入文件

?
1
2
3
4
5
6
7
8
9
public static void writeFileByBufferedOutputStream(String filePath, String content) throws IOException{
  File file = new File(filePath);
  synchronized (file) {
    BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(filePath));
    fos.write(content.getBytes("GBK"));
    fos.flush();
    fos.close();
  }
}

(3)通過FileWriter將字符串寫入文件

?
1
2
3
4
5
6
7
8
public static void writeFileByFileWriter(String filePath, String content) throws IOException{
    File file = new File(filePath);
    synchronized (file) {
      FileWriter fw = new FileWriter(filePath);
      fw.write(content);
      fw.close();
    }
  }

 

目錄操作
目錄是一個文件可以包含其他文件和目錄的列表。你想要在目錄中列出可用文件列表,可以通過使用 File 對象創建目錄,獲得完整詳細的能在 File 對象中調用的以及有關目錄的方法列表。

創建目錄
這里有兩個有用的文件方法,能夠創建目錄:

mkdir( ) 方法創建了一個目錄,成功返回 true ,創建失敗返回 false。失敗情況是指文件對象的路徑已經存在了,或者無法創建目錄,因為整個路徑不存在。
mkdirs( ) 方法創建一個目錄和它的上級目錄。
以下示例創建 “/ tmp / user / java / bin” 目錄:

?
1
2
3
4
5
6
7
8
9
10
import java.io.File;
 
public class CreateDir {
  public static void main(String args[]) {
   String dirname = "/tmp/user/java/bin";
   File d = new File(dirname);
   // Create directory now.
   d.mkdirs();
 }
}

編譯并執行以上代碼創建 “/ tmp /user/ java / bin”。

提示:Java 自動按 UNIX 和 Windows 約定來處理路徑分隔符。如果在 Windows 版本的 Java 中使用正斜杠(/),仍然可以得到正確的路徑。

目錄列表
如下,你能夠用 File 對象提供的 list() 方法來列出目錄中所有可用的文件和目錄

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.io.File;
 
public class ReadDir {
  public static void main(String[] args) {
 
   File file = null;
   String[] paths;
 
   try{  
     // create new file object
     file = new File("/tmp");
 
     // array of files and directory
     paths = file.list();
 
     // for each name in the path array
     for(String path:paths)
     {
      // prints filename and directory name
      System.out.println(path);
     }
   }catch(Exception e){
     // if any error occurs
     e.printStackTrace();
   }
  }
}

基于你/ tmp目錄下可用的目錄和文件,將產生以下結果:

?
1
2
3
4
test1.txt
test2.txt
ReadDir.java
ReadDir.class

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91丝袜 | 法国性xxx精品hd | 国产亚洲精品视频中文字幕 | 欧美一级网 | 国产精品一区二区三区在线播放 | 久久国产精品99久久人人澡 | 久久精品视频在线免费观看 | 播色网电影网 | 亚洲天堂午夜 | 91网址在线播放 | 日韩激情一区 | 精品国产乱码一区二区 | 精品一区二区三区免费爱 | 免费国产成人高清在线看软件 | 久久精品视频免费观看 | 97久久精品一区二区三区观看 | 一级免费 | 一级一级一级毛片 | av色偷偷| 国产一级毛片不卡 | 日本一区免费看 | 日本欧美一区 | 在线a | 午夜精品福利视频 | 日本网站一区 | 精品国产一区二区三区四区阿崩 | 久久免费精品 | 久久国产精品久久久久久久久久 | 中文字幕一区2区 | 国产二区三区在线播放 | 精精国产xxxx视频在线野外 | 日本一区视频在线观看 | 色中色综合 | 九九精品影院 | 最近免费观看高清韩国日本大全 | 羞羞电影网 | 久久精品国产99久久6动漫亮点 | 嗯哈~不行好大h双性 | 国产成人在线播放视频 | 久久最新免费视频 | 久久久久亚洲国产精品 |