KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > communicator > applications > webtalk > model > FileUtilities


1 /*
2  * FileUtilities.java
3  *
4  * Created on June 6, 2003, 7:59 AM
5  */

6
7 package com.quikj.application.communicator.applications.webtalk.model;
8
9 import java.io.*;
10 /**
11  *
12  * @author Vinod Batra
13  */

14 public class FileUtilities {
15     
16     /** Creates a new instance of FileUtilities */
17     public FileUtilities() {
18     }
19     
20     public boolean fileCopy(File inFile, File outFile) throws IOException {
21         
22         if (inFile.getCanonicalPath().equals(outFile.getCanonicalPath())) {
23             // inFile and outFile are the same,
24
// hence no copying is required
25
System.out.println("Source and destination file same. No copying required");
26             
27             return false;
28         }
29         
30         FileInputStream fin = new FileInputStream(inFile);
31         FileOutputStream fout = new FileOutputStream(outFile);
32         StreamCopy(fin, fout);
33         fin.close();
34         fout.close();
35         
36         return true;
37     }
38     
39     public String JavaDoc getDirName (String JavaDoc fileType,File file)
40     {
41         String JavaDoc dir = "";
42         String JavaDoc name = file.getName();
43         if (fileType.equals("htmlFile"))
44             {
45                 
46                 if((name.endsWith(".htm")) || (name.endsWith(".html"))
47                 || (name.endsWith(".HTM")) || (name.endsWith(".HTML")))
48                 {
49                    dir = "html";
50                 }
51                 
52             }
53             else if (fileType.equals("imageFile"))
54             {
55                 if((name.endsWith(".gif")) || (name.endsWith(".jpeg"))
56                 ||(name.endsWith(".jpg")) || (name.endsWith(".png"))
57                 ||(name.endsWith(".GIF")) || (name.endsWith(".JPG"))
58                 ||(name.endsWith(".JPEG")) || (name.endsWith(".PNG")))
59                 {
60                     dir = "icons";
61                 }
62                 
63             }
64             
65             return dir ;
66         
67     }
68     
69     private void StreamCopy(InputStream in, OutputStream out)
70     throws IOException {
71         
72         // do not allow other threads to read from the
73
// input or write to the output while copying is
74
// taking place
75

76         synchronized (in) {
77             synchronized (out) {
78                 
79                 byte[] buffer = new byte[1024];
80                 while (true) {
81                     int bytesRead = in.read(buffer);
82                     if (bytesRead == -1) break;
83                     out.write(buffer, 0, bytesRead);
84                 }
85             }
86         }
87     }
88     
89 }
90
Popular Tags