jtfile.java
来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 684 行 · 第 1/2 页
JAVA
684 行
try {
ostream.close ();
} catch (Exception e) {
handleException (e);
}
ostream = null; // check
}
void createParentDir (String name) {
File file;
String parentpath;
if (name == null)
return;
file = new File (name);
if (file == null) {
handleError ("JtFile: unable to create File object:" + name);
return;
}
parentpath = file.getParent ();
if (parentpath == null)
return;
//file = file.getParentFile ();
file = new File (parentpath);
if (file == null) {
handleError ("JtFile: unable to get parent File:" + name);
return;
}
if (!file.exists ())
if (!file.mkdirs ())
handleError ("JtFile: unable to create directories:" +
parentpath);
}
// Read_lines: read file line by line
private void read_lines (JtMessage ev) {
String line;
JtMessage e;
if (name == null)
return;
try {
BufferedReader d = new BufferedReader
(new FileReader (name));
while ((line = d.readLine ()) != null) {
//System.out.println ("JtFile.read_lines:" + line);
e = new JtMessage ();
e.setMsgId("JtMESSAGE");
e.setMsgSubject (ev.getMsgId ());
e.setMsgContent (line);
if (ev.getMsgReplyTo () != null)
this.sendMessage (ev.getMsgReplyTo (), e);
}
} catch (Exception ex) {
handleException (ex);
}
}
// rename a file
private boolean renameFile (String newName) {
File newFile;
File file;
if (name == null) {
handleError ("Attribute name needs to be set.");
return false;
}
if (newName == null)
return (false);
newFile = new File (newName);
file = new File (name);
return (file.renameTo(newFile));
}
/**
* Process object messages.
* <ul>
* <li> JtOPEN - Opens an output file. For all the operations the attribute 'name' specifies the file name.
* <li> JtSAVE_STRING - Writes the String specified by msgContent to the file. Opens the file if it
* hasn't been opened. Closes the file after saving the string.
* <li> JtCLOSE - Closes the file.
* <li> JtREAD_LINES - Reads input lines from the file, one line at a time.
* Each line is sent to the object specified by msgReplyTo (JtMessage object).
* <li> JtDELETE - Deletes the file or directory.
* <li> JtCONVERT_TO_STRING - Converts the content of the file to String.
* <li> JtCOPY - Makes a copy of the file. msgContent specifies the name of the destination file or
* <li> directory where the file should be copied.
* <li> JtRENAME - Renames the file. msgContent specifies the new name.
* </ul>
*/
public Object processMessage (Object message) {
String msgid = null;
byte buffer[];
JtBuffer buf;
File file;
JtMessage e = (JtMessage) message;
if (e == null)
return null;
msgid = (String) e.getMsgId ();
if (msgid == null)
return null;
// Remove this object
if (msgid.equals (JtObject.JtREMOVE)) {
return (null);
}
if (msgid.equals (JtFile.JtOPEN)) {
// Create the parent directories if needed
if (name == null)
return null;
if (createdir) {
createParentDir (name);
}
open ();
return null;
}
if (msgid.equals (JtFile.JtSAVE_STRING)) {
if (createdir) {
createParentDir (name);
}
save ((String) e.getMsgContent ());
return null;
}
if (msgid.equals (JtFile.JtCLOSE)) {
close ();
return null;
}
if (msgid.equals ("JtREAD_FILE")) { //check
return (read_file ());
}
if (msgid.equals (JtFile.JtRENAME)) {
return (new Boolean (renameFile ((String) e.getMsgContent ())));
}
if (msgid.equals (JtFile.JtCONVERT_TO_STRING)) {
read_file ();
if (this.buffer == null)
return (null);
return (new String (this.buffer));
}
if (msgid.equals (JtFile.JtREAD_LINES)) {
read_lines (e);
return (null);
}
/*
if (msgid.equals ("JtWRITE")) {
if (e.getMsgContent () == null)
return null;
buf = (RtBuffer) e.getMsgContent ();
buffer = buf.getBuffer ();
handleTrace ("RtFile: writing " + buffer.length + " bytes");
write (buffer, buffer.length);
return null;
}
*/
if (msgid.equals ("JtDATA_BUFFER")) {
if (e.getMsgContent () == null)
return null;
buf = (JtBuffer) e.getMsgContent ();
buffer = buf.getBuffer ();
handleTrace ("JtFile: writing " + buffer.length + " bytes");
write (buffer, buffer.length);
return null;
}
if (msgid.equals ("JtWRITEBYTE")) {
if (e.getMsgContent () == null)
return null;
buffer = (byte[]) e.getMsgContent ();
// buffer = buf.getBuffer ();
handleTrace ("JtFile: writing " + buffer.length + " bytes");
write (buffer, buffer.length);
return null;
}
if (msgid.equals (JtFile.JtDELETE)) {
if (name == null) {
handleError ("JtFile: Invalid attribute (name):"
+ name);
return null;
}
file = new File (name);
file.delete ();
return null;
}
if (msgid.equals (JtFile.JtCOPY)) {
copyFile ((String) e.getMsgContent());
return (null);
}
return (super.processMessage (message));
//handleError ("JtFile.processMessage: invalid message id:" + msgid);
//return (null);
}
/**
* Demonstrates the messages processed by JtFile.
*/
public static void main(String[] args) {
JtFactory main = new JtFactory ();
//JtMessage msg;
File tmp;
JtFile file;
JtMessage msg = new JtMessage (JtFile.JtSAVE_STRING);
// Create a JtFile object
file = (JtFile) main.createObject (JtFile.JtCLASS_NAME, "test");
main.setValue ("test", "name", "test.txt");
main.setValue ("test", "createdir", "true");
msg.setMsgContent ("Test");
// JtSAVE
main.sendMessage(file, msg);
msg.setMsgId(JtFile.JtCOPY);
msg.setMsgContent("/tmp/kk.txt");
main.sendMessage(file, msg);
// Create a JtFile object
file = (JtFile) main.createObject (JtFile.JtCLASS_NAME, "file");
main.setValue ("file", "name", "JtFile");
main.setValue ("file", "createdir", "true");
main.createObject (JtMessage.JtCLASS_NAME, "message");
//main.setValue ("message", "msgId", "JtDELETE");
//main.sendMessage ("file", "message");
// JtOPEN
main.setValue ("message", "msgId", JtFile.JtOPEN);
tmp = new File ("JtFile");
main.sendMessage ("file", "message");
main.setValue ("message", "msgId", JtFile.JtCLOSE);
main.sendMessage ("file", "message");
if (!tmp.exists ())
System.err.println ("JtFile(JtOPEN): FAILED");
else
System.err.println ("JtFile(JtOPEN): GO");
// JtDELETE
main.setValue ("message", "msgId", JtFile.JtDELETE);
main.sendMessage ("file", "message");
tmp = new File ("JtFile");
if (!tmp.exists ())
System.err.println ("JtFile(JtDELETE): GO");
else {
System.err.println ("JtFile(JtDELETE): FAILED");
}
// createdir attribute
main.setValue ("file", "name", "/tmp/JtFile/JtFile");
main.setValue ("file", "createdir", "true");
main.setValue ("message", "msgId", JtFile.JtOPEN);
main.sendMessage ("file", "message");
main.setValue ("message", "msgId", JtFile.JtCLOSE);
main.sendMessage ("file", "message");
tmp = new File ("/tmp/JtFile/JtFile");
if (!tmp.exists ())
main.handleError ("JtFile(JtOPEN/createdir=true): FAILED");
else
System.err.println ("JtFile(JtOPEN/createdir=true): GO");
main.setValue ("message", "msgId", JtFile.JtDELETE);
main.sendMessage ("file", "message");
if (!tmp.exists ())
System.err.println ("JtFile(JtDELETE): GO");
else
main.handleError ("JtFile(JtDELETE): FAILED");
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?