Java Create Temp File - java

What is the maximum name length of the TempFile in java and MaximumFilesize is depending
on the machine where we mention the temp directory to be created or some other java based?
When to call the deleteOnExit() method--- but what is the use of this method because it gets called when the JVM comes down.But in Production based servers will run 24*7.So file will be created continuously and it will be problem for the server where we create file because of the memory.

To autoclean temp-files older (modified) than XX seconds...
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
public class FileAutoCleaner {
final static FileAutoCleaner singleton = new FileAutoCleaner();
final HashSet<File> bag = new HashSet<File>();
public static FileAutoCleaner getInstance() {
return singleton;
}
// This create the temp file and add to bag for checking
public synchronized File createTempFile(String prefix, String suffix) throws IOException {
File tmp = File.createTempFile(prefix, suffix);
tmp.deleteOnExit();
bag.add(tmp);
return tmp;
}
// Periodically call this function to clean old files
public synchronized void cleanOldFiles(final int secondsOld) {
long now = (System.currentTimeMillis() / 1000);
for (File f : bag) {
long expired = (f.lastModified() / 1000) + secondsOld;
if (now >= expired) {
System.out.println("Deleted file=" + f.getAbsolutePath());
f.delete();
bag.remove(f);
}
}
}
public static void main(String[] args) throws Exception {
FileAutoCleaner fac = FileAutoCleaner.getInstance();
System.out.println(System.currentTimeMillis() / 1000);
fac.createTempFile("deleteme", "tmp");
for (int i = 0; i < 5; i++) {
System.out.println(System.currentTimeMillis() / 1000);
// delete if older than 2 seconds
fac.cleanOldFiles(2);
Thread.sleep(1000);
}
}
}

What is the maximum name length of the TempFile in java and MaximumFilesize is depenting on the machine where we mention the temp directory to be created or some other java based?
1775 static File generateFile(String prefix, String suffix, File dir) {
1776 long n = random.nextLong();
1777 if (n == Long.MIN_VALUE) {
1778 n = 0; // corner case
1779 } else {
1780 n = Math.abs(n);
1781 }
1782 return new File(dir, prefix + Long.toString(n) + suffix);
1783 }
so the file name could be any random long with prefix suffix
When to call the deleteOnExit() method--- but what is the use of this method because it gets called when the JVM comes down.But in Production based servers will run 24*7
There are some file thats needs to be created for application life,
For example when you launch eclipse you will see .lock file created to lock the work space it will get deleted when your eclipse exists

Maximum file sizes in java are limited to Long.MAX_VALUE but.... this, and filename length are limited by the underlying filesystem.... like EXT4 (Linux) or NTFS (Windows)

