11package com .mauriciotogneri .javautils ;
22
33import java .io .File ;
4+ import java .io .FileInputStream ;
45import java .io .FileOutputStream ;
56import java .io .IOException ;
7+ import java .io .InputStream ;
8+ import java .io .OutputStream ;
69import java .io .RandomAccessFile ;
10+ import java .util .ArrayList ;
11+ import java .util .List ;
712
813public class Record
914{
@@ -19,6 +24,16 @@ public Record(String path)
1924 this (new File (path ));
2025 }
2126
27+ public Record (File parent , String path )
28+ {
29+ this (new File (parent , path ));
30+ }
31+
32+ public Record (Record parent , String path )
33+ {
34+ this (new File (parent .file , path ));
35+ }
36+
2237 public File file ()
2338 {
2439 return file ;
@@ -39,29 +54,150 @@ public String path()
3954 return file .getAbsolutePath ();
4055 }
4156
57+ public long size ()
58+ {
59+ return file .length ();
60+ }
61+
62+ public String extension ()
63+ {
64+ String name = name ();
65+
66+ int index = name .lastIndexOf ("." );
67+
68+ if (index > -1 )
69+ {
70+ String extension = name .substring (index + 1 );
71+
72+ return extension .trim ();
73+ }
74+ else
75+ {
76+ return "" ;
77+ }
78+ }
79+
4280 public Record parent ()
4381 {
4482 return new Record (file .getParentFile ());
4583 }
4684
85+ public boolean rename (String name )
86+ {
87+ File newFile = new File (file .getParentFile (), name );
88+
89+ return !newFile .exists () && file .renameTo (newFile );
90+ }
91+
4792 public boolean isDirectory ()
4893 {
4994 return file .isDirectory ();
5095 }
5196
97+ public List <Record > children ()
98+ {
99+ List <Record > result = new ArrayList <>();
100+
101+ if (isDirectory ())
102+ {
103+ File [] children = file .listFiles ();
104+
105+ if (children != null )
106+ {
107+ for (File child : children )
108+ {
109+ if (child != null )
110+ {
111+ result .add (new Record (child ));
112+ }
113+ }
114+ }
115+ }
116+
117+ return result ;
118+ }
119+
52120 public boolean isFile ()
53121 {
54122 return file .isFile ();
55123 }
56124
57- public boolean create () throws IOException
125+ public boolean createFile () throws IOException
126+ {
127+ File parent = file .getParentFile ();
128+
129+ return (parent .exists () || parent .mkdirs ()) && (file .exists () || file .createNewFile ());
130+ }
131+
132+ public boolean createFolder () throws IOException
58133 {
59134 File parent = file .getParentFile ();
60135
61- return ((parent .exists () || parent .mkdirs ()) && (file .exists () || file .createNewFile ()));
136+ return (parent .exists () || parent .mkdirs ());
137+ }
138+
139+ public boolean copy (Record target ) throws IOException
140+ {
141+ return copy (target , false );
142+ }
143+
144+ public boolean move (Record target ) throws IOException
145+ {
146+ return copy (target , true );
62147 }
63148
64- public boolean remove ()
149+ private boolean copy (Record target , boolean delete ) throws IOException
150+ {
151+ if (target .isFile ())
152+ {
153+ throw new IOException (String .format ("Cannot copy '%s' to '%s'" , path (), target .path ()));
154+ }
155+
156+ if (isDirectory ())
157+ {
158+ Record targetFolder = new Record (target , name ());
159+ targetFolder .createFolder ();
160+
161+ boolean allCopied = true ;
162+
163+ for (Record record : children ())
164+ {
165+ allCopied &= record .copy (targetFolder , delete );
166+ }
167+
168+ if (delete && allCopied )
169+ {
170+ return delete ();
171+ }
172+ else
173+ {
174+ return allCopied ;
175+ }
176+ }
177+ else
178+ {
179+ Record targetFile = new Record (target , name ());
180+ targetFile .createFile ();
181+
182+ InputStream input = new FileInputStream (file );
183+ OutputStream output = new FileOutputStream (targetFile .file );
184+
185+ Streams .copy (input , output );
186+
187+ return !delete || delete ();
188+ }
189+ }
190+
191+ public static void main (String [] args ) throws IOException
192+ {
193+ Record record = new Record ("/home/max/TEST/aaa/Screenshot from 2017-06-10 16-51-09.png" );
194+
195+ boolean result = record .move (new Record ("/home/max/TEST/ddd" ));
196+
197+ System .out .print (result );
198+ }
199+
200+ public boolean delete ()
65201 {
66202 return file .exists () && file .delete ();
67203 }
@@ -88,7 +224,7 @@ public boolean write(String content) throws IOException
88224
89225 public boolean write (byte [] data ) throws IOException
90226 {
91- if (create ())
227+ if (createFile ())
92228 {
93229 FileOutputStream outputStream = null ;
94230
@@ -113,4 +249,10 @@ public boolean write(byte[] data) throws IOException
113249 return false ;
114250 }
115251 }
252+
253+ @ Override
254+ public String toString ()
255+ {
256+ return path ();
257+ }
116258}
0 commit comments