Source Code Cross Referenced for FileUtil.java in  » Portal » liferay-portal-4.4.2 » com » liferay » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » liferay portal 4.4.2 » com.liferay.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003:         *
004:         * Permission is hereby granted, free of charge, to any person obtaining a copy
005:         * of this software and associated documentation files (the "Software"), to deal
006:         * in the Software without restriction, including without limitation the rights
007:         * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008:         * copies of the Software, and to permit persons to whom the Software is
009:         * furnished to do so, subject to the following conditions:
010:         *
011:         * The above copyright notice and this permission notice shall be included in
012:         * all copies or substantial portions of the Software.
013:         *
014:         * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015:         * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016:         * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017:         * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018:         * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019:         * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020:         * SOFTWARE.
021:         */package com.liferay.util;
022:
023:        import com.liferay.portal.kernel.util.ByteArrayMaker;
024:        import com.liferay.portal.kernel.util.StringPool;
025:        import com.liferay.portal.kernel.util.StringUtil;
026:        import com.liferay.portal.kernel.util.Validator;
027:
028:        import java.io.BufferedInputStream;
029:        import java.io.BufferedReader;
030:        import java.io.BufferedWriter;
031:        import java.io.File;
032:        import java.io.FileInputStream;
033:        import java.io.FileOutputStream;
034:        import java.io.FileReader;
035:        import java.io.FileWriter;
036:        import java.io.IOException;
037:        import java.io.InputStream;
038:        import java.io.Reader;
039:
040:        import java.nio.channels.FileChannel;
041:
042:        import java.util.ArrayList;
043:        import java.util.Arrays;
044:        import java.util.List;
045:        import java.util.Properties;
046:
047:        import org.apache.commons.logging.Log;
048:        import org.apache.commons.logging.LogFactory;
049:
050:        /**
051:         * <a href="FileUtil.java.html"><b><i>View Source</i></b></a>
052:         *
053:         * @author Brian Wing Shun Chan
054:         * @author Alexander Chow
055:         *
056:         */
057:        public class FileUtil {
058:
059:            public static final String ENCODING = GetterUtil.getString(
060:                    SystemProperties.get("file.encoding"), "UTF-8");
061:
062:            public static void append(String fileName, String s)
063:                    throws IOException {
064:                append(new File(fileName), s);
065:            }
066:
067:            public static void append(String pathName, String fileName, String s)
068:                    throws IOException {
069:
070:                append(new File(pathName, fileName), s);
071:            }
072:
073:            public static void append(File file, String s) throws IOException {
074:                if (file.getParent() != null) {
075:                    mkdirs(file.getParent());
076:                }
077:
078:                BufferedWriter bw = new BufferedWriter(new FileWriter(file,
079:                        true));
080:
081:                bw.write(s);
082:
083:                bw.close();
084:            }
085:
086:            public static void copyDirectory(String sourceDirName,
087:                    String destinationDirName) {
088:
089:                copyDirectory(new File(sourceDirName), new File(
090:                        destinationDirName));
091:            }
092:
093:            public static void copyDirectory(File source, File destination) {
094:                if (source.exists() && source.isDirectory()) {
095:                    if (!destination.exists()) {
096:                        destination.mkdirs();
097:                    }
098:
099:                    File[] fileArray = source.listFiles();
100:
101:                    for (int i = 0; i < fileArray.length; i++) {
102:                        if (fileArray[i].isDirectory()) {
103:                            copyDirectory(fileArray[i], new File(destination
104:                                    .getPath()
105:                                    + File.separator + fileArray[i].getName()));
106:                        } else {
107:                            copyFile(fileArray[i], new File(destination
108:                                    .getPath()
109:                                    + File.separator + fileArray[i].getName()));
110:                        }
111:                    }
112:                }
113:            }
114:
115:            public static void copyFile(String source, String destination) {
116:                copyFile(source, destination, false);
117:            }
118:
119:            public static void copyFile(String source, String destination,
120:                    boolean lazy) {
121:
122:                copyFile(new File(source), new File(destination), lazy);
123:            }
124:
125:            public static void copyFile(File source, File destination) {
126:                copyFile(source, destination, false);
127:            }
128:
129:            public static void copyFile(File source, File destination,
130:                    boolean lazy) {
131:                if (!source.exists()) {
132:                    return;
133:                }
134:
135:                if (lazy) {
136:                    String oldContent = null;
137:
138:                    try {
139:                        oldContent = read(source);
140:                    } catch (Exception e) {
141:                        return;
142:                    }
143:
144:                    String newContent = null;
145:
146:                    try {
147:                        newContent = read(destination);
148:                    } catch (Exception e) {
149:                    }
150:
151:                    if (oldContent == null || !oldContent.equals(newContent)) {
152:                        copyFile(source, destination, false);
153:                    }
154:                } else {
155:                    if ((destination.getParentFile() != null)
156:                            && (!destination.getParentFile().exists())) {
157:
158:                        destination.getParentFile().mkdirs();
159:                    }
160:
161:                    try {
162:                        FileChannel srcChannel = new FileInputStream(source)
163:                                .getChannel();
164:                        FileChannel dstChannel = new FileOutputStream(
165:                                destination).getChannel();
166:
167:                        dstChannel.transferFrom(srcChannel, 0, srcChannel
168:                                .size());
169:
170:                        srcChannel.close();
171:                        dstChannel.close();
172:                    } catch (IOException ioe) {
173:                        _log.error(ioe.getMessage());
174:                    }
175:                }
176:            }
177:
178:            public static boolean delete(String file) {
179:                return delete(new File(file));
180:            }
181:
182:            public static boolean delete(File file) {
183:                if (file.exists()) {
184:                    return file.delete();
185:                } else {
186:                    return false;
187:                }
188:            }
189:
190:            public static void deltree(String directory) {
191:                deltree(new File(directory));
192:            }
193:
194:            public static void deltree(File directory) {
195:                if (directory.exists() && directory.isDirectory()) {
196:                    File[] fileArray = directory.listFiles();
197:
198:                    for (int i = 0; i < fileArray.length; i++) {
199:                        if (fileArray[i].isDirectory()) {
200:                            deltree(fileArray[i]);
201:                        } else {
202:                            fileArray[i].delete();
203:                        }
204:                    }
205:
206:                    directory.delete();
207:                }
208:            }
209:
210:            public static boolean exists(String fileName) {
211:                return exists(new File(fileName));
212:            }
213:
214:            public static boolean exists(File file) {
215:                return file.exists();
216:            }
217:
218:            public static String getAbsolutePath(File file) {
219:                return StringUtil.replace(file.getAbsolutePath(),
220:                        StringPool.BACK_SLASH, StringPool.SLASH);
221:            }
222:
223:            public static byte[] getBytes(File file) throws IOException {
224:                if ((file == null) || !file.exists()) {
225:                    return null;
226:                }
227:
228:                FileInputStream in = new FileInputStream(file);
229:
230:                byte[] bytes = getBytes(in, (int) file.length());
231:
232:                in.close();
233:
234:                return bytes;
235:            }
236:
237:            public static byte[] getBytes(InputStream in) throws IOException {
238:                return getBytes(in, -1);
239:            }
240:
241:            public static byte[] getBytes(InputStream in, int bufferSize)
242:                    throws IOException {
243:
244:                ByteArrayMaker out = null;
245:
246:                if (bufferSize <= 0) {
247:                    out = new ByteArrayMaker();
248:                } else {
249:                    out = new ByteArrayMaker(bufferSize);
250:                }
251:
252:                boolean createBuffered = false;
253:
254:                try {
255:                    if (!(in instanceof  BufferedInputStream)) {
256:                        in = new BufferedInputStream(in);
257:
258:                        createBuffered = true;
259:                    }
260:
261:                    int c = in.read();
262:
263:                    while (c != -1) {
264:                        out.write(c);
265:
266:                        c = in.read();
267:                    }
268:                } finally {
269:                    if (createBuffered) {
270:                        in.close();
271:                    }
272:                }
273:
274:                out.close();
275:
276:                return out.toByteArray();
277:            }
278:
279:            public static String getExtension(String fileName) {
280:                if (fileName == null) {
281:                    return null;
282:                }
283:
284:                int pos = fileName.lastIndexOf(StringPool.PERIOD);
285:
286:                if (pos != -1) {
287:                    return fileName.substring(pos + 1, fileName.length())
288:                            .toLowerCase();
289:                } else {
290:                    return null;
291:                }
292:            }
293:
294:            public static String getPath(String fullFileName) {
295:                int pos = fullFileName.lastIndexOf(StringPool.SLASH);
296:
297:                if (pos == -1) {
298:                    pos = fullFileName.lastIndexOf(StringPool.BACK_SLASH);
299:                }
300:
301:                String shortFileName = fullFileName.substring(0, pos);
302:
303:                if (Validator.isNull(shortFileName)) {
304:                    return StringPool.SLASH;
305:                }
306:
307:                return shortFileName;
308:            }
309:
310:            public static String getShortFileName(String fullFileName) {
311:                int pos = fullFileName.lastIndexOf(StringPool.SLASH);
312:
313:                if (pos == -1) {
314:                    pos = fullFileName.lastIndexOf(StringPool.BACK_SLASH);
315:                }
316:
317:                String shortFileName = fullFileName.substring(pos + 1,
318:                        fullFileName.length());
319:
320:                return shortFileName;
321:            }
322:
323:            public static String[] listDirs(String fileName) throws IOException {
324:                return listDirs(new File(fileName));
325:            }
326:
327:            public static String[] listDirs(File file) throws IOException {
328:                List dirs = new ArrayList();
329:
330:                File[] fileArray = file.listFiles();
331:
332:                for (int i = 0; i < fileArray.length; i++) {
333:                    if (fileArray[i].isDirectory()) {
334:                        dirs.add(fileArray[i].getName());
335:                    }
336:                }
337:
338:                return (String[]) dirs.toArray(new String[0]);
339:            }
340:
341:            public static String[] listFiles(String fileName)
342:                    throws IOException {
343:                if (Validator.isNull(fileName)) {
344:                    return new String[0];
345:                }
346:
347:                return listFiles(new File(fileName));
348:            }
349:
350:            public static String[] listFiles(File file) throws IOException {
351:                List files = new ArrayList();
352:
353:                File[] fileArray = file.listFiles();
354:
355:                for (int i = 0; (fileArray != null) && (i < fileArray.length); i++) {
356:                    if (fileArray[i].isFile()) {
357:                        files.add(fileArray[i].getName());
358:                    }
359:                }
360:
361:                return (String[]) files.toArray(new String[0]);
362:            }
363:
364:            public static void mkdirs(String pathName) {
365:                File file = new File(pathName);
366:
367:                file.mkdirs();
368:            }
369:
370:            public static boolean move(String sourceFileName,
371:                    String destinationFileName) {
372:
373:                return move(new File(sourceFileName), new File(
374:                        destinationFileName));
375:            }
376:
377:            public static boolean move(File source, File destination) {
378:                if (!source.exists()) {
379:                    return false;
380:                }
381:
382:                destination.delete();
383:
384:                return source.renameTo(destination);
385:            }
386:
387:            public static String read(String fileName) throws IOException {
388:                return read(new File(fileName));
389:            }
390:
391:            public static String read(File file) throws IOException {
392:                return read(file, false);
393:            }
394:
395:            public static String read(File file, boolean raw)
396:                    throws IOException {
397:                return read(file, ENCODING, raw);
398:            }
399:
400:            public static String read(File file, String encoding, boolean raw)
401:                    throws IOException {
402:
403:                FileInputStream fis = new FileInputStream(file);
404:
405:                byte[] bytes = new byte[fis.available()];
406:
407:                fis.read(bytes);
408:
409:                fis.close();
410:
411:                String s = new String(bytes, encoding);
412:
413:                if (raw) {
414:                    return s;
415:                } else {
416:                    return StringUtil.replace(s, StringPool.RETURN_NEW_LINE,
417:                            StringPool.NEW_LINE);
418:                }
419:            }
420:
421:            public static File[] sortFiles(File[] files) {
422:                if (files == null) {
423:                    return null;
424:                }
425:
426:                Arrays.sort(files, new FileComparator());
427:
428:                List directoryList = new ArrayList();
429:                List fileList = new ArrayList();
430:
431:                for (int i = 0; i < files.length; i++) {
432:                    if (files[i].isDirectory()) {
433:                        directoryList.add(files[i]);
434:                    } else {
435:                        fileList.add(files[i]);
436:                    }
437:                }
438:
439:                directoryList.addAll(fileList);
440:
441:                return (File[]) directoryList.toArray(new File[0]);
442:            }
443:
444:            public static String replaceSeparator(String fileName) {
445:                return StringUtil.replace(fileName, StringPool.BACK_SLASH,
446:                        StringPool.SLASH);
447:            }
448:
449:            public static List toList(Reader reader) {
450:                List list = new ArrayList();
451:
452:                try {
453:                    BufferedReader br = new BufferedReader(reader);
454:
455:                    String line = null;
456:
457:                    while ((line = br.readLine()) != null) {
458:                        list.add(line);
459:                    }
460:
461:                    br.close();
462:                } catch (IOException ioe) {
463:                }
464:
465:                return list;
466:            }
467:
468:            public static List toList(String fileName) {
469:                try {
470:                    return toList(new FileReader(fileName));
471:                } catch (IOException ioe) {
472:                    return new ArrayList();
473:                }
474:            }
475:
476:            public static Properties toProperties(FileInputStream fis) {
477:                Properties props = new Properties();
478:
479:                try {
480:                    props.load(fis);
481:                } catch (IOException ioe) {
482:                }
483:
484:                return props;
485:            }
486:
487:            public static Properties toProperties(String fileName) {
488:                try {
489:                    return toProperties(new FileInputStream(fileName));
490:                } catch (IOException ioe) {
491:                    return new Properties();
492:                }
493:            }
494:
495:            public static void write(String fileName, String s)
496:                    throws IOException {
497:                write(new File(fileName), s);
498:            }
499:
500:            public static void write(String fileName, String s, boolean lazy)
501:                    throws IOException {
502:
503:                write(new File(fileName), s, lazy);
504:            }
505:
506:            public static void write(String pathName, String fileName, String s)
507:                    throws IOException {
508:
509:                write(new File(pathName, fileName), s);
510:            }
511:
512:            public static void write(String pathName, String fileName,
513:                    String s, boolean lazy) throws IOException {
514:
515:                write(new File(pathName, fileName), s, lazy);
516:            }
517:
518:            public static void write(File file, String s) throws IOException {
519:                write(file, s, false);
520:            }
521:
522:            public static void write(File file, String s, boolean lazy)
523:                    throws IOException {
524:
525:                write(file, s, lazy, false);
526:            }
527:
528:            public static void write(File file, String s, boolean lazy,
529:                    boolean append) throws IOException {
530:
531:                if (file.getParent() != null) {
532:                    mkdirs(file.getParent());
533:                }
534:
535:                if (lazy && file.exists()) {
536:                    String content = read(file);
537:
538:                    if (content.equals(s)) {
539:                        return;
540:                    }
541:                }
542:
543:                BufferedWriter bw = new BufferedWriter(new FileWriter(file,
544:                        append));
545:
546:                bw.write(s);
547:
548:                bw.close();
549:            }
550:
551:            public static void write(String fileName, byte[] byteArray)
552:                    throws IOException {
553:
554:                write(new File(fileName), byteArray);
555:            }
556:
557:            public static void write(File file, byte[] byteArray)
558:                    throws IOException {
559:                if (file.getParent() != null) {
560:                    mkdirs(file.getParent());
561:                }
562:
563:                FileOutputStream fos = new FileOutputStream(file);
564:
565:                fos.write(byteArray);
566:
567:                fos.close();
568:            }
569:
570:            public static void write(String fileName, InputStream in)
571:                    throws IOException {
572:
573:                write(fileName, getBytes(in));
574:            }
575:
576:            public static void write(File file, InputStream in)
577:                    throws IOException {
578:                write(file, getBytes(in));
579:            }
580:
581:            private static Log _log = LogFactory.getLog(FileUtil.class);
582:
583:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.