1.创建文件

  1. import java.io.File;
  2. import java.io.IOException;
  3. public class CreateFileExample
  4. {
  5. public static void main( String[] args )
  6. {
  7. try {
  8. File file = new File("c:\\newfile.txt");
  9. //创建文件使用createNewFile()方法
  10. if (file.createNewFile()){
  11. System.out.println("File is created!");
  12. }else{
  13. System.out.println("File already exists.");
  14. }
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

2.建立文件路径

  1. import java.io.File;
  2. import java.io.IOException;
  3. public class FilePathExample1 {
  4. public static void main(String[] args) {
  5. try {
  6. String filename = "newFile.txt";
  7. String workingDirectory = System.getProperty("user.dir");
  8. //****************//
  9. String absoluteFilePath = "";
  10. //absoluteFilePath = workingDirectory + System.getProperty("file.separator") + filename;
  11. absoluteFilePath = workingDirectory + File.separator + filename;
  12. System.out.println("Final filepath : " + absoluteFilePath);
  13. //****************//
  14. File file = new File(absoluteFilePath);
  15. if (file.createNewFile()) {
  16. System.out.println("File is created!");
  17. } else {
  18. System.out.println("File is already existed!");
  19. }
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. }
  25. /////////////////////////////////////////////////////////////////
  26. import java.io.File;
  27. import java.io.IOException;
  28. public class FilePathExample2 {
  29. public static void main(String[] args) {
  30. try {
  31. String filename = "newFile.txt";
  32. String workingDirectory = System.getProperty("user.dir");
  33. //****************//
  34. File file = new File(workingDirectory, filename);
  35. //****************//
  36. System.out.println("Final filepath : " + file.getAbsolutePath());
  37. if (file.createNewFile()) {
  38. System.out.println("File is created!");
  39. } else {
  40. System.out.println("File is already existed!");
  41. }
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

3.设置文件权限

@检查文件权限是否设置

  • file.canExecute()
  • file.canWrite()
  • file.canRead()

@设置权限 
- file.setExecutable(boolean) 
- file.setReadable(boolean) 
- file.setWritable(boolean)

代码演示 :

  1. package com.File;
  2. import java.io.File;
  3. import java.io.IOException;
  4. public class FilePermissionExample
  5. {
  6. public static void main( String[] args )
  7. {
  8. try {
  9. //1.未设置权限
  10. File file = new File("D:\\2.txt");
  11. // if(file.exists()){
  12. // System.out.println("是否可执行: " + file.canExecute());
  13. // System.out.println("是否可写 : " + file.canWrite());
  14. // System.out.println("是否可读 : " + file.canRead());
  15. // }
  16. //2.已设置权限
  17. file.setExecutable(false);
  18. file.setReadable(false);
  19. file.setWritable(false);
  20. System.out.println(" 是否可执行: " + file.canExecute());
  21. System.out.println("是否可写 : " + file.canWrite());
  22. System.out.println("是否可读 : " + file.canRead());
  23. if (file.createNewFile()){
  24. System.out.println("文件已创建!");
  25. }else{
  26. System.out.println("文件已存在");
  27. }
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. }
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

4.读取文本内容的重要几个流

  • BufferedInputStream
  • FileInputStream
  • FileReader
  • BufferedReader
  • FileInputStream
  • DataInputStream

@.BufferedInputStream、 DataInputStream的使用

  1. import java.io.BufferedInputStream;
  2. import java.io.DataInputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. public class BufferedInputStreamExample {
  7. public static void main(String[] args) {
  8. File file = new File("C:\\testing.txt");
  9. FileInputStream fis = null;
  10. BufferedInputStream bis = null;
  11. DataInputStream dis = null;
  12. try {
  13. fis = new FileInputStream(file);
  14. bis = new BufferedInputStream(fis);
  15. dis = new DataInputStream(bis);
  16. while (dis.available() != 0) {
  17. System.out.println(dis.readLine());
  18. }
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. } finally {
  22. try {
  23. fis.close();
  24. bis.close();
  25. dis.close();
  26. } catch (IOException ex) {
  27. ex.printStackTrace();
  28. }
  29. }
  30. }
  31. }
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

@ FileInputStream的使用

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. public class ReadFileExample {
  5. public static void main(String[] args) {
  6. File file = new File("C:/robots.txt");
  7. FileInputStream fis = null;
  8. try {
  9. fis = new FileInputStream(file);
  10. System.out.println("Total file size to read (in bytes) : "
  11. + fis.available());
  12. int content;
  13. while ((content = fis.read()) != -1) {
  14. // convert to char and display it
  15. System.out.print((char) content);
  16. }
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. } finally {
  20. try {
  21. if (fis != null)
  22. fis.close();
  23. } catch (IOException ex) {
  24. ex.printStackTrace();
  25. }
  26. }
  27. }
  28. }
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

@BufferedReader的使用

  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. public class ReadFileExample1 {
  5. private static final String FILENAME = "E:\\test\\filename.txt";
  6. public static void main(String[] args) {
  7. BufferedReader br = null;
  8. FileReader fr = null;
  9. try {
  10. //br = new BufferedReader(new FileReader(FILENAME));
  11. fr = new FileReader(FILENAME);
  12. br = new BufferedReader(fr);
  13. String sCurrentLine;
  14. while ((sCurrentLine = br.readLine()) != null) {
  15. System.out.println(sCurrentLine);
  16. }
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. } finally {
  20. try {
  21. if (br != null)
  22. br.close();
  23. if (fr != null)
  24. fr.close();
  25. } catch (IOException ex) {
  26. ex.printStackTrace();
  27. }
  28. }
  29. }
  30. }
  31. #################################################################
  32. import java.io.BufferedReader;
  33. import java.io.FileReader;
  34. import java.io.IOException;
  35. public class ReadFileExample2 {
  36. private static final String FILENAME = "E:\\test\\filename.txt";
  37. public static void main(String[] args) {
  38. try (BufferedReader br = new BufferedReader(new FileReader(FILENAME))) {
  39. String sCurrentLine;
  40. while ((sCurrentLine = br.readLine()) != null) {
  41. System.out.println(sCurrentLine);
  42. }
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. }
  48. #################################################################
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77

5.写入文本的几个重要的流

  • BufferedOutputStream
  • FileOutputStream
  • FileWriter
  • BufferedWriter
  • FileOutputStream
  • DataOutputStream

@FileOutputStream 的使用

  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. public class WriteFileExample {
  5. public static void main(String[] args) {
  6. File file = new File("c:/newfile.txt");
  7. String content = "This is the text content";
  8. try (FileOutputStream fop = new FileOutputStream(file)) {
  9. // if file doesn't exists, then create it
  10. if (!file.exists()) {
  11. file.createNewFile();
  12. }
  13. // get the content in bytes
  14. byte[] contentInBytes = content.getBytes();
  15. fop.write(contentInBytes);
  16. fop.flush();
  17. fop.close();
  18. System.out.println("Done");
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. }
  22. }
  23. }
  • 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
  • 29
  • 30
  • 31

@BufferedWriter的使用


  1. import java.io.BufferedWriter;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. public class WriteToFileExample2 {
  5. private static final String FILENAME = "E:\\test\\filename.txt";
  6. public static void main(String[] args) {
  7. try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME))) {
  8. String content = "This is the content to write into file\n";
  9. bw.write(content);
  10. // no need to close it.
  11. //bw.close();
  12. System.out.println("Done");
  13. } catch (IOException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. }
  • 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
  • 29
  • 30
  • 31

6.向文件中添加新的内容

  1. import java.io.BufferedWriter;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. public class AppendToFileExample {
  6. private static final String FILENAME = "E:\\test\\filename.txt";
  7. public static void main(String[] args) {
  8. BufferedWriter bw = null;
  9. FileWriter fw = null;
  10. try {
  11. String data = " This is new content";
  12. File file = new File(FILENAME);
  13. // if file doesnt exists, then create it
  14. if (!file.exists()) {
  15. file.createNewFile();
  16. }
  17. // true = append file
  18. fw = new FileWriter(file.getAbsoluteFile(), true);
  19. bw = new BufferedWriter(fw);
  20. bw.write(data);
  21. System.out.println("Done");
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. } finally {
  25. try {
  26. if (bw != null)
  27. bw.close();
  28. if (fw != null)
  29. fw.close();
  30. } catch (IOException ex) {
  31. ex.printStackTrace();
  32. }
  33. }
  34. }
  35. }
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

7.删除文件

  1. mport java.io.File;
  2. public class DeleteFileExample
  3. {
  4. public static void main(String[] args)
  5. {
  6. try{
  7. File file = new File("c:\\logfile20100131.log");
  8. if(file.delete()){
  9. System.out.println(file.getName() + " is deleted!");
  10. }else{
  11. System.out.println("Delete operation is failed.");
  12. }
  13. }catch(Exception e){
  14. e.printStackTrace();
  15. }
  16. }
  17. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

8.删除指定格式的所有文件

  1. import java.io.*;
  2. public class FileChecker {
  3. private static final String FILE_DIR = "c:\\folder";
  4. private static final String FILE_TEXT_EXT = ".txt";
  5. public static void main(String args[]) {
  6. new FileChecker().deleteFile(FILE_DIR,FILE_TEXT_EXT);
  7. }
  8. public void deleteFile(String folder, String ext){
  9. GenericExtFilter filter = new GenericExtFilter(ext);
  10. File dir = new File(folder);
  11. //list out all the file name with .txt extension
  12. String[] list = dir.list(filter);
  13. if (list.length == 0) return;
  14. File fileDelete;
  15. for (String file : list){
  16. String temp = new StringBuffer(FILE_DIR)
  17. .append(File.separator)
  18. .append(file).toString();
  19. fileDelete = new File(temp);
  20. boolean isdeleted = fileDelete.delete();
  21. System.out.println("file : " + temp + " is deleted : " + isdeleted);
  22. }
  23. }
  24. //inner class, generic extension filter
  25. public class GenericExtFilter implements FilenameFilter {
  26. private String ext;
  27. public GenericExtFilter(String ext) {
  28. this.ext = ext;
  29. }
  30. public boolean accept(File dir, String name) {
  31. return (name.endsWith(ext));
  32. }
  33. }
  34. }
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

9.查找以某种格式的所有文件

  1. import java.io.*;
  2. public class FindCertainExtension {
  3. private static final String FILE_DIR = "c:\\folder";
  4. private static final String FILE_TEXT_EXT = ".jpg";
  5. public static void main(String args[]) {
  6. new FindCertainExtension().listFile(FILE_DIR, FILE_TEXT_EXT);
  7. }
  8. public void listFile(String folder, String ext) {
  9. GenericExtFilter filter = new GenericExtFilter(ext);
  10. File dir = new File(folder);
  11. if(dir.isDirectory()==false){
  12. System.out.println("Directory does not exists : " + FILE_DIR);
  13. return;
  14. }
  15. // list out all the file name and filter by the extension
  16. String[] list = dir.list(filter);
  17. if (list.length == 0) {
  18. System.out.println("no files end with : " + ext);
  19. return;
  20. }
  21. for (String file : list) {
  22. String temp = new StringBuffer(FILE_DIR).append(File.separator)
  23. .append(file).toString();
  24. System.out.println("file : " + temp);
  25. }
  26. }
  27. // inner class, generic extension filter
  28. public class GenericExtFilter implements FilenameFilter {
  29. private String ext;
  30. public GenericExtFilter(String ext) {
  31. this.ext = ext;
  32. }
  33. public boolean accept(File dir, String name) {
  34. return (name.endsWith(ext));
  35. }
  36. }
  37. }
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

10.修改文件名

  1. import java.io.File;
  2. public class RenameFileExample
  3. {
  4. public static void main(String[] args)
  5. {
  6. File oldfile =new File("oldfile.txt");
  7. File newfile =new File("newfile.txt");
  8. if(oldfile.renameTo(newfile)){
  9. System.out.println("Rename succesful");
  10. }else{
  11. System.out.println("Rename failed");
  12. }
  13. }
  14. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

11.复制文件内容

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. public class CopyFileExample
  8. {
  9. public static void main(String[] args)
  10. {
  11. InputStream inStream = null;
  12. OutputStream outStream = null;
  13. try{
  14. File afile =new File("Afile.txt");
  15. File bfile =new File("Bfile.txt");
  16. inStream = new FileInputStream(afile);
  17. outStream = new FileOutputStream(bfile);
  18. byte[] buffer = new byte[1024];
  19. int length;
  20. //copy the file content in bytes
  21. while ((length = inStream.read(buffer)) > 0){
  22. outStream.write(buffer, 0, length);
  23. }
  24. inStream.close();
  25. outStream.close();
  26. System.out.println("File is copied successful!");
  27. }catch(IOException e){
  28. e.printStackTrace();
  29. }
  30. }
  31. }
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

12.将一个文件移到另一个目录

  1. import java.io.File;
  2. public class MoveFileExample
  3. {
  4. public static void main(String[] args)
  5. {
  6. try{
  7. File afile =new File("C:\\folderA\\Afile.txt");
  8. if(afile.renameTo(new File("C:\\folderB\\" + afile.getName()))){
  9. System.out.println("File is moved successful!");
  10. }else{
  11. System.out.println("File is failed to move!");
  12. }
  13. }catch(Exception e){
  14. e.printStackTrace();
  15. }
  16. }
  17. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

13.创建文件是添加日期

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.StringTokenizer;
  5. public class GetFileCreationDateExample
  6. {
  7. public static void main(String[] args)
  8. {
  9. try{
  10. Process proc =
  11. Runtime.getRuntime().exec("cmd /c dir c:\\logfile.log /tc");
  12. BufferedReader br =
  13. new BufferedReader(
  14. new InputStreamReader(proc.getInputStream()));
  15. String data ="";
  16. //it's quite stupid but work
  17. for(int i=0; i<6; i++){
  18. data = br.readLine();
  19. }
  20. System.out.println("Extracted value : " + data);
  21. //split by space
  22. StringTokenizer st = new StringTokenizer(data);
  23. String date = st.nextToken();//Get date
  24. String time = st.nextToken();//Get time
  25. System.out.println("Creation Date : " + date);
  26. System.out.println("Creation Time : " + time);
  27. }catch(IOException e){
  28. e.printStackTrace();
  29. }
  30. }
  31. }
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

14.获取文件被修改的日期

  1. import java.io.File;
  2. import java.text.SimpleDateFormat;
  3. public class GetFileLastModifiedExample
  4. {
  5. public static void main(String[] args)
  6. {
  7. File file = new File("c:\\logfile.log");
  8. System.out.println("Before Format : " + file.lastModified());
  9. SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
  10. System.out.println("After Format : " + sdf.format(file.lastModified()));
  11. }
  12. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

15.修改文件日期

  1. import java.io.File;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. public class ChangeFileLastModifiedExample
  6. {
  7. public static void main(String[] args)
  8. {
  9. try{
  10. File file = new File("C:\\logfile.log");
  11. //print the original last modified date
  12. SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
  13. System.out.println("Original Last Modified Date : "
  14. + sdf.format(file.lastModified()));
  15. //set this date
  16. String newLastModified = "01/31/1998";
  17. //need convert the above date to milliseconds in long value
  18. Date newDate = sdf.parse(newLastModified);
  19. file.setLastModified(newDate.getTime());
  20. //print the latest last modified date
  21. System.out.println("Lastest Last Modified Date : "
  22. + sdf.format(file.lastModified()));
  23. }catch(ParseException e){
  24. e.printStackTrace();
  25. }
  26. }
  27. }
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

16.设置文件只能读

  1. import java.io.File;
  2. import java.io.IOException;
  3. public class FileReadAttribute
  4. {
  5. public static void main(String[] args) throws IOException
  6. {
  7. File file = new File("c:/file.txt");
  8. //mark this file as read only, since jdk 1.2
  9. file.setReadOnly();
  10. if(file.canWrite()){
  11. System.out.println("This file is writable");
  12. }else{
  13. System.out.println("This file is read only");
  14. }
  15. //revert the operation, mark this file as writable, since jdk 1.6
  16. file.setWritable(true);
  17. if(file.canWrite()){
  18. System.out.println("This file is writable");
  19. }else{
  20. System.out.println("This file is read only");
  21. }
  22. }
  23. }
  • 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
  • 29

17.获取文件大小

  1. import java.io.File;
  2. public class FileSizeExample
  3. {
  4. public static void main(String[] args)
  5. {
  6. File file =new File("c:\\java_xml_logo.jpg");
  7. if(file.exists()){
  8. double bytes = file.length();
  9. double kilobytes = (bytes / 1024);
  10. double megabytes = (kilobytes / 1024);
  11. double gigabytes = (megabytes / 1024);
  12. double terabytes = (gigabytes / 1024);
  13. double petabytes = (terabytes / 1024);
  14. double exabytes = (petabytes / 1024);
  15. double zettabytes = (exabytes / 1024);
  16. double yottabytes = (zettabytes / 1024);
  17. System.out.println("bytes : " + bytes);
  18. System.out.println("kilobytes : " + kilobytes);
  19. System.out.println("megabytes : " + megabytes);
  20. System.out.println("gigabytes : " + gigabytes);
  21. System.out.println("terabytes : " + terabytes);
  22. System.out.println("petabytes : " + petabytes);
  23. System.out.println("exabytes : " + exabytes);
  24. System.out.println("zettabytes : " + zettabytes);
  25. System.out.println("yottabytes : " + yottabytes);
  26. }else{
  27. System.out.println("File does not exists!");
  28. }
  29. }
  30. }
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

18.获取文件路径

  1. import java.io.File;
  2. import java.io.IOException;
  3. public class AbsoluteFilePathExample
  4. {
  5. public static void main(String[] args)
  6. {
  7. try{
  8. File temp = File.createTempFile("i-am-a-temp-file", ".tmp" );
  9. String absolutePath = temp.getAbsolutePath();
  10. System.out.println("File path : " + absolutePath);
  11. String filePath = absolutePath.
  12. substring(0,absolutePath.lastIndexOf(File.separator));
  13. System.out.println("File path : " + filePath);
  14. }catch(IOException e){
  15. e.printStackTrace();
  16. }
  17. }
  18. }
  • 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

19.获取文件的总行数

  1. import java.io.File;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.io.LineNumberReader;
  5. public class LineNumberReaderExample
  6. {
  7. public static void main(String[] args)
  8. {
  9. try{
  10. File file =new File("c:\\ihave10lines.txt");
  11. if(file.exists()){
  12. FileReader fr = new FileReader(file);
  13. LineNumberReader lnr = new LineNumberReader(fr);
  14. int linenumber = 0;
  15. while (lnr.readLine() != null){
  16. linenumber++;
  17. }
  18. System.out.println("Total number of lines : " + linenumber);
  19. lnr.close();
  20. }else{
  21. System.out.println("File does not exists!");
  22. }
  23. }catch(IOException e){
  24. e.printStackTrace();
  25. }
  26. }
  27. }
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

20.检查文件是否存在


  1. import java.io.*;
  2. public class FileChecker {
  3. public static void main(String args[]) {
  4. File f = new File("c:\\mkyong.txt");
  5. if(f.exists()){
  6. System.out.println("File existed");
  7. }else{
  8. System.out.println("File not found!");
  9. }
  10. }
  11. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

21.检查 文件是否隐藏

  1. import java.io.File;
  2. import java.io.IOException;
  3. public class FileHidden
  4. {
  5. public static void main(String[] args) throws IOException
  6. {
  7. File file = new File("c:/hidden-file.txt");
  8. if(file.isHidden()){
  9. System.out.println("This file is hidden");
  10. }else{
  11. System.out.println("This file is not hidden");
  12. }
  13. }
  14. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

22.读取文件为UTF-8的数据

  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.UnsupportedEncodingException;
  7. public class test {
  8. public static void main(String[] args){
  9. try {
  10. File fileDir = new File("c:\\temp\\test.txt");
  11. BufferedReader in = new BufferedReader(
  12. new InputStreamReader(
  13. new FileInputStream(fileDir), "UTF8"));
  14. String str;
  15. while ((str = in.readLine()) != null) {
  16. System.out.println(str);
  17. }
  18. in.close();
  19. }
  20. catch (UnsupportedEncodingException e)
  21. {
  22. System.out.println(e.getMessage());
  23. }
  24. catch (IOException e)
  25. {
  26. System.out.println(e.getMessage());
  27. }
  28. catch (Exception e)
  29. {
  30. System.out.println(e.getMessage());
  31. }
  32. }
  33. }
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

23.向文件中写入UTF-8的数据

  1. import java.io.BufferedWriter;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.OutputStreamWriter;
  6. import java.io.UnsupportedEncodingException;
  7. import java.io.Writer;
  8. public class test {
  9. public static void main(String[] args){
  10. try {
  11. File fileDir = new File("c:\\temp\\test.txt");
  12. Writer out = new BufferedWriter(new OutputStreamWriter(
  13. new FileOutputStream(fileDir), "UTF8"));
  14. out.append("Website UTF-8").append("\r\n");
  15. out.append("?? UTF-8").append("\r\n");
  16. out.append("??????? UTF-8").append("\r\n");
  17. out.flush();
  18. out.close();
  19. }
  20. catch (UnsupportedEncodingException e)
  21. {
  22. System.out.println(e.getMessage());
  23. }
  24. catch (IOException e)
  25. {
  26. System.out.println(e.getMessage());
  27. }
  28. catch (Exception e)
  29. {
  30. System.out.println(e.getMessage());
  31. }
  32. }
  33. }
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

24.将 文本内容复制给一个变量

  1. import java.io.DataInputStream;
  2. import java.io.FileInputStream;
  3. public class App{
  4. public static void main (String args[]) {
  5. try{
  6. DataInputStream dis =
  7. new DataInputStream (
  8. new FileInputStream ("c:\\logging.log"));
  9. byte[] datainBytes = new byte[dis.available()];
  10. dis.readFully(datainBytes);
  11. dis.close();
  12. String content = new String(datainBytes, 0, datainBytes.length);
  13. System.out.println(content);
  14. }catch(Exception ex){
  15. ex.printStackTrace();
  16. }
  17. }
  18. }
  • 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

25.生成文件校验值

  1. import java.io.FileInputStream;
  2. import java.security.MessageDigest;
  3. public class TestCheckSum {
  4. public static void main(String args[]) throws Exception {
  5. String datafile = "c:\\INSTLOG.TXT";
  6. MessageDigest md = MessageDigest.getInstance("SHA1");
  7. FileInputStream fis = new FileInputStream(datafile);
  8. byte[] dataBytes = new byte[1024];
  9. int nread = 0;
  10. while ((nread = fis.read(dataBytes)) != -1) {
  11. md.update(dataBytes, 0, nread);
  12. };
  13. byte[] mdbytes = md.digest();
  14. //convert the byte to hex format
  15. StringBuffer sb = new StringBuffer("");
  16. for (int i = 0; i < mdbytes.length; i++) {
  17. sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
  18. }
  19. System.out.println("Digest(in hex format):: " + sb.toString());
  20. }
  21. }
  • 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
  • 29
  • 30
  • 31

26.将文件转换成字节数组

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.nio.file.Files;
  5. import java.nio.file.Path;
  6. import java.nio.file.Paths;
  7. public class FileToArrayOfBytes {
  8. public static void main(String[] args) {
  9. try {
  10. // convert file to byte[]
  11. byte[] bFile = readBytesFromFile("C:\\temp\\testing1.txt");
  12. //java nio
  13. //byte[] bFile = Files.readAllBytes(new File("C:\\temp\\testing1.txt").toPath());
  14. //byte[] bFile = Files.readAllBytes(Paths.get("C:\\temp\\testing1.txt"));
  15. // save byte[] into a file
  16. Path path = Paths.get("C:\temp\\test2.txt");
  17. Files.write(path, bFile);
  18. System.out.println("Done");
  19. //Print bytes[]
  20. for (int i = 0; i < bFile.length; i++) {
  21. System.out.print((char) bFile[i]);
  22. }
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. private static byte[] readBytesFromFile(String filePath) {
  28. FileInputStream fileInputStream = null;
  29. byte[] bytesArray = null;
  30. try {
  31. File file = new File(filePath);
  32. bytesArray = new byte[(int) file.length()];
  33. //read file into bytes[]
  34. fileInputStream = new FileInputStream(file);
  35. fileInputStream.read(bytesArray);
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. } finally {
  39. if (fileInputStream != null) {
  40. try {
  41. fileInputStream.close();
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  47. return bytesArray;
  48. }
  49. }
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

27.文件保存字节数组

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.nio.file.Files;
  6. import java.nio.file.Path;
  7. import java.nio.file.Paths;
  8. public class ArrayOfBytesToFile {
  9. private static final String UPLOAD_FOLDER = "C:\\temp\\";
  10. public static void main(String[] args) {
  11. FileInputStream fileInputStream = null;
  12. try {
  13. File file = new File("C:\\temp\\testing1.txt");
  14. byte[] bFile = new byte[(int) file.length()];
  15. //read file into bytes[]
  16. fileInputStream = new FileInputStream(file);
  17. fileInputStream.read(bFile);
  18. //save bytes[] into a file
  19. writeBytesToFile(bFile, UPLOAD_FOLDER + "test1.txt");
  20. writeBytesToFileClassic(bFile, UPLOAD_FOLDER + "test2.txt");
  21. writeBytesToFileNio(bFile, UPLOAD_FOLDER + "test3.txt");
  22. System.out.println("Done");
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. } finally {
  26. if (fileInputStream != null) {
  27. try {
  28. fileInputStream.close();
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. }
  34. }
  35. //Classic, < JDK7
  36. private static void writeBytesToFileClassic(byte[] bFile, String fileDest) {
  37. FileOutputStream fileOuputStream = null;
  38. try {
  39. fileOuputStream = new FileOutputStream(fileDest);
  40. fileOuputStream.write(bFile);
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. } finally {
  44. if (fileOuputStream != null) {
  45. try {
  46. fileOuputStream.close();
  47. } catch (IOException e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. }
  52. }
  53. //Since JDK 7 - try resources
  54. private static void writeBytesToFile(byte[] bFile, String fileDest) {
  55. try (FileOutputStream fileOuputStream = new FileOutputStream(fileDest)) {
  56. fileOuputStream.write(bFile);
  57. } catch (IOException e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. //Since JDK 7, NIO
  62. private static void writeBytesToFileNio(byte[] bFile, String fileDest) {
  63. try {
  64. Path path = Paths.get(fileDest);
  65. Files.write(path, bFile);
  66. } catch (IOException e) {
  67. e.printStackTrace();
  68. }
  69. }
  70. }
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93

28.将字符串转换成InputStream

  1. import java.io.BufferedReader;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. public class StringToInputStreamExample {
  7. public static void main(String[] args) throws IOException {
  8. String str = "This is a String ~ GoGoGo";
  9. // convert String into InputStream
  10. InputStream is = new ByteArrayInputStream(str.getBytes());
  11. // read it with BufferedReader
  12. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  13. String line;
  14. while ((line = br.readLine()) != null) {
  15. System.out.println(line);
  16. }
  17. br.close();
  18. }
  19. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

29.将InputStream转换为字符串

  1. import java.io.BufferedReader;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. public class InputStreamToStringExample {
  7. public static void main(String[] args) throws IOException {
  8. // intilize an InputStream
  9. InputStream is =
  10. new ByteArrayInputStream("file content..blah blah".getBytes());
  11. String result = getStringFromInputStream(is);
  12. System.out.println(result);
  13. System.out.println("Done");
  14. }
  15. // convert InputStream to String
  16. private static String getStringFromInputStream(InputStream is) {
  17. BufferedReader br = null;
  18. StringBuilder sb = new StringBuilder();
  19. String line;
  20. try {
  21. br = new BufferedReader(new InputStreamReader(is));
  22. while ((line = br.readLine()) != null) {
  23. sb.append(line);
  24. }
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. } finally {
  28. if (br != null) {
  29. try {
  30. br.close();
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. }
  36. return sb.toString();
  37. }
  38. }
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

30.将文件转换为十六进制


  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.PrintStream;
  6. public class File2Hex
  7. {
  8. public static void convertToHex(PrintStream out, File file) throws IOException {
  9. InputStream is = new FileInputStream(file);
  10. int bytesCounter =0;
  11. int value = 0;
  12. StringBuilder sbHex = new StringBuilder();
  13. StringBuilder sbText = new StringBuilder();
  14. StringBuilder sbResult = new StringBuilder();
  15. while ((value = is.read()) != -1) {
  16. //convert to hex value with "X" formatter
  17. sbHex.append(String.format("%02X ", value));
  18. //If the chracater is not convertable, just print a dot symbol "."
  19. if (!Character.isISOControl(value)) {
  20. sbText.append((char)value);
  21. }else {
  22. sbText.append(".");
  23. }
  24. //if 16 bytes are read, reset the counter,
  25. //clear the StringBuilder for formatting purpose only.
  26. if(bytesCounter==15){
  27. sbResult.append(sbHex).append(" ").append(sbText).append("\n");
  28. sbHex.setLength(0);
  29. sbText.setLength(0);
  30. bytesCounter=0;
  31. }else{
  32. bytesCounter++;
  33. }
  34. }
  35. //if still got content
  36. if(bytesCounter!=0){
  37. //add spaces more formatting purpose only
  38. for(; bytesCounter<16; bytesCounter++){
  39. //1 character 3 spaces
  40. sbHex.append(" ");
  41. }
  42. sbResult.append(sbHex).append(" ").append(sbText).append("\n");
  43. }
  44. out.print(sbResult);
  45. is.close();
  46. }
  47. public static void main(String[] args) throws IOException
  48. {
  49. //display output to console
  50. convertToHex(System.out, new File("c:/file.txt"));
  51. //write the output into a file
  52. convertToHex(new PrintStream("c:/file.hex"), new File("c:/file.txt"));
  53. }
  54. }
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

31.File如何得到空闲磁盘空间


  1. import java.io.File;
  2. public class DiskSpaceDetail
  3. {
  4. public static void main(String[] args)
  5. {
  6. File file = new File("c:");
  7. long totalSpace = file.getTotalSpace(); //total disk space in bytes.
  8. long usableSpace = file.getUsableSpace(); ///unallocated / free disk space in bytes.
  9. long freeSpace = file.getFreeSpace(); //unallocated / free disk space in bytes.
  10. System.out.println(" === Partition Detail ===");
  11. System.out.println(" === bytes ===");
  12. System.out.println("Total size : " + totalSpace + " bytes");
  13. System.out.println("Space free : " + usableSpace + " bytes");
  14. System.out.println("Space free : " + freeSpace + " bytes");
  15. System.out.println(" === mega bytes ===");
  16. System.out.println("Total size : " + totalSpace /1024 /1024 + " mb");
  17. System.out.println("Space free : " + usableSpace /1024 /1024 + " mb");
  18. System.out.println("Space free : " + freeSpace /1024 /1024 + " mb");
  19. }
  20. }

Java中的File操作总结的更多相关文章

  1. java学习(九) —— java中的File文件操作及IO流概述

    前言 流是干什么的:为了永久性的保存数据. IO流用来处理设备之间的数据传输(上传和下载文件) java对数据的操作是通过流的方式. java用于操作流的对象都在IO包中. java IO系统的学习, ...

  2. java中的File类

    File类 java中的File类其实和文件并没有多大关系,它更像一个对文件路径描述的类.它即可以代表某个路径下的特定文件,也可以用来表示该路径的下的所有文件,所以我们不要被它的表象所迷惑.对文件的真 ...

  3. Java中的文件操作

    在使用计算机编程中,常常会用到对于文件的操作,以下是我对于Java中文件的相关内容学习之后的一个总结和在学习过程中遇到的一些问题. 一.什么是文件 对于文件进行操作,首先我们要知道什么是文件.在此之前 ...

  4. Java中的文件操作(一)RandomAccessFile

    今天,学到的是java中的文件操作. Java.IO.File Java中操作文件用到RandomAccessFile类,既可以读取文件内容,也可以向文件输出数据,但不同与普通输入/输出流的是Rand ...

  5. Java中的IO操作和缓冲区

    目录 Java中的IO操作和缓冲区 一.简述 二.IO流的介绍 什么是流 输入输出流的作用范围 三.Java中的字节流和字符流 字节流 字符流 二者的联系 1.InputStreamReader 2. ...

  6. JAVA中的时间操作

    java中的时间操作不外乎这四种情况: 1.获取当前时间 2.获取某个时间的某种格式 3.设置时间 4.时间的运算 好,下面就针对这四种情况,一个一个搞定. 一.获取当前时间 有两种方式可以获得,第一 ...

  7. java中的集合操作类(未完待续)

    申明: 实习生的肤浅理解,如发现有错误之处.还望大牛们多多指点 废话 事实上我写java的后台操作,我每次都会遇到一条语句:List<XXXXX> list = new ArrayList ...

  8. JAVA中通过Jedis操作Redis连接与插入简单库

    一.简述 JAVA中通过Jedis操作Redis连接与插入简单库 二.依赖 <!-- https://mvnrepository.com/artifact/redis.clients/jedis ...

  9. JAVA中的File.separate(跨平台路径)

    转: JAVA中的File.separate(跨平台路径) 2016年03月27日 23:33:50 才不是本人 阅读数:1952   在Windows下的路径分隔符和Linux下的路径分隔符是不一样 ...

随机推荐

  1. DDos攻击解决办法

    (1).DDos概念 分布式拒绝服务攻击(英文意思是Distributed Denial of Service,简称DDoS)是指处于不同位置的多个攻击者同时向一个或数个目标发动攻击,或者一个攻击者控 ...

  2. html中的<pre>标签

    定义和用法 pre 元素可定义预格式化的文本.被包围在 pre 元素中的文本通常会保留空格和换行符.而文本也会呈现为等宽字体. <pre> 标签的一个常见应用就是用来表示计算机的源代码. ...

  3. python编程中的一些有用插件或工具

    windows监控 在python编程的windows系统监控中,需要监控监控硬件信息需要两个模块:WMI 和 pypiwin32 . 前端文件上传插件 krajee karkit 后台管理模板 ni ...

  4. spring 理解Spring AOP 一个简单的约定游戏

    应该说AOP原理是Spring技术中最难理解的一个部分,而这个约定游戏也许会给你很多的帮助,通过这个约定游戏,就可以理解Spring AOP的含义和实现方法,也能帮助读者更好地运用Spring AOP ...

  5. Flink 在IDEA执行时的webui

    不过Flink IDEA中执行的webui 需要 flink-runtime-web 包的支持 pom 如下: <dependency> <groupId>org.apache ...

  6. Text Prompted Remote Speaker Authentication : Joint Speech and Speaker Recognition/Verification System :: Major Project ::: Introduction

    转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2010/12/text-prompted-remote-speaker.html Biometrics ...

  7. web端自动化——Remote应用

    Selenium Grid允许同时并行地.在不同的环境上运行多个测试任务.这里主要演示一下怎么使用Selenium Grid. 准备: 1. 需要两台机子 2.两台机子分别安装好JDK环境 3.两台机 ...

  8. RabbitMQ官方教程五 Topic(GOLANG语言实现)

    在上一教程中,我们改进了日志记录系统. 我们没有使用只能进行虚拟广播的fanout交换器,而是使用直接交换器,并有可能选择性地接收日志. 尽管使用直接交换改进了我们的系统,但它仍然存在局限性-它不能基 ...

  9. js 次方 开方 对数

    次方 ,用Math.pow(值,次方数) 如: Math.pow(3,2);   3的平方 Math.Pow(2,3);   2的立方 开方Math.sqrt(值) 如: Math.sqrt(9);  ...

  10. Eclipse使用JDBC方式连接SQLServer2017

    这篇博客写的比较详细了,图文并茂: https://blog.csdn.net/rebekahq/article/details/78691343 这里补充一些可能会遇到的问题: 1.与博客中不同之处 ...