Skip to content

Commit 7b62700

Browse files
Improved Record class
1 parent e29fdab commit 7b62700

5 files changed

Lines changed: 194 additions & 66 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ and the dependency:
2323
<dependency>
2424
<groupId>com.mauriciotogneri</groupId>
2525
<artifactId>javautils</artifactId>
26-
<version>1.0.0</version>
26+
<version>1.1.0</version>
2727
</dependency>
2828
```
2929

@@ -32,6 +32,6 @@ or if you use Gradle:
3232
```groovy
3333
dependencies
3434
{
35-
compile 'com.mauriciotogneri:javautils:1.0.0'
35+
compile 'com.mauriciotogneri:javautils:1.1.0'
3636
}
3737
```

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<modelVersion>4.0.0</modelVersion>
88
<groupId>com.mauriciotogneri</groupId>
99
<artifactId>javautils</artifactId>
10-
<version>1.0.0</version>
10+
<version>1.1.0</version>
1111
<name>Java Utils</name>
1212
<packaging>jar</packaging>
1313
<url>https://github.com/mauriciotogneri/java-utils</url>

src/main/java/com/mauriciotogneri/javautils/Files.java

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/main/java/com/mauriciotogneri/javautils/Record.java

Lines changed: 146 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package com.mauriciotogneri.javautils;
22

33
import java.io.File;
4+
import java.io.FileInputStream;
45
import java.io.FileOutputStream;
56
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.OutputStream;
69
import java.io.RandomAccessFile;
10+
import java.util.ArrayList;
11+
import java.util.List;
712

813
public 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
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.mauriciotogneri.javautils;
2+
3+
import java.io.Closeable;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.OutputStream;
7+
8+
public class Streams
9+
{
10+
public static void copy(InputStream inputStream, OutputStream outputStream) throws IOException
11+
{
12+
try
13+
{
14+
int read;
15+
byte[] bytes = new byte[1024];
16+
17+
while ((read = inputStream.read(bytes)) != -1)
18+
{
19+
outputStream.write(bytes, 0, read);
20+
}
21+
22+
outputStream.flush();
23+
}
24+
finally
25+
{
26+
close(inputStream);
27+
close(outputStream);
28+
}
29+
}
30+
31+
public static void close(Closeable closeable)
32+
{
33+
if (closeable != null)
34+
{
35+
try
36+
{
37+
closeable.close();
38+
}
39+
catch (Exception e)
40+
{
41+
// ignore
42+
}
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)