jtfile.java
来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 684 行 · 第 1/2 页
JAVA
684 行
package Jt;
import java.util.*;
import java.io.*;
/**
* Handles files
*/
public class JtFile extends JtObject {
private static final long serialVersionUID = 1L;
public static final String JtCLASS_NAME = JtFile.class.getName();
public static final String JtWRITE = "JtWRITE";
public static final String JtOPEN = "JtOPEN";
public static final String JtCLOSE = "JtCLOSE";
public static final String JtDELETE = "JtDELETE";
public static final String JtSAVE_STRING = "JtSAVE";
public static final String JtCONVERT_TO_STRING = "JtCONVERT_TO_STRING";
public static final String JtREAD_LINES = "JtREAD_LINES";
public static final String JtCOPY = "JtCOPY";
public static final String JtRENAME = "JtRENAME";
String name;
FileOutputStream ostream = null;
boolean createdir = true;
boolean recursive = false;
Vector filelist = null;
String filter = null;
byte[] buffer = null;
public final static int BUFFER_SIZE = 1024; // Buffer size (read_file)
public final static int MAX_LENGTH = 1024 * 3000; // Max length(read_file)
int buffersize = BUFFER_SIZE;
public JtFile() {
}
/**
* Specifies the name of the file or directory
* @param newName file name
*/
public void setName (String newName) {
name = newName;
}
/**
* Returns the name of the file or directory.
*/
public String getName () {
return (name);
}
/**
* Sets the createDir flag which determines if necessary but nonexisting parent directories
* should be created.
* @param newCreatedir create nonexisting parent directories
*/
public void setCreatedir (boolean newCreatedir) {
createdir = newCreatedir;
}
/**
* Returns the createDir flag which determines if necessary but nonexisting parent directories
* should be created.
*/
public boolean getCreatedir () {
return (createdir);
}
// File list (output)
/**
* This attribute is being deprecated.
*/
public Vector getFilelist () {
return (filelist);
}
/**
* Sets the filter flag which determines what file extension should be considered.
*/
public void setFilter (String filter) {
this.filter = filter;
}
/**
* Returns the filter flag which determines what file extension should be considered.
*/
public String getFilter () {
return (filter);
}
/**
* Set the recursive flag which determines if the JtCLEANUP message should recursively
* remove files under the subdirectories.
* @param recursive
*
*/
public void setRecursive (boolean recursive) {
this.recursive = recursive;
}
/**
* Returns the recursive flag which determines if the JtCLEANUP message should recursively
* remove files under the subdirectories.
*
*/
public boolean getRecursive () {
return (recursive);
}
// open operation
void open () {
if (name == null) {
handleError ("Attribute name needs to be set.");
return;
}
try {
ostream = new FileOutputStream (name);
} catch (Exception e) {
handleException (e);
}
}
// Save
private void save (String content) {
if (content == null)
return;
if (ostream == null)
open ();
if (ostream == null)
return;
try {
ostream.write(content.getBytes());
} catch (Exception ex) {
handleException (ex);
}
close ();
}
/*
public byte [] getBuffer ()
{
return (buffer);
}
*/
private void copyFile (String targetPath) {
FileInputStream fis = null;
FileOutputStream fos = null;
File srcFile;
File targetFile;
String nameFile;
if (name == null) {
handleError ("Attribute name needs to be set.");
}
srcFile = new File (name);
if (srcFile.isDirectory()) {
handleError ("Source file cannot be a directory:" + name);
return;
}
if (targetPath == null) {
handleError ("Invalid targetPath: null.");
return;
}
targetFile = new File (targetPath);
if (targetFile.isDirectory()) {
nameFile = srcFile.getName();
targetFile = new File (targetPath, nameFile);
}
try {
byte[] buf = new byte[1024];
fis = new FileInputStream(name);
fos = new FileOutputStream(targetFile);
int i = 0;
while ((i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}
}
catch (Exception e) {
handleException (e);
}
finally {
try {
if (fis != null) fis.close();
if (fos != null) fos.close();
} catch (Exception ex) {
handleException (ex);
}
}
}
// read_file: read the whole file into memory (good only for small files)
private Object read_file ()
{
FileInputStream istream;
File file;
int len, offset = 0, i;
long file_size;
byte buf [] = new byte [buffersize];
if (name == null)
return (null);
file = new File (name);
if (!file.exists ())
{
handleError ("JtFile.readfile: file does not exist:" + name);
buffer = null;
return (null); // check
}
if (file.length () == 0L)
{
handleTrace ("JtFile.readfile: empty file" + name);
buffer = null;
return (null);
}
// This is to avoid memory problems while handling big files.
// Huge files should be split.
file_size = file.length ();
if (file_size > MAX_LENGTH)
{
handleError
("JtFile.readfile: file exceeded maximum length allowed:" +
name + ":" + file_size);
return (null);
}
buffer = new byte[(int) file_size];
try {
istream = new FileInputStream (name);
offset = 0;
while ((len = istream.read (buf, 0, buffersize)) > 0)
{
for (i = 0; i < len; i++)
{
if (offset + i >= file_size)
{
handleError ("JtFile.readfile: file size mismatch" +
name);
buffer = null;
istream.close ();
return (null);
}
buffer[offset + i] = buf[i];
}
offset += len;
}
if (offset != file_size)
handleWarning ("JtFile.readfile: file size mismatch" +
name); // check
istream.close ();
} catch (Exception e) {
buffer = null;
handleException (e);
}
if (buffer == null)
return (null);
/*
handleTrace ("read_file:");
for (i = 0; i < offset; i++)
System.out.print (new Character ((char) buffer[i]));
*/
return (this);
}
// write operation
void write (byte buffer[], int len) {
if (ostream == null)
return;
try {
ostream.write (buffer, 0, len);
} catch (Exception e) {
handleException (e);
}
}
// Destroy operation
void destroy () {
if (ostream != null)
close ();
}
// close operation
void close () {
if (ostream == null)
return;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?