2 * Description: Global package for file operations.
4 * @ Author Create/Modi Note
5 * Xiaofeng Xie Jun 15, 2002
12 package net
.adaptivebox
.global
;
17 public class GlobalFile
{
19 // used by the createTempDir to give an index of temp number.
20 private static int counter
= -1;
23 * Create a temp directory in the given directory.
24 * @param prefix the prefix for the directory.
25 * @param directory the directory that the temp dirctory placed.
26 * @return If a temp directory is created, return a File Object, else
29 public static File
createTempDir(String prefix
, String directory
)
33 boolean isCreated
= false;
36 counter
= new Random().nextInt() & 0xffff;
40 throw new NullPointerException();
41 if (prefix
.length() < 3)
42 throw new IllegalArgumentException("Prefix string too short");
43 if (directory
== null) {
44 tempDir
= prefix
+ counter
;
46 tempDir
= getFileLocation(directory
, prefix
+ counter
);
48 f
= new File(tempDir
);
49 isCreated
= f
.mkdir();
55 * Add the given text string to the end of a given file.
56 * @param inStr The string to be added.
57 * @param fileStr the name of the file to be added.
59 public static void addStringToFile(String inStr
, String fileStr
) throws Exception
{
61 RandomAccessFile raFile
= new RandomAccessFile(fileStr
,"rw");
62 raFile
.seek(raFile
.length());
63 raFile
.writeBytes(inStr
);
67 // String oldFileStr = getStringFromFile(fileStr);
68 // String newFileStr = inStr;
69 // if (oldFileStr != null) {
70 // newFileStr = oldFileStr+inStr;
72 // saveStringToFile(newFileStr,fileStr);
76 public static Object
loadObjectFromFile(String fileName
) throws Exception
{
77 FileInputStream fis
= new FileInputStream(fileName
);
78 ObjectInputStream ois
= new ObjectInputStream(fis
);
79 Object obj
= ois
.readObject();
84 public static void saveObjectToFile(String fileName
, Object obj
) throws Exception
{
85 FileOutputStream ostream
= new FileOutputStream(fileName
);
86 ObjectOutputStream p
= new ObjectOutputStream(ostream
);
93 * Save the given text string to a given file.
94 * @param inStr The string to be saved.
95 * @param fileStr the name of the file to be saved.
97 public static void saveStringToFile(String inStr
, String fileStr
) throws Exception
{
98 new File(new File(fileStr
).getParent()).mkdirs();
99 FileOutputStream pspOutputStream
= new FileOutputStream(new File(fileStr
));
100 pspOutputStream
.write(inStr
.getBytes());
101 pspOutputStream
.close();
105 * Load text string from a given file.
106 * @param fileStr the name of the file to be loaded.
107 * @return A text string that is the content of the file. if the given file is
108 * not exist, then return null.
110 public static String
getStringFromFile(String fileStr
) throws Exception
{
111 String getStr
= null;
112 FileInputStream pspInputStream
= new FileInputStream(fileStr
);
113 byte[] pspFileBuffer
= new byte[pspInputStream
.available()];
114 pspInputStream
.read(pspFileBuffer
);
115 pspInputStream
.close();
116 getStr
= new String(pspFileBuffer
);
121 * Load curve data from a specified file.
122 * @param fileStr the name of the file to be loaded.
123 * @return An ArrayList that include the curve data.
125 public static ArrayList
<ArrayList
<Double
>> getCurveDataFromFile(String fileName
) {
126 File file
= new File(fileName
);
132 FileInputStream inStream
= null;
133 BufferedReader inReader
= null;
135 inStream
= new FileInputStream(file
);
136 inReader
= new BufferedReader(new InputStreamReader(inStream
));
139 return null;//Data file open error.
141 ArrayList
<Double
> xaxes
= new ArrayList
<Double
>(1);
142 ArrayList
<Double
> yaxes
= new ArrayList
<Double
>(1);
146 boolean start
= false;
147 while(inReader
.ready()!=false){
148 st
= new StringTokenizer(inReader
.readLine());
150 while(!st
.hasMoreTokens()){//Justify blank lines.
151 if(inReader
.ready()!=false){
152 st
= new StringTokenizer(inReader
.readLine());
157 if((!start
)&&(!s
.startsWith("@")))
163 if(s
.startsWith("#")||s
.startsWith("$")||s
.startsWith("/")) break over
;//Justify comment line.
167 xaxis
= Double
.valueOf(s
);
169 }catch(NumberFormatException e
){
173 return null;//Data file data format error.
177 yaxis
= Double
.valueOf(s
);
179 }catch(NumberFormatException e
){
183 return null;//Data file data format error.
190 return null;//Uncertain data file error.
192 ArrayList
<ArrayList
<Double
>> curveData
= new ArrayList
<ArrayList
<Double
>>(2);
193 curveData
.add(xaxes
);
194 curveData
.add(yaxes
);
199 * Get a full path of a given file name and a directory name.
200 * @param fileName the name of the file.
201 * @param dir the name of directory.
202 * @return The full path.
204 public static String
getFileLocation(String dir
, String fileName
) {
205 String realDir
= dir
;
206 while (realDir
.length()>0 && (realDir
.endsWith("/")||realDir
.endsWith("\\"))) {
207 realDir
= dir
.substring(0, dir
.length()-1);
209 return realDir
+BasicTag
.FILE_SEP_TAG
+fileName
;
212 public static String
getFileName(String nameBody
, String suffix
) {
213 if (suffix
==null || suffix
.trim().length()==0) {
216 String fileName
= nameBody
;
217 if(nameBody
.endsWith(".")) {
218 return fileName
+suffix
;
220 return nameBody
+"."+suffix
;
224 public static String
getFileLocation(String dir
, String fileNameBody
, String fileNameSuffix
) {
225 String filename
= getFileName(fileNameBody
, fileNameSuffix
);
226 return getFileLocation(dir
, filename
);
229 public static void clear(String fileStr
) throws Exception
{
230 File file
= new File(fileStr
);
235 String
[] fileNames
= file
.list();
236 if (fileNames
==null) {
239 for (int i
=0; i
<fileNames
.length
; i
++) {
240 String newFileName
= GlobalFile
.getFileLocation(fileStr
,fileNames
[i
]);
246 public static String
getFilePrefix(String fileStr
) {
247 int index
= fileStr
.lastIndexOf(BasicTag
.DOT_TAG
);
248 if(index
==-1) index
= fileStr
.length();
249 return fileStr
.substring(0, index
);
252 public static String
getFileSuffix(String fileStr
) {
253 String
[] subNames
= GlobalString
.tokenize(fileStr
, BasicTag
.DOT_TAG
);
254 int subNameLen
= subNames
.length
;
255 if(subNameLen
==1) return "";
256 else return subNames
[subNameLen
-1];
259 public static String
createTempImageFile(String origFile
) throws Exception
{
260 return createTempImageFile(origFile
, "img", ".inf");
263 public static String
createTempImageFile(String origFile
, String prefix
, String suffix
) throws Exception
{
264 File outputFile
= createTempFile(prefix
, suffix
);
265 outputFile
.deleteOnExit();
266 copyFile(outputFile
.getAbsolutePath(), origFile
);
267 return outputFile
.getAbsolutePath();
270 public static void copyFile(String imgFile
, String origFile
) throws Exception
{
271 String fileContent
= GlobalFile
.getStringFromFile(origFile
);
272 GlobalFile
.saveStringToFile(fileContent
, imgFile
);
275 public static File
createTempFile(String prefix
, String suffix
) throws Exception
{
276 String realSuffix
= suffix
;
277 if (!realSuffix
.startsWith(".")) realSuffix
= "."+suffix
;
278 return File
.createTempFile(prefix
, realSuffix
);