Skip to content

Commit cdd0750

Browse files
author
Niall Pemberton
committed
IO-234 Add Methods For Retrieving System Directories/Paths to FileUtils - thanks to Michael Wooten for the patch
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@982449 13f79535-47bb-0310-9956-ffa450edef68
1 parent f4eb259 commit cdd0750

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

src/java/org/apache/commons/io/FileUtils.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,51 @@ public FileUtils() {
112112
*/
113113
public static final File[] EMPTY_FILE_ARRAY = new File[0];
114114

115+
//-----------------------------------------------------------------------
116+
/**
117+
* Returns the path to the system temporary directory.
118+
*
119+
* @return the path to the system temporary directory.
120+
*
121+
* @since Commons IO 2.0
122+
*/
123+
public static String getTempDirectoryPath() {
124+
return System.getProperty("java.io.tmpdir");
125+
}
126+
127+
/**
128+
* Returns a {@link File} representing the system temporary directory.
129+
*
130+
* @return the system temporary directory.
131+
*
132+
* @since Commons IO 2.0
133+
*/
134+
public static File getTempDirectory() {
135+
return new File(getTempDirectoryPath());
136+
}
137+
138+
/**
139+
* Returns the path to the user's home directory.
140+
*
141+
* @return the path to the user's home directory.
142+
*
143+
* @since Commons IO 2.0
144+
*/
145+
public static String getUserDirectoryPath() {
146+
return System.getProperty("user.home");
147+
}
148+
149+
/**
150+
* Returns a {@link File} representing the user's home directory.
151+
*
152+
* @return the user's home directory.
153+
*
154+
* @since Commons IO 2.0
155+
*/
156+
public static File getUserDirectory() {
157+
return new File(getUserDirectoryPath());
158+
}
159+
115160
//-----------------------------------------------------------------------
116161
/**
117162
* Opens a {@link FileInputStream} for the specified file, providing better

src/test/org/apache/commons/io/FileUtilsTestCase.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,39 @@ protected void tearDown() throws Exception {
100100
FileUtils.deleteDirectory(getTestDirectory());
101101
}
102102

103+
//-----------------------------------------------------------------------
104+
/**
105+
* Tests the {@link FileUtils#getTempDirectoryPath()} method.
106+
*/
107+
public void testGetTempDirectoryPath() {
108+
assertEquals(System.getProperty("java.io.tmpdir"),
109+
FileUtils.getTempDirectoryPath());
110+
}
111+
112+
/**
113+
* Tests the {@link FileUtils#getTempDirectory()} method.
114+
*/
115+
public void testGetTempDirectory() {
116+
File tempDirectory = new File(System.getProperty("java.io.tmpdir"));
117+
assertEquals(tempDirectory, FileUtils.getTempDirectory());
118+
}
119+
120+
/**
121+
* Tests the {@link FileUtils#getUserDirectoryPath()} method.
122+
*/
123+
public void testGetUserDirectoryPath() {
124+
assertEquals(System.getProperty("user.home"),
125+
FileUtils.getUserDirectoryPath());
126+
}
127+
128+
/**
129+
* Tests the {@link FileUtils#getUserDirectory()} method.
130+
*/
131+
public void testGetUserDirectory() {
132+
File userDirectory = new File(System.getProperty("user.home"));
133+
assertEquals(userDirectory, FileUtils.getUserDirectory());
134+
}
135+
103136
//-----------------------------------------------------------------------
104137
public void test_openInputStream_exists() throws Exception {
105138
File file = new File(getTestDirectory(), "test.txt");

0 commit comments

Comments
 (0)