📄 tempfileservice.java
字号:
/*
* YALE - Yet Another Learning Environment
* Copyright (C) 2001-2004
* Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,
* Katharina Morik, Oliver Ritthoff
* Artificial Intelligence Unit
* Computer Science Department
* University of Dortmund
* 44221 Dortmund, Germany
* email: yale-team@lists.sourceforge.net
* web: http://yale.cs.uni-dortmund.de/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package edu.udo.cs.yale.tools;
import edu.udo.cs.yale.Yale;
import edu.udo.cs.yale.Experiment;
import edu.udo.cs.yale.operator.OperatorException;
import java.io.*;
import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;
/** This class manages temporary files like model files, files with
* predicted labels, and so on.
* The methods of this class can handle given strings that shall
* be part of the name of the temporary files and automatically
* increments a counter that is also part of these file names.
* Methods for using base files or renaming are also included.
* The user can choose, whether all temporary files are to be kept
* or to be deleted as soon as they are no longer needed or as soon
* as the YALE has completed the whole experiment.
*
* Parameters read from the experiment configuration file:
* <ul>
* <li><code>temp_dir</code>: Name of the directory for the
* temporary files.</li>
* <li><code>keep_temp_files</code>: Either <code>none</code>
* or <code>all</code> indicating whether no temporary
* or all temporary files should be kept when they are
* no longer needed by YALE.</li>
* </ul>
* <br>
*
* @author Ingo Mierswa
* @version $Id: TempFileService.java,v 2.12 2004/08/27 11:57:45 ingomierswa Exp $
*/
public class TempFileService {
public static final String[] DELETE_TEMP_FILES = { "no", "directly", "on_exit" };
public static final int NO = 0;
public static final int DIRECTLY = 1; // ?? comments on semantics and procedure ??
public static final int ON_EXIT = 2;
/** Indicates wether or not deleteOnExit() should be invoked on all created temporary files.
* One out of NO, DIRECTLY, and ON_EXIT. */
private static int deleteTempFiles;
/** Prefix of all files. The value is "_yale_". */
private static final String TMP_PREFIX="_yale_";
/** Directory of all temp files. */
private static File tmpDir;
/** Directory of all temp files. */
private static String tmpDirString;
/** Initializes the temp file service. */
public static void init(Experiment experiment) throws OperatorException {
TempFileService.deleteTempFiles = experiment.getRootOperator().getParameterAsInt("delete_temp_files");
String tempDirName = experiment.getRootOperator().getParameterAsString("temp_dir");
LogService.logMessage("TempFileService: delete_temp_files = "+
DELETE_TEMP_FILES[TempFileService.deleteTempFiles],
LogService.MINIMUM);
if (tempDirName == null) {
File experimentFile = experiment.getExperimentFile();
if (experimentFile == null) {
tempDirName = "unnamed.tmp";
} else {
tempDirName = experimentFile.getName()+".tmp";
}
}
TempFileService.tmpDir = experiment.resolveFileName(tempDirName);
if (!tmpDir.exists()) {
LogService.logMessage("Creating temp directory "+tmpDir, LogService.INIT);
if (!Tools.mkdir(tmpDir.getAbsoluteFile())) {
throw new OperatorException("Cannot create temp directory "+tmpDir);
} else {
if (deleteTempFiles > NO)
tmpDir.deleteOnExit();
}
}
}
/** Creates a new temp file with a name build of prefix and the current file index. */
public static File createTempFile() {
try {
File file = File.createTempFile(TMP_PREFIX, null, tmpDir);
return file;
} catch (IOException e) {
throw new RuntimeException(e.toString() + ": Cannot create temp file.");
}
}
/** Like <code>createTempFile</code>, additionally the given name is part of the file name. */
public static File createTempFile(String name) {
try {
File file = File.createTempFile(TMP_PREFIX+name, null, tmpDir);
return file;
} catch (IOException e) {
throw new RuntimeException(e.toString() + ": Cannot create temp file.");
}
}
/** Gets a file and renames it to prefix + current file index. */
public static File createTempFile(File oldfile) {
return createTempFile("", oldfile);
}
/** Like <code>createTempFile(File)</code>, additionally the given name is part of the file name.
*/
public static File createTempFile(String name, File oldfile) {
try {
File newfile = File.createTempFile(TMP_PREFIX+name, null, tmpDir);
if (!oldfile.exists()) {
throw new RuntimeException("TempFileService: Cannot rename file '"+oldfile.getPath()+
". File does not exist.");
}
// neccessary for Windows OS
if (newfile.exists()) newfile.delete();
if (oldfile.renameTo(newfile)) {
LogService.logMessage("TempFileService: Renamed file '" + oldfile.getPath() +
"' to '"+newfile.getPath()+"'.",
LogService.MINIMUM);
} else {
throw new RuntimeException("TempFileService: Cannot rename '"+oldfile.getPath()+
"' to '"+newfile.getPath()+"'! Original file exists, "+
"but cannot be renamed.");
}
if (deleteTempFiles > NO) newfile.deleteOnExit();
return newfile;
} catch (IOException e) {
throw new RuntimeException(e.toString() + ": Cannot create temp file.");
}
}
/** Fills the given file array with files of a created filestem (which will be returned),
* and each of the extensions. */
public static String createTempFiles(String name, String[] extensions, File[] files) {
try {
File tempFile = File.createTempFile(TMP_PREFIX + name, "", tmpDir);
for (int i = 0; i < files.length; i++) {
files[i] = new File(tempFile.getAbsolutePath() + extensions[i]);
if (deleteTempFiles > NO) files[i].deleteOnExit();
}
return tempFile.getAbsolutePath();
} catch (IOException e) {
throw new RuntimeException(e.toString() + ": Cannot create temp file.");
}
}
/** Deletes the file if delete_temp_files is set to DIRECTLY. */
public static void deleteTempFile(File file) {
switch (deleteTempFiles) {
case DIRECTLY:
if (file.exists()) {
LogService.logMessage("Deleting temporary file '"+file.getName()+"' ("+file.length()+" bytes)",
LogService.MINIMUM);
file.delete();
}
return;
case ON_EXIT:
file.deleteOnExit();
return;
}
}
/** Returns the temp dir. */
public static File getTempDir() {
return tmpDir;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -