mirror of
https://github.com/SWG-Source/dsrc.git
synced 2026-07-13 22:01:04 -04:00
85 lines
1.4 KiB
Plaintext
85 lines
1.4 KiB
Plaintext
include java.io.File;
|
|
include java.io.FileInputStream;
|
|
include java.io.InputStream;
|
|
include java.io.FileWriter;
|
|
|
|
String readTextFile(String fileName)
|
|
{
|
|
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;
|
|
}
|
|
|
|
boolean isWritable(String fileName)
|
|
{
|
|
boolean result = false;
|
|
File f = new File(fileName);
|
|
if(f.exists())
|
|
{
|
|
if(f.canWrite())
|
|
{
|
|
result = true;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
boolean writeTextFile(String fileName, String fileContents)
|
|
{
|
|
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;
|
|
}
|