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
|
|
|
|
2017-07-10 16:01:56 +00:00
|
|
|
private static void isRootParent() {
|
|
|
|
if(!IsWindows) {
|
|
|
|
File f = new File("/root");
|
|
|
|
File f2 = f.getParentFile();
|
|
|
|
System.out.println("------------"+f2);
|
|
|
|
expect(f2.getPath().equals("/"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2017-07-10 16:01:56 +00:00
|
|
|
isRootParent();
|
2012-03-13 14:26:51 +00:00
|
|
|
|
2012-05-05 01:55:53 +00:00
|
|
|
{ File f = new File("test.txt");
|
|
|
|
FileOutputStream out = new FileOutputStream(f);
|
2017-07-19 12:29:47 +00:00
|
|
|
byte[] message = "hello, world!\n".getBytes("UTF-8");
|
|
|
|
out.write(message);
|
|
|
|
out.close();
|
2012-05-05 01:55:53 +00:00
|
|
|
|
2017-07-19 12:29:47 +00:00
|
|
|
expect(f.lastModified() > 0);
|
2013-10-17 19:30:41 +00:00
|
|
|
|
2017-07-19 12:29:47 +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);
|
2012-05-05 01:55:53 +00:00
|
|
|
}
|
2017-07-19 12:29:47 +00:00
|
|
|
|
|
|
|
expect(in.read() == -1);
|
|
|
|
expect(in.available() == 0);
|
2012-05-05 01:55:53 +00:00
|
|
|
} finally {
|
2017-07-19 12:29:47 +00:00
|
|
|
in.close();
|
2012-05-05 01:55:53 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|