mirror of
https://github.com/SWG-Source/dsrc.git
synced 2026-01-17 00:05:07 -05:00
89 lines
2.4 KiB
Java
Executable File
89 lines
2.4 KiB
Java
Executable File
package script.developer;
|
|
|
|
import script.obj_id;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileWriter;
|
|
|
|
public class file_access extends script.base_script
|
|
{
|
|
public file_access()
|
|
{
|
|
}
|
|
public static String readTextFile(String fileName) throws InterruptedException
|
|
{
|
|
String result = null;
|
|
File f = new File(fileName);
|
|
if (f.exists())
|
|
{
|
|
long len = f.length();
|
|
if (len > 0)
|
|
{
|
|
try
|
|
{
|
|
FileInputStream inputStream = new FileInputStream(f);
|
|
byte[] buf = new byte[(int)len];
|
|
inputStream.read(buf);
|
|
result = new String(buf);
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
obj_id self = getSelf();
|
|
sendSystemMessageTestingOnly(self, "An exception occurred while trying to read " + fileName);
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
public static boolean isWritable(String fileName) throws InterruptedException
|
|
{
|
|
boolean result = false;
|
|
File f = new File(fileName);
|
|
if (f.exists())
|
|
{
|
|
if (f.canWrite())
|
|
{
|
|
result = true;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
public static boolean writeTextFile(String fileName, String fileContents) throws InterruptedException
|
|
{
|
|
boolean result = false;
|
|
File f = new File(fileName);
|
|
if (!f.exists())
|
|
{
|
|
try
|
|
{
|
|
f.createNewFile();
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
if (f.canWrite())
|
|
{
|
|
f.delete();
|
|
try
|
|
{
|
|
if (f.createNewFile())
|
|
{
|
|
FileWriter writer = new FileWriter(f);
|
|
writer.write(fileContents);
|
|
writer.close();
|
|
result = true;
|
|
}
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
obj_id self = getSelf();
|
|
sendSystemMessageTestingOnly(self, "failed to write " + fileName + " : " + e);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
}
|