KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > webEditor > util > FileAccess


1 /* $Id: FileAccess.java,v 1.4 2001/02/26 23:41:01 agarcia3 Exp $
2     webEditor. The new way in content management
3     Copyright (C) 2001 Alfredo Garcia
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     GNU General Public License for more details.
13 */

14
15 package webEditor.util;
16
17 import java.io.*;
18 import java.util.*;
19 import org.apache.regexp.RE;
20
21 /**
22  * Encapsulates certain file access operations
23  *
24  * @author <a HREF="mailto:agarcia@mundofree.com">Alfredo Garcia</a>
25  */

26
27 public class FileAccess
28 {
29    /**
30     * Read the content of a file in a single string
31     * @param file File to read
32     * @return String Content of the file
33     */

34    public String JavaDoc readFile (String JavaDoc file)
35    {
36     RandomAccessFile fileDesc;
37     String JavaDoc buffer = null;
38     String JavaDoc line = null;
39
40 try {
41     fileDesc = new RandomAccessFile (file,"r");
42     while ( (line = fileDesc.readLine() ) != null ) {
43         if ( buffer == null ) {
44             buffer = line + "\n";
45         }
46         else {
47             buffer = buffer + line + "\n";
48         }
49     }
50
51 } catch (Exception JavaDoc e) {
52     e.printStackTrace();
53 }
54     return (buffer);
55    }
56
57
58   /**
59     * Create the directory where the data file is stored
60     * @param docName doc ID (usually the relative path)
61     * @param path Absolute path from the document
62     * @return void
63     */

64    public void createDocDir (String JavaDoc docName, String JavaDoc path)
65    {
66 try {
67     // We use this regular expresion in order to match xml files
68
// if the docName string doesn´t include the "/" string is because
69
// is not a normal document
70
RE r = new RE("/");
71     if ( r.match (docName) ) {
72
73       StringTokenizer token = new StringTokenizer (docName,"/");
74       // We have strings with the format /yyyy/mm/dd/file.xml
75

76       String JavaDoc yearDir = path + token.nextToken();
77       File fileDesc = new File (yearDir);
78       if (! fileDesc.exists() ) {
79         fileDesc.mkdir ();
80       }
81
82       String JavaDoc monthDir = yearDir + "/" + token.nextToken();
83       fileDesc = new File (monthDir);
84       if (! fileDesc.exists() ) {
85         fileDesc.mkdir ();
86       }
87
88       String JavaDoc dayDir = monthDir + "/" + token.nextToken();
89       fileDesc = new File (dayDir);
90       if (! fileDesc.exists() ) {
91         fileDesc.mkdir ();
92       }
93     }
94 }
95 catch (Exception JavaDoc e) {}
96
97    }
98
99    
100 }
101
Popular Tags