Create a hidden java folder?

4

I have a method that receives a route to create a file in this and of course the directory I do it in this way.

    public static File createTempFile(String exteString, String nombre, String path) throws IOException {
        try {
            File file = new File(path + "\" + nombre + exteString);
//--
            Path toCreatePath = Paths.get(file.toURI());
            if (!Files.exists(toCreatePath)) {
                Files.createDirectories(toCreatePath);
            }
            return file;
        } catch (IOException e) {
            System.out.println(ReportLog.getErrorBuilder(new Object(), e));
        }
        return null;
    }

My problem is that I need somehow to hide this directory , that is, it can not be seen by the user , since it is going to contain secret information

  

(not explicitly confidential information only information not   redundant for the user)

I think it's possible since the class Files contains the method isHidden but I still do not know how to tell the directory to be hidden.

    
asked by theboshy 05.10.2017 в 00:26
source

3 answers

2

I give you an example with java.nio that I found in this site :

package com.resolvethis.nio;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;

public class MakeFileHidden {

    public static void main(String[] args) {
        try {
            Path path = Paths.get("C:\resolvethis", "testfile.txt");
            Boolean hidden = (Boolean) Files.getAttribute(path, "dos:hidden", LinkOption.NOFOLLOW_LINKS);
            if (hidden != null && !hidden) {
                Files.setAttribute(path, "dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS);
                System.out.println("File is now hidden!");
            }
        } catch (IOException ex) {
            System.err.println("Things went wrong: " + ex.getMessage());
            ex.printStackTrace();
        }
    }
}

We will have to be careful if what you want is multiplatform.

    
answered by 05.10.2017 / 00:40
source
1

That's right, you can indicate the :hidden attribute by using the setAttribute () in the documentation is the example for this, applied to its function would only be

public static File createTempFile(String exteString, String nombre, String path) throws IOException {
        try {
            File file = new File(path + "\" + nombre + exteString);
            Path toCreatePath = Paths.get(file.toURI());
            if (!Files.exists(toCreatePath)) {
                Files.createDirectories(toCreatePath);
                // Añadimos el atributo a la carpeta
                Files.setAttribute(file.toPath(), "dos:hidden", true);
            }
            return file;
        } catch (IOException e) {

        }
        return null;
    }
    
answered by 05.10.2017 в 00:41
1

isHidden () only returns a value Boolean to define if your attribute is present

Another option besides the one commented by @ Dev.Joel (Java 7 +):

 Files.setAttribute(file.toPath(), "dos:hidden", true);

is through the command attrib using Runtime.exec() (Java 6 -):

Process p = Runtime.getRuntime().exec("attrib +h " + rutaDirectorio);
p.waitFor();
    
answered by 05.10.2017 в 00:42