String tmpDir = System.getProperty("java.io.tmpdir");
File file=new File(tmpDir+"\"+fileName+".tmp");

Related

How to find specific directory and its files according to the keyword passed in java and loading in memory approach

I have a project structure like below:
Now, my problem statement is I have to iterate resources folder, and given a key, I have to find that specific folder and its files.
For that, I have written a below code with the recursive approach but I am not getting the output as intended:
public class ConfigFileReader {
public static void main(String[] args) throws Exception {
System.out.println("Print L");
String path = "C:\\...\\ConfigFileReader\\src\\resources\\";
//FileReader reader = new FileReader(path + "\\Encounter\\Encounter.properties");
//Properties p = new Properties();
//p.load(reader);
File[] files = new File(path).listFiles();
String resourceType = "Encounter";
System.out.println(navigateDirectoriesAndFindTheFile(resourceType, files));
}
public static String navigateDirectoriesAndFindTheFile(String inputResourceString, File[] files) {
String entirePathOfTheIntendedFile = "";
for (File file : files) {
if (file.isDirectory()) {
navigateDirectoriesAndFindTheFile(inputResourceString, file.listFiles());
System.out.println("Directory: " + file.getName());
if (file.getName().startsWith(inputResourceString)) {
entirePathOfTheIntendedFile = file.getPath();
}
} else {
System.out.print("Inside...");
entirePathOfTheIntendedFile = file.getPath();
}
}
return entirePathOfTheIntendedFile;
}
}
Output:
The output should return C:\....\Encounter\Encounter.properties as the path.
First of all, if it finds the string while traversing it should return the file inside that folder and without navigating the further part as well as what is the best way to iterate over suppose 1k files because every time I can't follow this method because it doesn't seem an effective way of doing it. So, how can I use an in-memory approach for this problem? Please guide me through it.
You will need to check the output of recursive call and pass that back when a match is found.
Always use File or Path to handle filenames.
Assuming that I've understood the logic of the search, try this which scans for files of form XXX\XXXyyyy
public class ConfigReader
{
public static void main(String[] args) throws Exception {
System.out.println("Print L");
File path = new File(args[0]).getAbsoluteFile();
String resourceType = "Encounter";
System.out.println(navigateDirectoriesAndFindTheFile(resourceType, path));
}
public static File navigateDirectoriesAndFindTheFile(String inputResourceString, File path) {
File[] files = path.listFiles();
File found = null;
for (int i = 0; found == null && files != null && i < files.length; i++) {
File file = files[i];
if (file.isDirectory()) {
found = navigateDirectoriesAndFindTheFile(inputResourceString, file);
} else if (file.getName().startsWith(inputResourceString) && file.getParentFile().getName().equals(inputResourceString)) {
found = file;
}
}
return found;
}
}
If this is slow especially for 1K of files re-write with Files.walkFileTree which would be much faster than File.list() in recursion.

How to create a temp file in java without the random number appended to the filename?

I need to create a temp file, so I tried this:
String[] TempFiles = {"c1234c10","c1234c11","c1234c12","c1234c13"};
for (int i = 0; i <= 3; i++) {
try {
String tempFile = TempFiles[i];
File temp = File.createTempFile(tempFile, ".xls");
System.out.println("Temp file : " + temp.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
The output is something like this:
Temp file : C:\Users\MD1000\AppData\Local\Temp\c1234c108415816200650069233.xls
Temp file : C:\Users\MD1000\AppData\Local\Temp\c1234c113748833645638701089.xls
Temp file : C:\Users\MD1000\AppData\Local\Temp\c1234c126104766829220422260.xls
Temp file : C:\Users\MD1000\AppData\Local\Temp\c1234c137493179265536640669.xls
Now, I don't want the extra numbers (long int) which is getting added to the file name. How can I achieve that? Thanks
First, use the following snippet to get the system's temp directory:
String tDir = System.getProperty("java.io.tmpdir");
Then use the tDir variable in conjunction with your tempFiles[] array to create each file individually.
Using Guava:
import com.google.common.io.Files;
...
File myTempFile = new File(Files.createTempDir(), "MySpecificName.png");
You can't if you use File.createTempFile to generate a temporary file name. I looked at the java source for generating a temp file (for java 1.7, you didn't state your version so I just used mine):
private static class TempDirectory {
private TempDirectory() { }
// temporary directory location
private static final File tmpdir = new File(fs.normalize(AccessController
.doPrivileged(new GetPropertyAction("java.io.tmpdir"))));
static File location() {
return tmpdir;
}
// file name generation
private static final SecureRandom random = new SecureRandom();
static File generateFile(String prefix, String suffix, File dir) {
long n = random.nextLong();
if (n == Long.MIN_VALUE) {
n = 0; // corner case
} else {
n = Math.abs(n);
}
return new File(dir, prefix + Long.toString(n) + suffix);
}
}
This is the code in the java JDK that generates the temp file name. You can see that it generates a random number and inserts it into your file name between your prefix and suffix. This is in "File.java" (in java.io). I did not see any way to change that.
If you want files with specific names created in the system-wide temporary directory, then expand the %temp% environment variable and create the file manually, nothing wrong with that.
Edit: Actually, use System.getProperty("java.io.tmpdir")); for that.
Just putting up the option here:
If someone anyhow need to use createTempFile method, you can do create a temp file and rename it using Files.move option:
final Path path = Files.createTempFile(fileName, ".xls");
Files.move(path, path.resolveSibling(fileName));
You can create a temp directory then store new files in it. This way all of the new files you add won't have a random extension to it. When you're done all you have to do is delete the temp directory you added.
public static File createTempFile(String prefix, String suffix) {
File parent = new File(System.getProperty("java.io.tmpdir"));
File temp = new File(parent, prefix + suffix);
if (temp.exists()) {
temp.delete();
}
try {
temp.createNewFile();
} catch (IOException ex) {
ex.printStackTrace();
}
return temp;
}
public static File createTempDirectory(String fileName) {
File parent = new File(System.getProperty("java.io.tmpdir"));
File temp = new File(parent, fileName);
if (temp.exists()) {
temp.delete();
}
temp.mkdir();
return temp;
}
Custom names can be saved as follows
File temp=new File(tempFile, ".xls");
if (!temp.exists()) {
temp.createNewFile();
}
public static File createTempDirectory(String dirName) {
File baseDir = new File(System.getProperty("java.io.tmpdir"));
File tempDir = new File(baseDir, dirName);
if (tempDir.mkdir()) {
return tempDir;
}
return null;
}

how to get name of all file in a directory and sub-directory in java

I have a directory which consist of some different sub directory which every one have several files. how can i get name of all file?
If you want to use a library, try the listFiles method from apache commons io FileUtils, which will recurse into directories for you.
Here's an example of how you could call it to find all files named *.dat and *.txt in any directory anywhere under the specified starting directory:
Collection<File> files = FileUtils.listFiles(new File("my/dir/path"), {"dat", "txt"}, true);
public static void walkin(File dir) {
String pattern = "file pattern"; //for example ".java"
File listFile[] = dir.listFiles();
if(listFile != null) {
for(int i=0; i<listFile.length; i++) {
if(listFile[i].isDirectory()) {
walkin(listFile[i]);
} else {
if(listFile[i].getName().endsWith(pattern))
{
System.out.println(listFile[i].getPath());
}
}
}
}
}
Recurse through the directory structure, gathering the names of all the files that are not sub-directories.
You are looking for File.list() take a closer look into the javadoc for more details.
To list a directory using Java do something similar to this
File dir = new File(fname);
String[] list = dir.list();
if(list == null){
System.out.println("Specified directory does not exist or is not a directory.");
System.exit(0);
}else{
//list the directory content
for(int i = 0; i < chld.length; i++){
String fileName = list[i];
System.out.println(fileName);
}
Most of this code comes from here, http://www.roseindia.net/java/beginners/DirectoryListing.shtml
This programme will display the whole structure with nested files and nested sub directories with file system.
import java.io.File;
public class DirectoryStructure
{
static void RecursivePrint(File[] arr, int index, int level)
{
// terminate condition
if (index == arr.length) {
return;
}
// tabs for internal levels
for (int i = 0; i < level; i++) {
System.out.print("\t");
}
// for files
if (arr[index].isFile()) {
System.out.println(arr[index].getName());
}
// for sub-directories
else if (arr[index].isDirectory())
{
System.out.println("[" + arr[index].getName() + "]");
// recursion for sub-directories
RecursivePrint(arr[index].listFiles(), 0, level + 1);
}
// recursion for main directory
RecursivePrint(arr, ++index, level);
}
// Driver Method
public static void main(String[] args)
{
// Provide full path for directory(change accordingly)
String maindirpath = "E:\\dms\\Notes";
// File object
File maindir = new File(maindirpath);
if (maindir.exists() && maindir.isDirectory())
{
// array for files and sub-directories
// of directory pointed by maindir
File arr[] = maindir.listFiles();
System.out.println("**********************************************");
System.out.println("Files from main directory : " + maindir);
System.out.println("**********************************************");
// Calling recursive method
RecursivePrint(arr, 0, 0);
}
}
}
Using Apache Commons
String filePath = "/apps/fraud";
String[] acceptedExtension = {"ctl","otl","dat","csv","xls"};
String[] acceptedFolders = {"suresh","dir","kernel"};
Collection fileList = FileUtils.listFiles(
new File(filePath),
new SuffixFileFilter(acceptedExtension) ,
new NameFileFilter(acceptedFolders)
);

how to set Hidden proprerty of a file

I'm trying to create a static method that let me hide a file.
I've found some possible way to do that and I wrote this:
public static void hide(File src) throws InterruptedException, IOException {
if(System.getProperty("os.name").contains("Windows"))
{
Process p = Runtime.getRuntime().exec("attrib +h " + src.getPath());
p.waitFor();
}
else
{
src.renameTo(new File(src.getParent()+File.separator+"."+src.getName()));
}
}
Unfortunatley this isn't working in windows and neither on Ubuntu...
In Oracle's tuorials I've found this way
Path file = ...;
Files.setAttribute(file, "dos:hidden", true);
but I don't know how to use it because my JDK doesn't have the class "Path".
Can anyone help me with a method that can work in unix OS and Windows?
The Path class was introduced in Java 7.
Before Java 7 there was no built-in way to access properties like this, so you'll have to do something similar to what you're trying (and on Unix-y OS there is no "hidden property", but all files that start with a . are hidden by default).
Regarding your exec() call there's a great (if a bit old) article that lists all the stuff that can go wrong and how to fix it (it's quite an involved process, unfortunately).
And a minor note: new File(src.getParent()+File.separator+"."+src.getName()) can be replaced by new File(src.getParent(), "." + src.getName()), which would be a bit cleaner.
If a file as not parent, getParent() will return null. Perhaps what you wanted for unix was
src.renameTo(new File(src.getParent(), '.'+src.getName()));
Path is available in Java 7.
you won't be able to achieve this with standard JDK code. The File class offers an isHidden method, however, it states clearly that the concept of hidden is file system dependent:
Tests whether the file named by this
abstract pathname is a hidden file.
The exact definition of hidden is
system-dependent. On UNIX systems, a
file is considered to be hidden if its
name begins with a period character
('.'). On Microsoft Windows systems, a
file is considered to be hidden if it
has been marked as such in the
filesystem.
As such you need to write platform specific code (JNI?) to hide a file.
Operating system detection code:
public class OperatingSystemUtilities
{
private static String operatingSystem = null;
private static String getOperatingSystemName()
{
if (operatingSystem == null)
{
operatingSystem = System.getProperty("os.name");
}
return operatingSystem;
}
public static boolean isWindows()
{
String operatingSystemName = getOperatingSystemName();
return operatingSystemName.startsWith("Windows");
}
public static boolean isMacOSX()
{
String operatingSystemName = getOperatingSystemName();
return operatingSystemName.startsWith("Mac OS X");
}
public static boolean isUnix()
{
return !isWindows();
}
}
Hiding the file code:
public static String hideFile(String filePath) throws IOException
{
Path path = Paths.get(filePath);
if (OperatingSystemUtilities.isWindows())
{
Files.setAttribute(path, "dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS);
return path.toString();
} else if (OperatingSystemUtilities.isUnix())
{
String filename = path.getFileName().toString();
if (filename.startsWith("."))
{
return path.toString();
}
// Keep trying to rename
while (true)
{
Path parent = path.toAbsolutePath().getParent();
Path newPath = Paths.get(parent + File.separator + "." + filename);
// Modify the file name when it exists
if (Files.exists(newPath))
{
int lastDotIndex = filename.lastIndexOf(".");
if (lastDotIndex == -1)
{
lastDotIndex = filename.length();
}
Random random = new Random();
int randomNumber = random.nextInt();
randomNumber = Math.abs(randomNumber);
filename = filename.substring(0, lastDotIndex) + randomNumber + filename.substring(lastDotIndex, filename.length());
continue;
}
Files.move(path, newPath);
return newPath.toString();
}
}
throw new IllegalStateException("Unsupported OS!");
}
Note that you have to pay attention to avoid a file name clash when renaming to hide the file on Unix. This is what the code implements despite it being unlikely.

Get size of folder or file

How can I retrieve size of folder or file in Java?
java.io.File file = new java.io.File("myfile.txt");
file.length();
This returns the length of the file in bytes or 0 if the file does not exist. There is no built-in way to get the size of a folder, you are going to have to walk the directory tree recursively (using the listFiles() method of a file object that represents a directory) and accumulate the directory size for yourself:
public static long folderSize(File directory) {
long length = 0;
for (File file : directory.listFiles()) {
if (file.isFile())
length += file.length();
else
length += folderSize(file);
}
return length;
}
WARNING: This method is not sufficiently robust for production use. directory.listFiles() may return null and cause a NullPointerException. Also, it doesn't consider symlinks and possibly has other failure modes. Use this method.
Using java-7 nio api, calculating the folder size can be done a lot quicker.
Here is a ready to run example that is robust and won't throw an exception. It will log directories it can't enter or had trouble traversing. Symlinks are ignored, and concurrent modification of the directory won't cause more trouble than necessary.
/**
* Attempts to calculate the size of a file or directory.
*
* <p>
* Since the operation is non-atomic, the returned value may be inaccurate.
* However, this method is quick and does its best.
*/
public static long size(Path path) {
final AtomicLong size = new AtomicLong(0);
try {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
#Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
size.addAndGet(attrs.size());
return FileVisitResult.CONTINUE;
}
#Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
System.out.println("skipped: " + file + " (" + exc + ")");
// Skip folders that can't be traversed
return FileVisitResult.CONTINUE;
}
#Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
if (exc != null)
System.out.println("had trouble traversing: " + dir + " (" + exc + ")");
// Ignore errors traversing a folder
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
throw new AssertionError("walkFileTree will not throw IOException if the FileVisitor does not");
}
return size.get();
}
You need FileUtils#sizeOfDirectory(File) from commons-io.
Note that you will need to manually check whether the file is a directory as the method throws an exception if a non-directory is passed to it.
WARNING: This method (as of commons-io 2.4) has a bug and may throw IllegalArgumentException if the directory is concurrently modified.
In Java 8:
long size = Files.walk(path).mapToLong( p -> p.toFile().length() ).sum();
It would be nicer to use Files::size in the map step but it throws a checked exception.
UPDATE:
You should also be aware that this can throw an exception if some of the files/folders are not accessible. See this question and another solution using Guava.
public static long getFolderSize(File dir) {
long size = 0;
for (File file : dir.listFiles()) {
if (file.isFile()) {
System.out.println(file.getName() + " " + file.length());
size += file.length();
}
else
size += getFolderSize(file);
}
return size;
}
For Java 8 this is one right way to do it:
Files.walk(new File("D:/temp").toPath())
.map(f -> f.toFile())
.filter(f -> f.isFile())
.mapToLong(f -> f.length()).sum()
It is important to filter out all directories, because the length method isn't guaranteed to be 0 for directories.
At least this code delivers the same size information like Windows Explorer itself does.
Here's the best way to get a general File's size (works for directory and non-directory):
public static long getSize(File file) {
long size;
if (file.isDirectory()) {
size = 0;
for (File child : file.listFiles()) {
size += getSize(child);
}
} else {
size = file.length();
}
return size;
}
Edit: Note that this is probably going to be a time-consuming operation. Don't run it on the UI thread.
Also, here (taken from https://stackoverflow.com/a/5599842/1696171) is a nice way to get a user-readable String from the long returned:
public static String getReadableSize(long size) {
if(size <= 0) return "0";
final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
int digitGroups = (int) (Math.log10(size)/Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups))
+ " " + units[digitGroups];
}
File.length() (Javadoc).
Note that this doesn't work for directories, or is not guaranteed to work.
For a directory, what do you want? If it's the total size of all files underneath it, you can recursively walk children using File.list() and File.isDirectory() and sum their sizes.
The File object has a length method:
f = new File("your/file/name");
f.length();
If you want to use Java 8 NIO API, the following program will print the size, in bytes, of the directory it is located in.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathSize {
public static void main(String[] args) {
Path path = Paths.get(".");
long size = calculateSize(path);
System.out.println(size);
}
/**
* Returns the size, in bytes, of the specified <tt>path</tt>. If the given
* path is a regular file, trivially its size is returned. Else the path is
* a directory and its contents are recursively explored, returning the
* total sum of all files within the directory.
* <p>
* If an I/O exception occurs, it is suppressed within this method and
* <tt>0</tt> is returned as the size of the specified <tt>path</tt>.
*
* #param path path whose size is to be returned
* #return size of the specified path
*/
public static long calculateSize(Path path) {
try {
if (Files.isRegularFile(path)) {
return Files.size(path);
}
return Files.list(path).mapToLong(PathSize::calculateSize).sum();
} catch (IOException e) {
return 0L;
}
}
}
The calculateSize method is universal for Path objects, so it also works for files.
Note that if a file or directory is inaccessible, in this case the returned size of the path object will be 0.
Works for Android and Java
Works for both folders and files
Checks for null pointer everywhere where needed
Ignores symbolic link aka shortcuts
Production ready!
Source code:
public long fileSize(File root) {
if(root == null){
return 0;
}
if(root.isFile()){
return root.length();
}
try {
if(isSymlink(root)){
return 0;
}
} catch (IOException e) {
e.printStackTrace();
return 0;
}
long length = 0;
File[] files = root.listFiles();
if(files == null){
return 0;
}
for (File file : files) {
length += fileSize(file);
}
return length;
}
private static boolean isSymlink(File file) throws IOException {
File canon;
if (file.getParent() == null) {
canon = file;
} else {
File canonDir = file.getParentFile().getCanonicalFile();
canon = new File(canonDir, file.getName());
}
return !canon.getCanonicalFile().equals(canon.getAbsoluteFile());
}
I've tested du -c <folderpath> and is 2x faster than nio.Files or recursion
private static long getFolderSize(File folder){
if (folder != null && folder.exists() && folder.canRead()){
try {
Process p = new ProcessBuilder("du","-c",folder.getAbsolutePath()).start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String total = "";
for (String line; null != (line = r.readLine());)
total = line;
r.close();
p.waitFor();
if (total.length() > 0 && total.endsWith("total"))
return Long.parseLong(total.split("\\s+")[0]) * 1024;
} catch (Exception ex) {
ex.printStackTrace();
}
}
return -1;
}
for windows, using java.io this reccursive function is useful.
public static long folderSize(File directory) {
long length = 0;
if (directory.isFile())
length += directory.length();
else{
for (File file : directory.listFiles()) {
if (file.isFile())
length += file.length();
else
length += folderSize(file);
}
}
return length;
}
This is tested and working properly on my end.
private static long getFolderSize(Path folder) {
try {
return Files.walk(folder)
.filter(p -> p.toFile().isFile())
.mapToLong(p -> p.toFile().length())
.sum();
} catch (IOException e) {
e.printStackTrace();
return 0L;
}
public long folderSize (String directory)
{
File curDir = new File(directory);
long length = 0;
for(File f : curDir.listFiles())
{
if(f.isDirectory())
{
for ( File child : f.listFiles())
{
length = length + child.length();
}
System.out.println("Directory: " + f.getName() + " " + length + "kb");
}
else
{
length = f.length();
System.out.println("File: " + f.getName() + " " + length + "kb");
}
length = 0;
}
return length;
}
After lot of researching and looking into different solutions proposed here at StackOverflow. I finally decided to write my own solution. My purpose is to have no-throw mechanism because I don't want to crash if the API is unable to fetch the folder size. This method is not suitable for mult-threaded scenario.
First of all I want to check for valid directories while traversing down the file system tree.
private static boolean isValidDir(File dir){
if (dir != null && dir.exists() && dir.isDirectory()){
return true;
}else{
return false;
}
}
Second I do not want my recursive call to go into symlinks (softlinks) and include the size in total aggregate.
public static boolean isSymlink(File file) throws IOException {
File canon;
if (file.getParent() == null) {
canon = file;
} else {
canon = new File(file.getParentFile().getCanonicalFile(),
file.getName());
}
return !canon.getCanonicalFile().equals(canon.getAbsoluteFile());
}
Finally my recursion based implementation to fetch the size of the specified directory. Notice the null check for dir.listFiles(). According to javadoc there is a possibility that this method can return null.
public static long getDirSize(File dir){
if (!isValidDir(dir))
return 0L;
File[] files = dir.listFiles();
//Guard for null pointer exception on files
if (files == null){
return 0L;
}else{
long size = 0L;
for(File file : files){
if (file.isFile()){
size += file.length();
}else{
try{
if (!isSymlink(file)) size += getDirSize(file);
}catch (IOException ioe){
//digest exception
}
}
}
return size;
}
}
Some cream on the cake, the API to get the size of the list Files (might be all of files and folder under root).
public static long getDirSize(List<File> files){
long size = 0L;
for(File file : files){
if (file.isDirectory()){
size += getDirSize(file);
} else {
size += file.length();
}
}
return size;
}
in linux if you want to sort directories then du -hs * | sort -h
You can use Apache Commons IO to find the folder size easily.
If you are on maven, please add the following dependency in your pom.xml file.
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
If not a fan of Maven, download the following jar and add it to the class path.
https://repo1.maven.org/maven2/commons-io/commons-io/2.6/commons-io-2.6.jar
public long getFolderSize() {
File folder = new File("src/test/resources");
long size = FileUtils.sizeOfDirectory(folder);
return size; // in bytes
}
To get file size via Commons IO,
File file = new File("ADD YOUR PATH TO FILE");
long fileSize = FileUtils.sizeOf(file);
System.out.println(fileSize); // bytes
It is also achievable via Google Guava
For Maven, add the following:
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.1-jre</version>
</dependency>
If not using Maven, add the following to class path
https://repo1.maven.org/maven2/com/google/guava/guava/28.1-jre/guava-28.1-jre.jar
public long getFolderSizeViaGuava() {
File folder = new File("src/test/resources");
Iterable<File> files = Files.fileTreeTraverser()
.breadthFirstTraversal(folder);
long size = StreamSupport.stream(files.spliterator(), false)
.filter(f -> f.isFile())
.mapToLong(File::length).sum();
return size;
}
To get file size,
File file = new File("PATH TO YOUR FILE");
long s = file.length();
System.out.println(s);
fun getSize(context: Context, uri: Uri?): Float? {
var fileSize: String? = null
val cursor: Cursor? = context.contentResolver
.query(uri!!, null, null, null, null, null)
try {
if (cursor != null && cursor.moveToFirst()) {
// get file size
val sizeIndex: Int = cursor.getColumnIndex(OpenableColumns.SIZE)
if (!cursor.isNull(sizeIndex)) {
fileSize = cursor.getString(sizeIndex)
}
}
} finally {
cursor?.close()
}
return fileSize!!.toFloat() / (1024 * 1024)
}

Categories