2011-12-29 17:17:01 +00:00
|
|
|
import java.io.File;
|
2012-05-05 01:55:53 +00:00
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
2011-12-29 17:17:01 +00:00
|
|
|
|
|
|
|
public class Files {
|
2015-09-02 14:31:30 +00:00
|
|
|
private static final boolean IsWindows
|
|
|
|
= System.getProperty("os.name").equals("Windows");
|
|
|
|
|
2011-12-29 17:17:01 +00:00
|
|
|
private static void expect(boolean v) {
|
|
|
|
if (! v) throw new RuntimeException();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void isAbsoluteTest(boolean absolutePath) {
|
|
|
|
File file = new File("test.txt");
|
|
|
|
if (absolutePath) {
|
|
|
|
file = file.getAbsoluteFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean isAbsolute = file.isAbsolute();
|
|
|
|
|
|
|
|
if (absolutePath) {
|
|
|
|
expect(isAbsolute);
|
|
|
|
} else {
|
|
|
|
expect(!isAbsolute);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-02-09 21:04:42 +00:00
|
|
|
|
2012-02-21 00:23:18 +00:00
|
|
|
private static void setExecutableTestWithPermissions(boolean executable)
|
|
|
|
throws Exception
|
|
|
|
{
|
2012-02-09 21:04:42 +00:00
|
|
|
File file = File.createTempFile("avian.", null);
|
2013-11-26 22:05:14 +00:00
|
|
|
try {
|
|
|
|
file.setExecutable(executable);
|
|
|
|
if (executable) {
|
|
|
|
expect(file.canExecute());
|
|
|
|
} else {
|
|
|
|
// Commented out because this will fail on Windows - both on Avian and on OpenJDK
|
|
|
|
// The implementation for Windows considers canExecute() to be the same as canRead()
|
|
|
|
// expect(!file.canExecute());
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
expect(file.delete());
|
2012-02-09 21:04:42 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-29 17:17:01 +00:00
|
|
|
|
2012-02-21 00:23:18 +00:00
|
|
|
public static void main(String[] args) throws Exception {
|
2011-12-29 17:17:01 +00:00
|
|
|
isAbsoluteTest(true);
|
|
|
|
isAbsoluteTest(false);
|
2012-02-09 21:04:42 +00:00
|
|
|
setExecutableTestWithPermissions(true);
|
|
|
|
setExecutableTestWithPermissions(false);
|
2012-03-13 14:26:51 +00:00
|
|
|
|
|
|
|
{ File f = new File("test.txt");
|
|
|
|
f.createNewFile();
|
|
|
|
expect(! f.createNewFile());
|
|
|
|
f.delete();
|
|
|
|
}
|
2012-05-05 01:55:53 +00:00
|
|
|
|
|
|
|
{ File f = new File("test.txt");
|
|
|
|
FileOutputStream out = new FileOutputStream(f);
|
|
|
|
try {
|
|
|
|
byte[] message = "hello, world!\n".getBytes();
|
|
|
|
out.write(message);
|
|
|
|
out.close();
|
|
|
|
|
2013-10-17 19:30:41 +00:00
|
|
|
expect(f.lastModified() > 0);
|
|
|
|
|
2012-05-05 01:55:53 +00:00
|
|
|
FileInputStream in = new FileInputStream(f);
|
|
|
|
try {
|
|
|
|
expect(in.available() == message.length);
|
|
|
|
|
|
|
|
for (int i = 0; i < message.length; ++i) {
|
|
|
|
in.read();
|
|
|
|
expect(in.available() == message.length - i - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(in.read() == -1);
|
|
|
|
expect(in.available() == 0);
|
|
|
|
} finally {
|
|
|
|
in.close();
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
f.delete();
|
|
|
|
}
|
|
|
|
}
|
2012-07-31 15:27:18 +00:00
|
|
|
|
2015-09-02 14:31:30 +00:00
|
|
|
if(IsWindows) {
|
|
|
|
expect(new File("/c:\\test").getPath().equals("c:\\test"));
|
|
|
|
} else {
|
|
|
|
expect(new File("/c:\\test").getPath().equals("/c:\\test"));
|
|
|
|
}
|
|
|
|
|
2012-07-31 15:27:18 +00:00
|
|
|
expect(new File("foo/bar").getParent().equals("foo"));
|
|
|
|
expect(new File("foo/bar/").getParent().equals("foo"));
|
|
|
|
expect(new File("foo/bar//").getParent().equals("foo"));
|
|
|
|
|
|
|
|
expect(new File("foo/nonexistent-directory").listFiles() == null);
|
2011-12-29 17:17:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|