asmuts 02/01/14 22:46:47
Added: src/java/org/apache/stratum/jcs/utils/file FileUtil.java
GetFile.java
Log:
Revision Changes Path
1.1
jakarta-turbine-stratum/src/java/org/apache/stratum/jcs/utils/file/FileUtil.java
Index: FileUtil.java
===================================================================
package org.apache.stratum.jcs.utils.file;
import java.util.*;
import java.io.*;
import java.text.*;
////////////////////////////////////////////////////////////////////////
public class FileUtil {
private FileUtil() {
}
public static String getTextFile( String textSourceFile ) {
StringBuffer TextBuffer = new StringBuffer();
String line = null;
try {
BufferedReader in = new BufferedReader( new FileReader( new File(
textSourceFile ) ) );
while ( ( line = in.readLine() ) != null ) {
TextBuffer.append( line ).append( "\n" );
}
in.close();
} catch ( IOException ioe ) {
TextBuffer.append( ioe.toString() );
}
return TextBuffer.toString();
} // end getTextFile
public static void main( String[] args ) {
String filename = "c:/O_01_115451234_00000090_20010204.EXT";
int num = 1000;
String f = null;
GetFile gf = new GetFile();
long start = System.currentTimeMillis();
for ( int i = 0; i < num; i++ ) {
f = gf.file2String( filename );
}
System.out.println( f );
System.out.println( "old took " + (System.currentTimeMillis() - start) + " ms."
);
start = System.currentTimeMillis();
for ( int i = 0; i < num; i++ ) {
f = getTextFile( filename );
}
System.out.println( f );
System.out.println( "new took " + (System.currentTimeMillis() - start) + " ms."
);
}
/** Deletes the given file. */
public static void deleteFile (File file) {
if (file == null || !file.exists()) {
return;
}
if (!file.isFile()) {
IllegalArgumentException ex = new IllegalArgumentException("file " +
file.getAbsolutePath() + " must be a file.");
throw ex;
}
int i = 0;
for (; i < 10 && !file.delete(); i++) {
//log.warn("Failed to delete file " + file.getAbsolutePath() + "...retrying "+
i);
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException ignore) {}
}
if (i == 10) {
IllegalStateException ex = new IllegalStateException("Failed to delete file "
+ file.getAbsolutePath());
throw ex;
}
return;
}
/** Renames the from-file to the to-file. */
public static void renameFile (File from, File to) {
if (from == null || to == null || !from.exists() || !from.isFile()) {
IllegalArgumentException ex = new IllegalArgumentException("From and to file
must not be null.");
throw ex;
}
if (!from.exists() || !from.isFile()) {
IllegalArgumentException ex = new IllegalArgumentException("From file does not
exist or not a file.");
throw ex;
}
deleteFile(to);
int i = 0;
for (; i < 10 && !from.renameTo(to); i++) {
//log.warn("Failed to rename file from " + from.getAbsolutePath() + "to " +
to.getAbsolutePath() + "...retrying " + i);
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException ignore) {}
}
if (i == 10) {
IllegalStateException ex = new IllegalStateException("Failed to rename file
from "
+ from.getAbsolutePath() + "to " + to.getAbsolutePath());
throw ex;
}
return;
}
} // end class Utilities
1.1
jakarta-turbine-stratum/src/java/org/apache/stratum/jcs/utils/file/GetFile.java
Index: GetFile.java
===================================================================
package org.apache.stratum.jcs.utils.file;
import java.util.*;
import java.io.*;
import java.text.*;
public class GetFile {
String fullName;
public GetFile(){}
public GetFile( String fullName ){
this.fullName = fullName;
}
public boolean exists(){
File file = new File( fullName );
return file.exists();
}
public String file2String() {
StringBuffer TextBuffer = new StringBuffer();
String desc;
char[] chars = null;
int c;
FileReader in;
try {
File inputFile = new File( fullName );
in = new FileReader( inputFile );
while ( ( c = in.read() ) != -1 )
TextBuffer.append( (char) c );
in.close();
} catch ( IOException ioe ) {
TextBuffer.append( ioe.toString() );
}
return TextBuffer.toString();
} // end file2String
public String file2String( String textSourceFile ) {
StringBuffer TextBuffer = new StringBuffer();
String desc;
char[] chars = null;
int c;
FileReader in;
try {
File inputFile = new File( textSourceFile );
in = new FileReader( inputFile );
while ( ( c = in.read() ) != -1 )
TextBuffer.append( (char) c );
in.close();
} catch ( IOException ioe ) {
TextBuffer.append( ioe.toString() );
}
return TextBuffer.toString();
} // end file2String
} // end class GetFile
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>