File Operations (IO)

JAVA file operations

java IO ~ summarize the basic operations

1 file write

2 reads the contents of the file

3 Delete Files

4 Copy the contents of the file to another file

5 append data to the file

6 create a temporary file

7 modify the file last modification date

8 Get File Size

9 file renaming

10 is provided to read-only file

11 detects whether a file exists

12 Create a file in the specified directory

13 Get file need to change the time

14 Create a file

15 Compare the file path

  1 import java.io.*;
  2 import java.util.*;
  3 
  4 public class FileControl {
  5     public static void main(String[] args) throws Exception{
  6         FileWrite();
  7         FileRead();
  8         FileDel();
  9         FileCopy();
 10         FileAddData();
 11         FileCreatTemporary();
 12         FileLastDate();
 13         FileGetSize();
 14         FileRename();
 15         FileOnlyRead();
 16         FileIfExist();
 17         FileCreatFile();
 18         FileGetChangeDate();
 19         FileCreat();
 20         FilePathCompare();
 21     }
 22 
 23     // 文件写入
 24     private static void FileWrite(){
 25         try {
 26             BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
 27             out.write("w3cSchool");
 28             the out.close ();
 29              System.out.println ( "file is successfully created!" );
 30          } the catch (IOException E) {
 31 is              e.printStackTrace ();
 32          }
 33 is      }
 34 is      // read the contents of the file 
35      Private  static  void FileRead () {
 36          the try {
 37 [              the BufferedReader in = new new the BufferedReader ( new new the FileReader ( "the test.log" ));
 38 is              String STR;
 39              the while ! ((STR = in.readLine ()) =null ) {
 40                  System.out.println (STR);
 41 is              }
 42 is          } the catch (IOException E) {
 43 is              e.printStackTrace ();
 44 is          }
 45      }
 46 is      // delete the file 
47      Private  static  void FileDel () {
 48          the try {
 49              file file = new new file ( "named text.txt in" );
 50              IF (File.delete ()) {
 51 is                  System.out.println (file.getName () + "the file has been deleted!" );
52 is              } the else {
 53 is                  System.out.println ( "document delete failed!" );
 54 is              }
 55          } the catch (Exception E) {
 56 is              e.printStackTrace ();
 57 is          }
 58      }
 59      // contents of the file copied to another file 
60      Private  static  void the FileCopy () throws Exception {
 61 is          BufferedWriter, = OUT1 of new new BufferedWriter, ( new new FileWriter ( "srcFile" ));
 62 is         out1.write("String to be copied\n");
 63         out1.close();
 64         InputStream in = new FileInputStream(new File("srcFile"));
 65         OutputStream out = new FileOutputStream(new File("destnfile"));
 66         byte[] buf = new byte[1024];
 67         int len;
 68         while ((len = in.read(buf)) > 0){
 69             out.write(buf, 0, len);
 70         }
 71         in.close();
 72         out1.close();
 73         BufferedReader in1 = new BufferedReader(new FileReader("destnfile"));
 74         String str;
 75         while ((str = in1.readLine()) != null){
 76             System.out.println(str);
 77         }
 78         in1.close();
 79     }
 80     // 向文件中追加数据
 81     private static void FileAddData(){
 82         try {
 83             BufferedWriter out = new BufferedWriter(new FileWriter("filename"));
 84             out.write("aString1\n");
 85             out.close();
 86             out = new BufferedWriter(new FileWriter("filename", true));
 87             out.write("aString2");
 88             out.close();
 89             BufferedReader in = new BufferedReader(new FileReader("filename"));
 90             String str;
 91             while ((str = in.readLine()) != null){
 92                 System.out.println(str);
 93             }
 94             in.close();
 95         }catch (IOException e){
 96             System.out.println("Exception occoured" + e);
 97         }
 98     }
 99     // 创建临时文件
100     private static void FileCreatTemporary() throws Exception{
101         File temp = File.createTempFile("pattern", ".suffix");
102         temp.deleteOnExit ();
 103          BufferedWriter, OUT = new new BufferedWriter, ( new new FileWriter (TEMP));
 104          out.write ( "aString" );
 105          System.out.println ( "temporary file created" );
 106          the out.close ( );
 107      }
 108      // modify the date the file was last 
109      Private  static  void FileLastDate () throws Exception {
 110          file fileToChange = new new file ( "myJava.txt" );
 111          fileToChange.createNewFile ();
 112         Date filetime = new Date(fileToChange.lastModified());
113         System.out.println(filetime.toString());
114         System.out.println(fileToChange.setLastModified(System.currentTimeMillis()));
115         filetime = new Date(fileToChange.lastModified());
116         System.out.println(filetime.toString());
117     }
118     // 获取文件大小
119     public static long getFileSize(String filename){
120         File file = new File(filename);
121         if (!file.exists() || !file.isFile ()) {
 122              System.out.println ( "file does not exist" );
 123              return -1 ;
 124          }
 125          return file.length ();
 126      }
 127      Private  static  void FileGetSize () {
 128          Long size = getFileSize ( "java.txt" );
 129          System.out.println ( "java file size is:" + size);
 130.      }
 131 is      // file renaming 
132      Private  static  void FileRename () {
 133          file in oldName = new newFile ( "program.txt" );
 134          File newName = new new File ( "java.txt" );
 135          IF (oldName.renameTo (newName)) {
 136              System.out.println ( "has been renamed" );
 137          } the else {
 138              System.out.println ( "Error" );
 139          }
 140      }
 141 is      // set the file read-only mode 
142      Private  static  void FileOnlyRead () {
 143          file file = new new file ( "java.txt" );
 144         System.out.println (file.setReadOnly ());
 145          System.out.println (file.canWrite ());
 146      }
 147      // detecting whether a file exists 
148      Private  static  void FileIfExist () {
 149          File File = new new File ( "java.txt" );
 150          System.out.println (File.Exists ());
 151      }
 152      // create a file in the specified file 
153      Private  static  void FileCreatFile () throws Exception {
 154          file file = null ;
 155         File dir = new File("E:/JAVA_CODE/");
156         file = File.createTempFile("JavaTemp", ".javatemp", dir);
157         System.out.println(file.getPath());
158     }
159     // 获取文件修改时间
160     private static void FileGetChangeDate(){
161         File file = new File("MAIN.java");
162         Long lastModified = file.lastModified();
163         Date date = new Date(lastModified);
164         System.out.println(date);
165     }
 166      // create a file 
167 is      Private  static  void FileCreat () {
 168          the try {
 169              File File = new new File ( "E: /JAVA_CODE/myfile.txt" );
 170.              IF (file.createNewFile ())
 171 is                  the System.out. println ( "create a file success!" );
 172              the else System.out.println ( "error creating a file!" );
 173          } the catch (IOException IO) {
 174              io.printStackTrace ();
 175          }
 176      }
 177      //Comparison file path 
178      Private  static  void FilePathCompare () {
 179          File file1 = new new File ( "E: /demo1/demo.txt" );
 180 [          File file2 = new new File ( "E: /demo2/demo.txt" );
 181          IF (file1.compareTo (file2) == 0 ) {
 182              System.out.println ( "consistent with the file path!" );
 183 is          } the else {
 184              System.out.println ( "different file path!" );
 185          }
 186      }
 187 }
View Code

 

Guess you like

Origin www.cnblogs.com/skygrass0531/p/12307130.html