powered by simpleCommunicator - 2.0.56     © 2025 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / Java [игнор отключен] [закрыт для гостей] / free disk space - как найти ???
2 сообщений из 2, страница 1 из 1
free disk space - как найти ???
    #32527713
Aleksej
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Здравствуйте

никак не могу найти возможность выяснить сколько свободного места есть на диске с помощью "pure java", без использования C и т. д.

Всё что пока нашёл, это типа нижеследующего, но это только для Windows и то неизвестно всегда ли сработает:

Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
public static long getFreeDiskSpace(String dirName)
{
try
{
// guess correct 'dir' command by looking at the 
// operating system name
String os = System.getProperty("os.name");
String command;
if (os.equals("Windows NT") ||
os.equals("Windows 2000"))
{
command = "cmd.exe /c dir " + dirName;
}
else
{
command = "command.com /c dir " + dirName;
}
// run the dir command on the argument directory name
Runtime runtime = Runtime.getRuntime();
Process process = null;
process = runtime.exec(command);
if (process == null)
{
return - 1 ;
}
// read the output of the dir command
// only the last line is of interest 
BufferedReader in = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
String freeSpace = null;
while ((line = in.readLine()) != null)
{
freeSpace = line;
}
if (freeSpace == null)
{
return - 1 ;
}
process.destroy();
// remove dots & commas & leading and trailing whitespace
freeSpace = freeSpace.trim();
freeSpace = freeSpace.replaceAll("\\.", "");
freeSpace = freeSpace.replaceAll(",", "");
String[] items = freeSpace.split(" ");
// the first valid numeric value in items after(!) index  0 
// is probably the free disk space
int index =  1 ;
while (index < items.length)
{
try
{
long bytes = Long.parseLong(items[index++]);
return bytes;
}
catch (NumberFormatException nfe)
{
}
}
return - 1 ;
}
catch (Exception exception)
{
return - 1 ;
} 
}


Есть ли какая нибудь другая возможность ?

Спасибо

Алексей
...
Рейтинг: 0 / 0
free disk space - как найти ???
    #32527748
Грасоff
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
никак.

только тем что ты делаешь. т.е. либо dir (win) либо df (unix).

есть тулзы специальные, которые парсинг результата указанных команд делают сами. например JConfig:

http://www.tolstoy.com/samizdat/jconfig.html

.: 6.000.000 ways to die :.
...
Рейтинг: 0 / 0
2 сообщений из 2, страница 1 из 1
Форумы / Java [игнор отключен] [закрыт для гостей] / free disk space - как найти ???
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]