litesoft
@ 947
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 190 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.aokeyhole.persist.lines; import java.io.*; import java.text.*; import org.litesoft.commonfoundation.typeutils.*; import java.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( Timestamps.now() ); } 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() ); } } } } |
Commits for litesoft/trunk/Java/KeyHole/src/org/litesoft/aokeyhole/persist/lines/FileLinesPersister.java
Revision | Author | Commited | Message |
---|---|---|---|
947 Diff | GeorgeS | Fri 06 Jun, 2014 23:36:56 +0000 | Correct Spelling of package! |
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
858 Diff | GeorgeS | Sun 04 Nov, 2012 18:40:40 +0000 | |
830 Diff | GeorgeS | Fri 31 Aug, 2012 18:10:19 +0000 | |
787 Diff | GeorgeS | Mon 30 Jul, 2012 03:00:12 +0000 | |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
17 Diff | GeorgeS | Thu 18 Feb, 2010 23:54:44 +0000 | |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |