litesoft
@ 829
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.aokeyhole.persist; import java.io.*; import java.text.*; import java.util.*; import org.litesoft.util.*; public class FileLinesPersister implements LinesPersister { public static final String SUFFIX_OLD = ".previous"; public static final String SUFFIX_NEW = ".tmpNew"; private File mFile; private String mName; private boolean mReadOnly; private static final String TIMESTAMP_PREVIOUS = "TimestampPrevious"; public FileLinesPersister( String pFileName ) throws IOException { mFile = new File( pFileName ); if ( !mFile.exists() || mFile.isDirectory() ) { throw new FileNotFoundException( pFileName + " Not a File" ); } if ( !mFile.canRead() ) { throw new IOException( pFileName + " Not Readable" ); } mName = cleanName( mFile.getName() ); mReadOnly = !mFile.canWrite(); if ( mName.length() == 0 ) { throw new IOException( pFileName + " does NOT appear to have a File 'Name'" ); } } private String cleanName( String pName ) { pName += '.'; return pName.substring( 0, pName.indexOf( '.' ) ); } @Override public String getName() { return mName; } @Override public boolean isReadOnly() { return mReadOnly; } @Override public String[] loadLines() throws IOException { BufferedReader reader = new BufferedReader( new FileReader( mFile ) ); List<String> lines = new ArrayList<String>(); boolean ok = false; try { for ( String line; null != (line = reader.readLine()); ) { lines.add( line ); } ok = true; } finally { if ( !ok ) { try { reader.close(); } catch ( IOException ignore ) { // Have to eat this because Java provides no mechanism to release the file resources that does NOT throw an exception } } } reader.close(); return lines.toArray( new String[lines.size()] ); } @Override public void saveLines( String[] pLines ) throws IOException { String fPath = mFile.getPath(); File newFile = new File( fPath + SUFFIX_NEW ); File oldFile = new File( fPath + timestamp() + SUFFIX_OLD ); writeLines( newFile, pLines ); roll( newFile, mFile, oldFile ); } private String timestamp() { return System.getProperty( TIMESTAMP_PREVIOUS ) == null ? // "" : // "." + new SimpleDateFormat( "yyyyMMddHHmmss" ).format( Utils.getCurrentTimestamp() ); } private void writeLines( File pFile, String[] pLines ) throws IOException { BufferedWriter writer = new BufferedWriter( new FileWriter( pFile ) ); boolean ok = false; try { if ( pLines != null ) { for ( String line : pLines ) { if ( line != null ) { writer.write( line ); writer.newLine(); } } } ok = true; } finally { if ( !ok ) { try { writer.close(); } catch ( IOException ignore ) { // Have to eat this because Java provides no mechanism to release the file resources that does NOT throw an exception } } } writer.close(); } private void roll( File pNewFile, File pCurFile, File pOldFile ) throws IOException { if ( pCurFile.exists() ) { if ( pOldFile.exists() ) { remove( pOldFile ); } rename( pCurFile, pOldFile ); } rename( pNewFile, pCurFile ); } private void rename( File pFileFrom, File pFileTo ) throws IOException { if ( pFileFrom.exists() ) { if ( !pFileFrom.renameTo( pFileTo ) ) { throw new IOException( "Unable to rename (" + pFileFrom.getPath() + ") to: " + pFileTo.getPath() ); } } } private void remove( File pFileToDel ) throws IOException { if ( pFileToDel.exists() ) { if ( !pFileToDel.delete() ) { throw new IOException( "Unable to remove/delete: " + pFileToDel.getPath() ); } } } } |