textfile.java
来自「FuncPlotter is a combined Java applicati」· Java 代码 · 共 1,009 行 · 第 1/2 页
JAVA
1,009 行
{ char ch = text.charAt( inIndex++ ); if ( ch == '\r' ) { if ( (inIndex < endIndex) && (text.charAt( inIndex ) == '\n') ) ++inIndex; ch = '\n'; } text.setCharAt( outIndex++, ch ); } } // Set length of text text.setLength( outIndex ); // Return line separator counts return lineSeparatorCounts; } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods//////////////////////////////////////////////////////////////////////// public StringBuilder read( ) throws AppException { return (StringBuilder)read( false ); } //------------------------------------------------------------------ public StringBuffer readSync( ) throws AppException { return (StringBuffer)read( true ); } //------------------------------------------------------------------ public String[] readLines( ) throws AppException { return readLines( 0 ); } //------------------------------------------------------------------ public String[] readLines( int maxNumLines ) throws AppException { List<String> lines = new ArrayList<String>( ); BufferedReader inStream = null; try { // Open input stream on file FileChannel fileChannel = null; try { FileInputStream fileInStream = new FileInputStream( file ); fileChannel = fileInStream.getChannel( ); inStream = new BufferedReader( new InputStreamReader( fileInStream, charsetName ) ); } catch ( FileNotFoundException e ) { throw new FileException( ErrorId.FAILED_TO_OPEN_FILE, file, e ); } catch ( SecurityException e ) { throw new FileException( ErrorId.FILE_ACCESS_NOT_PERMITTED, file, e ); } catch ( UnsupportedEncodingException e ) { String[] substStrs = { charsetName }; throw new AppException( ErrorId.ENCODING_NOT_SUPPORTED, substStrs ); } // Lock file try { if ( fileChannel.tryLock( 0, Long.MAX_VALUE, true ) == null ) throw new FileException( ErrorId.FAILED_TO_LOCK_FILE, file ); } catch ( Exception e ) { throw new FileException( ErrorId.FAILED_TO_LOCK_FILE, file, e ); } // Read file while ( (maxNumLines == 0) || (lines.size( ) < maxNumLines) ) { try { try { // Test whether operation has been terminated by a monitor for ( ProgressListener listener : progressListeners ) { if ( listener.isOperationTerminated( ) ) throw new TerminatedException( ); } // Read line from input stream String line = inStream.readLine( ); if ( line == null ) break; lines.add( line ); // Notify monitors of progress for ( ProgressListener listener : progressListeners ) listener.progress( (maxNumLines == 0) ? -1.0 : (double)lines.size( ) / (double)maxNumLines ); } catch ( IOException e ) { throw new FileException( ErrorId.ERROR_READING_FILE, file, e ); } } catch ( OutOfMemoryError e ) { throw new FileException( ErrorId.NOT_ENOUGH_MEMORY_TO_READ_FILE, file ); } } // Close input stream try { inStream.close( ); inStream = null; } catch ( IOException e ) { throw new FileException( ErrorId.FAILED_TO_CLOSE_FILE, file, e ); } } catch ( AppException e ) { try { if ( inStream != null ) inStream.close( ); } catch ( IOException e1 ) { // ignore } throw e; } // Return lines of text return lines.toArray( new String[lines.size( )] ); } //------------------------------------------------------------------ public void write( CharSequence text ) throws AppException { File tempFile = null; OutputStreamWriter outStream = null; boolean oldFileDeleted = false; try { // Create temporary file try { tempFile = File.createTempFile( TEMP_FILE_PREFIX, null, file.getAbsoluteFile( ).getParentFile( ) ); } catch ( Exception e ) { throw new AppException( ErrorId.FAILED_TO_CREATE_TEMPORARY_FILE, e ); } // Open output stream on temporary file FileChannel fileChannel = null; try { FileOutputStream fileOutStream = new FileOutputStream( tempFile ); fileChannel = fileOutStream.getChannel( ); outStream = new OutputStreamWriter( fileOutStream, charsetName ); } catch ( FileNotFoundException e ) { throw new FileException( ErrorId.FAILED_TO_OPEN_FILE, tempFile, e ); } catch ( SecurityException e ) { throw new FileException( ErrorId.FILE_ACCESS_NOT_PERMITTED, tempFile, e ); } catch ( UnsupportedEncodingException e ) { String[] substStrs = { charsetName }; throw new AppException( ErrorId.ENCODING_NOT_SUPPORTED, substStrs ); } // Lock file try { if ( fileChannel.tryLock( ) == null ) throw new FileException( ErrorId.FAILED_TO_LOCK_FILE, tempFile ); } catch ( Exception e ) { throw new FileException( ErrorId.FAILED_TO_LOCK_FILE, tempFile, e ); } // Write file try { int textLength = text.length( ); int offset = 0; while ( offset < textLength ) { // Test whether operation has been terminated by a monitor for ( ProgressListener listener : progressListeners ) { if ( listener.isOperationTerminated( ) ) throw new TerminatedException( ); } // Write block of data to output stream int endOffset = Math.min( offset + BLOCK_LENGTH, textLength ); outStream.append( text, offset, endOffset ); offset = endOffset; // Notify monitor of progress for ( ProgressListener listener : progressListeners ) listener.progress( (double)offset / (double)textLength ); } } catch ( IOException e ) { throw new FileException( ErrorId.ERROR_WRITING_FILE, tempFile, e ); } // Close output stream try { outStream.close( ); outStream = null; } catch ( IOException e ) { throw new FileException( ErrorId.FAILED_TO_CLOSE_FILE, tempFile, e ); } // Delete any existing file try { if ( file.exists( ) && !file.delete( ) ) throw new FileException( ErrorId.FAILED_TO_DELETE_FILE, file ); oldFileDeleted = true; } catch ( SecurityException e ) { throw new FileException( ErrorId.FAILED_TO_DELETE_FILE, file, e ); } // Rename temporary file try { if ( !tempFile.renameTo( file ) ) throw new TempFileException( ErrorId.FAILED_TO_RENAME_FILE, file, tempFile ); } catch ( SecurityException e ) { throw new TempFileException( ErrorId.FAILED_TO_RENAME_FILE, file, e, tempFile ); } } catch ( AppException e ) { try { if ( outStream != null ) outStream.close( ); } catch ( Exception e1 ) { // ignore } try { if ( !oldFileDeleted && (tempFile != null) && tempFile.exists( ) ) tempFile.delete( ); } catch ( Exception e1 ) { // ignore } throw e; } } //------------------------------------------------------------------ public void addProgressListener( ProgressListener listener ) { progressListeners.add( listener ); } //------------------------------------------------------------------ public void removeProgressListener( ProgressListener listener ) { progressListeners.remove( listener ); } //------------------------------------------------------------------ public ProgressListener[] getProgressListeners( ) { return progressListeners.toArray( new ProgressListener[progressListeners.size( )] ); } //------------------------------------------------------------------ private Object read( boolean sync ) throws AppException { // Test file length if ( file.length( ) >= Integer.MAX_VALUE ) throw new FileException( ErrorId.FILE_IS_TOO_LONG, file ); int fileLength = (int)file.length( ); // Read file FileChannel fileChannel = null; InputStreamReader inStream = null; TextBuffer buffer = null; try { // Open input stream on file try { FileInputStream fileInStream = new FileInputStream( file ); fileChannel = fileInStream.getChannel( ); inStream = new InputStreamReader( fileInStream, charsetName ); } catch ( FileNotFoundException e ) { throw new FileException( ErrorId.FAILED_TO_OPEN_FILE, file, e ); } catch ( SecurityException e ) { throw new FileException( ErrorId.FILE_ACCESS_NOT_PERMITTED, file, e ); } catch ( UnsupportedEncodingException e ) { String[] substStrs = { charsetName }; throw new AppException( ErrorId.ENCODING_NOT_SUPPORTED, substStrs ); } // Lock file try { if ( fileChannel.tryLock( 0, Long.MAX_VALUE, true ) == null ) throw new FileException( ErrorId.FAILED_TO_LOCK_FILE, file ); } catch ( Exception e ) { throw new FileException( ErrorId.FAILED_TO_LOCK_FILE, file, e ); } // Read file try { // Allocate buffer for file, assuming that the text length in chars is not greater than the // file length in bytes buffer = new TextBuffer( sync, fileLength + 1 ); // Read file try { char[] readBuf = new char[BLOCK_LENGTH]; while ( true ) { // Test whether operation has been terminated by a monitor for ( ProgressListener listener : progressListeners ) { if ( listener.isOperationTerminated( ) ) throw new TerminatedException( ); } // Read block of data from input stream int readLength = inStream.read( readBuf ); if ( readLength <= 0 ) break; buffer.append( readBuf, readLength ); // Notify monitors of progress for ( ProgressListener listener : progressListeners ) listener.progress( (double)buffer.getLength( ) / (double)fileLength ); } } catch ( IOException e ) { throw new FileException( ErrorId.ERROR_READING_FILE, file, e ); } } catch ( OutOfMemoryError e ) { throw new FileException( ErrorId.NOT_ENOUGH_MEMORY_TO_READ_FILE, file ); } // Close input stream try { inStream.close( ); inStream = null; } catch ( IOException e ) { throw new FileException( ErrorId.FAILED_TO_CLOSE_FILE, file, e ); } } catch ( AppException e ) { try { if ( inStream != null ) inStream.close( ); } catch ( IOException e1 ) { // ignore } throw e; } // Return buffer return buffer.getBuffer( ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance variables//////////////////////////////////////////////////////////////////////// private File file; private String charsetName; private List<ProgressListener> progressListeners;}//----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?