The JAVA file operations 1,2,3,4

. 1  Package first_program;
 2  
. 3  Import java.io.File;
 . 4  Import java.io.IOException;
 . 5  
. 6  public  class num_1v {
 . 7          public  static  void main (String [] args) {
 . 8              File File = new new File ( "E: \ \ src.txt "); // create a file object 
9              IF (File.Exists ()) { // If the file exists 
10                  File.delete (); // file delete 
11                  System.out.println (" deleted files " );
 12              }the else {
 13 is                  the try { // the try statement block catch an exception may occur 
14                      file.createNewFile (); // Create the file 
15                  } the catch (IOException E) {
 16                      e.printStackTrace ();
 . 17                  }
 18 is                  System.out.println ( "file has been created" );
 19              }
 20          }
 21 }
 1 package first_program;
 2 
 3 import java.io.*;
 4 
 5 public class num_2v {
 6     @SuppressWarnings("resource")
 7     public static void main(String[] args) {
 8         File file = new File("e:\\dest.txt");
 9         String str = "12345abcde@#$%&*软件技术专业435345";
10         try {
11             FileOutputStream fos = new FileOutputStream(file, true);
12             for(int i = 0;i < str.length();i++){
13                 fos.write((char)str.charAt(i));
14             }
15             /*byte[] buf = str.getBytes();
16              fos.write(buf);*/
17         } catch (Exception e) {
18             e.printStackTrace();
19         }
20     }
21 }

 

. 1  Package first_program;
 2  
. 3  Import the java.io. * ;
 . 4  / * file input stream of bytes application FileInputStream
 5  modified in num_2v program reads the file "dest.txt", the read data is output in the console.
. 6   * / 
. 7  public  class num_3v {
 . 8      public  static  void main (String [] args) {
 . 9          // Create a file object 
10          File File = new new File ( "E: \\ Dest.txt" );
 . 11          the try {
 12 is              // create a file using the file object input stream object corresponding to open the file 
13 is              the FileInputStream FIS = new newThe FileInputStream (File);
 14              / * for (int I = 0; I <file.length (); I ++) {
 15                   char C = (char) (fis.read ()); // loop reads characters
 16                   System. Out.print (C);
 . 17               }
 18 is               * / 
. 19              // create a byte array in accordance with the byte length of the file, forced into the int type 
20 is              byte [] = buf new new  byte [( int ) file.length ()];
 21              // read data stored in the file byte array 
22 is              fis.read (buf);
 23 is              // using the byte array to create a string 
24              string STR = new new string (buf);
 25              //输出
26             System.out.println(str);
27             fis.close();
28         } catch (Exception e) {
29             e.printStackTrace();
30         }
31     }
32 }
 1 package first_program;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 
 8 public class num_4v {
 9     public static void main(String[] args) throws IOException{
10         File srcFile = new File("e:\\src.txt");//源文件对象
11         File destFile = new File("e:\\dest.txt");//Target file object 
12 is          IF {((destFile.exists ())!) // determines whether the target file exists 
13 is              destFile.createNewFile (); // If a new file is created does not exist 
14          }
 15          // use the source file object file created input stream object 
16          the FileInputStream FIS = new new the FileInputStream (srcFile);
 . 17          // Create a file output stream object using the target file object 
18 is          a FileOutputStream fos = new new a FileOutputStream (the destFile);
 . 19          byte [] = buf new new  byte [1024]; // Create byte array, as a temporary buffer 
20 is          System.out.println ( "start copy file" );
 21         the while (! fis.read (buf) = -1) { // the recycle stream to read data from a file input 
22 is              fos.write (buf); // written to the file output stream 
23 is          }
 24          System.out.println ( "file copied successfully" );
 25          fis.close (); // Close the stream 
26 is          fos.close ();
 27          
28      }
 29 }

 

Guess you like

Origin www.cnblogs.com/WuYangdan-5201314/p/10947920.html