Skip to content

Commit e29fdab

Browse files
Added new classes
1 parent d69a8ee commit e29fdab

4 files changed

Lines changed: 179 additions & 4 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>0.6.0</version>
26+
<version>1.0.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:0.6.0'
35+
compile 'com.mauriciotogneri:javautils:1.0.0'
3636
}
3737
```

pom.xml

Lines changed: 2 additions & 2 deletions
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>0.6.0</version>
10+
<version>1.0.0</version>
1111
<name>Java Utils</name>
1212
<packaging>jar</packaging>
1313
<url>https://github.com/mauriciotogneri/java-utils</url>
@@ -69,7 +69,7 @@
6969
<dependency>
7070
<groupId>com.google.code.gson</groupId>
7171
<artifactId>gson</artifactId>
72-
<version>2.8.0</version>
72+
<version>2.8.1</version>
7373
</dependency>
7474

7575
<dependency>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.mauriciotogneri.javautils;
2+
3+
import java.io.Closeable;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.OutputStream;
8+
9+
public class Files
10+
{
11+
public File create(File file) throws IOException
12+
{
13+
Record record = new Record(file);
14+
15+
if (record.create())
16+
{
17+
return record.file();
18+
}
19+
else
20+
{
21+
throw new IOException(String.format("File '%s' cannot be created", file.getAbsolutePath()));
22+
}
23+
}
24+
25+
public File create(String path) throws IOException
26+
{
27+
return create(new File(path));
28+
}
29+
30+
public void copy(InputStream inputStream, OutputStream outputStream) throws IOException
31+
{
32+
int read;
33+
byte[] bytes = new byte[1024];
34+
35+
while ((read = inputStream.read(bytes)) != -1)
36+
{
37+
outputStream.write(bytes, 0, read);
38+
}
39+
40+
outputStream.flush();
41+
42+
close(outputStream);
43+
}
44+
45+
public void close(Closeable closeable)
46+
{
47+
if (closeable != null)
48+
{
49+
try
50+
{
51+
closeable.close();
52+
}
53+
catch (Exception e)
54+
{
55+
// ignore
56+
}
57+
}
58+
}
59+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.mauriciotogneri.javautils;
2+
3+
import java.io.File;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.RandomAccessFile;
7+
8+
public class Record
9+
{
10+
private final File file;
11+
12+
public Record(File file)
13+
{
14+
this.file = file;
15+
}
16+
17+
public Record(String path)
18+
{
19+
this(new File(path));
20+
}
21+
22+
public File file()
23+
{
24+
return file;
25+
}
26+
27+
public boolean exists()
28+
{
29+
return file.exists();
30+
}
31+
32+
public String name()
33+
{
34+
return file.getName();
35+
}
36+
37+
public String path()
38+
{
39+
return file.getAbsolutePath();
40+
}
41+
42+
public Record parent()
43+
{
44+
return new Record(file.getParentFile());
45+
}
46+
47+
public boolean isDirectory()
48+
{
49+
return file.isDirectory();
50+
}
51+
52+
public boolean isFile()
53+
{
54+
return file.isFile();
55+
}
56+
57+
public boolean create() throws IOException
58+
{
59+
File parent = file.getParentFile();
60+
61+
return ((parent.exists() || parent.mkdirs()) && (file.exists() || file.createNewFile()));
62+
}
63+
64+
public boolean remove()
65+
{
66+
return file.exists() && file.delete();
67+
}
68+
69+
public String string() throws IOException
70+
{
71+
return new String(bytes());
72+
}
73+
74+
public byte[] bytes() throws IOException
75+
{
76+
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
77+
byte[] bytes = new byte[(int) randomAccessFile.length()];
78+
randomAccessFile.readFully(bytes);
79+
randomAccessFile.close();
80+
81+
return bytes;
82+
}
83+
84+
public boolean write(String content) throws IOException
85+
{
86+
return write(content.getBytes("UTF-8"));
87+
}
88+
89+
public boolean write(byte[] data) throws IOException
90+
{
91+
if (create())
92+
{
93+
FileOutputStream outputStream = null;
94+
95+
try
96+
{
97+
outputStream = new FileOutputStream(file);
98+
outputStream.write(data);
99+
outputStream.flush();
100+
}
101+
finally
102+
{
103+
if (outputStream != null)
104+
{
105+
outputStream.close();
106+
}
107+
}
108+
109+
return true;
110+
}
111+
else
112+
{
113+
return false;
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)