bcv-vf/src/me/konloch/kontainer/io/DiskWriter.java
Kalen Kinloch 4e6647be19 2.3.0 Release
12/16/2014 - Started updating the class viewer.
12/18/2014 - Finished a basic concept of the new class viewer.
12/18/2014 - Fixed an error with importing some jars.
12/18/2014 - Fixed the about window.
12/18/2014 - Finished the final concept for the new class viewer.
12/18/2014 - Threaded save Java files as zip, it now runs in a
background thread.
12/18/2014 - Save Java files as zip now prompts you to select a
decompiler.
12/18/2014 - Removed the cursor waiting for save Java files as zip.
12/18/2014 - Wrapped the save Java files as zip around an exception
handler, it will now safely show the exception if any is thrown.
12/18/2014 - Fixed not escaping the Java strings by default for the
Bytecode decompiler. - http://i.imgur.com/YrRnZA7.png
12/18/2014 - Used Eclipse's code formatting tool and formatted the code
12/19/2014 - Priav03 fixed the quick class searcher.
2014-12-19 13:18:51 -08:00

200 lines
No EOL
4.6 KiB
Java

package me.konloch.kontainer.io;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
/**
* This method will save to disk
*
* @author Konloch
*
*/
public class DiskWriter {
/**
* Used to insert a difference string with preserving the file extension
*
* @param fileName
* The file name
* @param difference
* Normally an integer
* @return The filename with the difference inserted and the file extension
* preserved
*/
public static String insertFileName(String fileName, String difference) {
String[] babe = fileName.split("\\.");
int count = 0;
int math = babe.length;
String m = "";
for (String s2 : babe) {
m += s2;
if (math - 2 == count)
m += difference + ".";
else if (math - 1 != count)
m += ".";
count++;
}
return m;
}
/**
* Writes a new line to the file, if it doesn't exist it will automatically
* create it.
*
* @param filename
* @param fileContents
* @param debug
*/
public static synchronized void writeNewLine(String filename,
byte[] fileContents, boolean debug) {
PrintWriter writer = null;
String original = filename;
int counter = 0;
boolean saved = false;
while (!saved) {
try {
writer = new PrintWriter(new BufferedWriter(new FileWriter(
filename, true)));
writer.println(fileContents);
if (debug)
System.out.println("Saved " + filename + " to disk");
saved = true;
} catch (Exception e) {
if (debug)
System.out.println("Failed saving, trying to save as "
+ filename);
if (original.contains(".")) {
filename = insertFileName(original, "" + counter);
} else
filename = original + counter;
counter++;
}
}
writer.close();
}
/**
* Writes a string to the file
*
* @param filename
* @param lineToWrite
* @param debug
*/
public static synchronized void writeNewLine(String filename,
String lineToWrite, boolean debug) {
PrintWriter writer = null;
String original = filename;
int counter = 0;
boolean saved = false;
while (!saved) {
try {
writer = new PrintWriter(new BufferedWriter(new FileWriter(
filename, true)));
writer.println(lineToWrite);
if (debug)
System.out.println("Saved " + filename + ">" + lineToWrite
+ " to disk");
saved = true;
} catch (Exception e) {
if (debug)
System.out.println("Failed saving, trying to save as "
+ filename);
if (original.contains(".")) {
filename = insertFileName(original, "" + counter);
} else
filename = original + counter;
counter++;
}
}
writer.close();
}
/**
* Deletes the original file if it exists, then writes the fileContents[] to
* the file.
*
* @param filename
* @param fileContents
* @param debug
*/
public static synchronized void replaceFile(String filename,
byte[] fileContents, boolean debug) {
File f = new File(filename);
if (f.exists())
f.delete();
PrintWriter writer = null;
String original = filename;
int counter = 0;
boolean saved = false;
while (!saved) {
try {
writer = new PrintWriter(new BufferedWriter(new FileWriter(
filename, true)));
writer.println(fileContents);
if (debug)
System.out.println("Saved " + filename + " to disk");
saved = true;
} catch (Exception e) {
if (debug)
System.out.println("Failed saving, trying to save as "
+ filename);
if (original.contains(".")) {
filename = insertFileName(original, "" + counter);
} else
filename = original + counter;
counter++;
}
}
writer.close();
}
/**
* Deletes the original file if it exists, then writes the lineToWrite to
* the file.
*
* @param filename
* @param lineToWrite
* @param debug
*/
public static synchronized void replaceFile(String filename,
String lineToWrite, boolean debug) {
File f = new File(filename);
if (f.exists())
f.delete();
PrintWriter writer = null;
String original = filename;
int counter = 0;
boolean saved = false;
while (!saved) {
try {
writer = new PrintWriter(new BufferedWriter(new FileWriter(
filename, true)));
writer.println(lineToWrite);
if (debug)
System.out.println("Saved " + filename + ">" + lineToWrite
+ " to disk");
saved = true;
} catch (Exception e) {
if (debug)
System.out.println("Failed saving, trying to save as "
+ filename + "_");
if (original.contains(".")) {
filename = insertFileName(original, "" + counter);
} else
filename = original + counter;
counter++;
}
}
writer.close();
}
}