| 804 | } |
| 805 | |
| 806 | public static boolean moveFile( File oldPlace, File newPlace ) { |
| 807 | boolean removeNewFile = true; |
| 808 | log.i("Moving file " + oldPlace.getAbsolutePath() + " to " + newPlace.getAbsolutePath()); |
| 809 | if ( !oldPlace.exists() ) { |
| 810 | log.e("File " + oldPlace.getAbsolutePath() + " does not exist!"); |
| 811 | return false; |
| 812 | } |
| 813 | FileOutputStream os = null; |
| 814 | FileInputStream is = null; |
| 815 | try { |
| 816 | if ( !newPlace.createNewFile() ) |
| 817 | return false; // cannot create file |
| 818 | os = new FileOutputStream(newPlace); |
| 819 | is = new FileInputStream(oldPlace); |
| 820 | byte[] buf = new byte[0x10000]; |
| 821 | for (;;) { |
| 822 | int bytesRead = is.read(buf); |
| 823 | if ( bytesRead<=0 ) |
| 824 | break; |
| 825 | os.write(buf, 0, bytesRead); |
| 826 | } |
| 827 | removeNewFile = false; |
| 828 | oldPlace.delete(); |
| 829 | return true; |
| 830 | } catch ( IOException e ) { |
| 831 | return false; |
| 832 | } finally { |
| 833 | try { |
| 834 | if (os != null) |
| 835 | os.close(); |
| 836 | } catch (IOException ee) { |
| 837 | // ignore |
| 838 | } |
| 839 | try { |
| 840 | if (is != null) |
| 841 | is.close(); |
| 842 | } catch (IOException ee) { |
| 843 | // ignore |
| 844 | } |
| 845 | if ( removeNewFile ) |
| 846 | newPlace.delete(); |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | /** |
| 851 | * Checks whether file under old path exists, and moves it to better place when necessary. |