import weka.core.Utils; //导入方法依赖的package包/类
/**
* Resets the Loader object and sets the source of the data set to be the
* supplied File object.
*
* @param file the source file.
* @throws IOException if an error occurs
*/
@Override
public void setSource(File file) throws IOException {
File original = file;
m_structure = null;
setRetrieval(NONE);
if (file == null) {
throw new IOException("Source file object is null!");
}
// try {
String fName = file.getPath();
try {
if (m_env == null) {
m_env = Environment.getSystemWide();
}
fName = m_env.substitute(fName);
} catch (Exception e) {
// ignore any missing environment variables at this time
// as it is possible that these may be set by the time
// the actual file is processed
// throw new IOException(e.getMessage());
}
file = new File(fName);
// set the source only if the file exists
if (file.exists() && file.isFile()) {
if (file.getName().endsWith(
getFileExtension() + FILE_EXTENSION_COMPRESSED)) {
setSource(new GZIPInputStream(new FileInputStream(file)));
} else {
setSource(new FileInputStream(file));
}
} else {
// System.out.println("Looking in classpath....");
// look for it as a resource in the classpath
// forward slashes are platform independent for loading from the
// classpath...
String fnameWithCorrectSeparators = fName
.replace(File.separatorChar, '/');
if (this.getClass().getClassLoader()
.getResource(fnameWithCorrectSeparators) != null) {
// System.out.println("Found resource in classpath...");
setSource(this.getClass().getClassLoader()
.getResourceAsStream(fnameWithCorrectSeparators));
}
}
// }
/*
* catch (FileNotFoundException ex) { throw new
* IOException("File not found"); }
*/
if (m_useRelativePath) {
try {
m_sourceFile = Utils.convertToRelativePath(original);
m_File = m_sourceFile.getPath();
} catch (Exception ex) {
// System.err.println("[AbstractFileLoader] can't convert path to relative path.");
m_sourceFile = original;
m_File = m_sourceFile.getPath();
}
} else {
m_sourceFile = original;
m_File = m_sourceFile.getPath();
}
}