Added old project initial commit
All checks were successful
Locusworks Team/lsproject/pipeline/head This commit looks good
All checks were successful
Locusworks Team/lsproject/pipeline/head This commit looks good
This commit is contained in:
72
src/main/java/net/locusworks/lsproject/util/Utility.java
Normal file
72
src/main/java/net/locusworks/lsproject/util/Utility.java
Normal file
@@ -0,0 +1,72 @@
|
||||
package net.locusworks.lsproject.util;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import net.locusworks.lsproject.object.Network;
|
||||
|
||||
|
||||
public final class Utility {
|
||||
public static final String FILE_EXTENSION = ".lsa";
|
||||
|
||||
/**
|
||||
* Save a network to disk. It uses it's name + the file extension FILE_EXTENSION.
|
||||
* @param network Network to save.
|
||||
* @throws IOException Thrown if an IO error occurs during write.
|
||||
*/
|
||||
public static void saveNetwork() throws IOException {
|
||||
FileOutputStream out = new FileOutputStream(Network.getInstance().getName() + FILE_EXTENSION);
|
||||
ObjectOutputStream objectOut = new ObjectOutputStream(out);
|
||||
|
||||
objectOut.writeObject(Network.getInstance());
|
||||
|
||||
objectOut.flush();
|
||||
objectOut.close();
|
||||
out.close();
|
||||
|
||||
System.out.println("Wrote network " + Network.getInstance().getName() + " to file.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Load an existing network object file from disk.
|
||||
* @param networkFile File to load.
|
||||
* @return Network object.
|
||||
* @throws IOException Thrown if an IO error occurs during read.
|
||||
*/
|
||||
public static void loadNetwork(File networkFile) throws IOException {
|
||||
if (!networkFile.getName().endsWith(FILE_EXTENSION)) {
|
||||
throw new IOException("Invalid network object file.");
|
||||
}
|
||||
|
||||
FileInputStream in = new FileInputStream(networkFile);
|
||||
ObjectInputStream objectIn = new ObjectInputStream(in);
|
||||
|
||||
try {
|
||||
Network.createInstance((Network)objectIn.readObject());
|
||||
}
|
||||
catch (ClassNotFoundException e) {
|
||||
throw new IOException("Target file is corrupted.");
|
||||
}
|
||||
finally {
|
||||
objectIn.close();
|
||||
in.close();
|
||||
}
|
||||
|
||||
System.out.println("Loaded network " + Network.getInstance().getName() + ".");
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility function for getting a file's extension.
|
||||
* @param f File to look at.
|
||||
* @return Extension of the file.
|
||||
*/
|
||||
public static String getExtension(File f) {
|
||||
String ext = null;
|
||||
String s = f.getName();
|
||||
int i = s.lastIndexOf('.');
|
||||
|
||||
if (i > 0 && i < s.length() - 1) {
|
||||
ext = s.substring(i+1).toLowerCase();
|
||||
}
|
||||
return ext;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user