Database Reference
In-Depth Information
encapsulates filesystem metadata for files and directories, including file length, block size,
replication, modification time, ownership, and permission information.
The method
getFileStatus()
on
FileSystem
provides a way of getting a
FileStatus
object for a single file or directory.
Example 3-5
shows an example of its
use.
Example 3-5. Demonstrating file status information
public class
ShowFileStatusTest
{
private
MiniDFSCluster cluster
;
// use an in-process HDFS cluster for
testing
private
FileSystem fs
;
@Before
public
void
setUp
()
throws
IOException
{
Configuration conf
=
new
Configuration
();
if
(
System
.
getProperty
(
"test.build.data"
) ==
null
) {
System
.
setProperty
(
"test.build.data"
,
"/tmp"
);
}
cluster
=
new
MiniDFSCluster
.
Builder
(
conf
).
build
();
fs
=
cluster
.
getFileSystem
();
OutputStream out
=
fs
.
create
(
new
Path
(
"/dir/file"
));
out
.
write
(
"content"
.
getBytes
(
"UTF-8"
));
out
.
close
();
}
@After
public
void
tearDown
()
throws
IOException
{
if
(
fs
!=
null
) {
fs
.
close
(); }
if
(
cluster
!=
null
) {
cluster
.
shutdown
(); }
}
@Test
(
expected
=
FileNotFoundException
.
class
)
public
void
throwsFileNotFoundForNonExistentFile
()
throws
IOException
{
fs
.
getFileStatus
(
new
Path
(
"no-such-file"
));
}
@Test
public
void
fileStatusForFile
()
throws
IOException
{
Path file
=
new
Path
(
"/dir/file"
);
FileStatus stat
=
fs
.
getFileStatus
(
file
);
assertThat
(
stat
.
getPath
().
toUri
().
getPath
(),
is
(
"/dir/file"
));
assertThat
(
stat
.
isDirectory
(),
is
(
false
));
assertThat
(
stat
.
getLen
(),
is
(
7L
));