upsidedown

files

Mar 5th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.50 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. class WSExample1
  4. {
  5.     public static void main(String args[])
  6.     {
  7.         write("C:/dell/file.txt"); 
  8.         read("C:/dell/file.txt");  
  9.         copy("C:/dell/file.txt","C:/dell/copiedfile.txt");
  10.         //delete("C:/dell/file.txt");  
  11.        
  12.         append("C:/dell/copiedfile.txt");  
  13.     }
  14.    
  15.    
  16.    
  17.      public static int write(String path)
  18.      {
  19.         try
  20.         {
  21.      
  22.             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  23.             String str;
  24.      
  25.               BufferedWriter out = new BufferedWriter(new FileWriter(path));
  26.            
  27.      
  28.             System.out.println("Enter lines of text.");
  29.             System.out.println("Enter 'end' to quit.");
  30.             do {
  31.          
  32.                 str = br.readLine();
  33.                 if(!str.equals("end"))
  34.                 {
  35.                 out.write(str);
  36.                 out.newLine();
  37.                 }
  38.              
  39.               } while(!str.equals("end"));
  40.               out.close();
  41.         }
  42.        
  43.         catch(IOException ioe){
  44.                 System.out.println("Exception at write");
  45.             }
  46.       return 0;
  47.     }
  48.    
  49.    
  50.     public static int read(String path)
  51.      {
  52.         try
  53.         {
  54.      
  55.             BufferedReader br = new BufferedReader(new FileReader(path));
  56.             String str;
  57.      
  58.                  
  59.             System.out.println("The contents of the file are");
  60.            
  61.             String line = br.readLine();
  62.             while(line!=null)
  63.             {
  64.                 System.out.println(line);  
  65.                 line = br.readLine();
  66.             }
  67.            
  68.             br.close();
  69.         }
  70.            
  71.        
  72.         catch(IOException ioe)
  73.             {
  74.                 System.out.println("Exception at read");
  75.             }
  76.       return 0;
  77.     }
  78.    
  79.     public static int delete(String path)
  80.     {
  81.         try{
  82.  
  83.             File f= new File(path);
  84.  
  85.             if(f.delete())
  86.             {
  87.                 System.out.println("file deleted");
  88.             }
  89.            
  90.             else
  91.             {
  92.                 System.out.println("Delete failed.");
  93.             }
  94.  
  95.         }
  96.        
  97.         catch(Exception e){
  98.             System.out.println("Exception at DELETE");
  99.         }
  100.        
  101.         return 0;
  102.        
  103.     }
  104.    
  105.    
  106.    
  107.    
  108.     public static int copy(String path1, String path2)
  109.      {
  110.         try
  111.         {
  112.      
  113.             BufferedReader br = new BufferedReader(new FileReader(path1));
  114.              BufferedWriter out = new BufferedWriter(new FileWriter(path2));
  115.             String str;
  116.      
  117.                
  118.            
  119.            
  120.             String line = br.readLine();
  121.             while(line!=null)
  122.             {
  123.                 out.write(line);
  124.                 out.newLine();
  125.                 line = br.readLine();
  126.             }
  127.            
  128.             br.close();
  129.             out.close();
  130.         }
  131.            
  132.        
  133.         catch(IOException ioe)
  134.             {
  135.                 System.out.println("Exception at read");
  136.             }
  137.       return 0;
  138.     }
  139.    
  140.    
  141.    
  142.     public static int append(String path)
  143.      {
  144.         try
  145.         {
  146.      
  147.             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  148.             String str;
  149.      
  150.               BufferedWriter out = new BufferedWriter(new FileWriter(path, true )); //FileWriter(file path,boolean append )
  151.            
  152.      
  153.             System.out.println("Enter lines of text to append to " + path);
  154.             System.out.println("Enter 'end' to quit.");
  155.             do {
  156.                 str = br.readLine();
  157.                 if(!str.equals("end"))
  158.                 {
  159.                 out.write(str);
  160.                 out.newLine();
  161.                 }
  162.              
  163.               } while(!str.equals("end"));
  164.               out.close();
  165.         }
  166.        
  167.         catch(IOException ioe){
  168.                 System.out.println("Exception at write");
  169.             }
  170.       return 0;
  171.     }
  172.    
  173.    
  174. }
Advertisement
Add Comment
Please, Sign In to add comment