Search code examples
javalinuxfilewriter

My java code doesn't write a file on Linux


I have some code for entering my expenses. However, the program doesn't write a text file to a specified folder on Linux (Ubuntu 16.04). The program used to write normally the file in the folder where the code was. Now, I'm trying to write the file into a specific folder, but it fails (even though I set permission for all the users to read, write and execute, the file is not created it.) I want to add that the code makes the directory but fails to write a file inside the specific directory . Any help or guidance would be appreciated.

import java.io.* 
import java.util.EnumSet;
import java.util.Set;


public class Create_File {

private Services services = new Services();

final Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxrwxrwx");


public void create_File(String content) {

    int product_id =0;

    String headline = "XXXX" ;


    String username = System.getProperty("user.home");
    String dataFolder = System.getProperty("user.home") + "/My_Data/Budget_backup";

    System.out.println(username);


    File directory = new File(dataFolder+File.separator+"Budget_app_data_expenses");
    String name_of_file = "budget_back_up.txt";

    File file = new File(directory+File.separator+ name_of_file);
    System.out.println(file.toString());

    Path path = FileSystems.getDefault().getPath(dataFolder, name_of_file );



    if (Files.notExists(path, new LinkOption[]{LinkOption.NOFOLLOW_LINKS})){
        try {
            Files.createDirectories(
                    Paths.get(dataFolder),
                    PosixFilePermissions.asFileAttribute(perms));
            //directory.mkdirs();

            //file.createNewFile();
                 product_id = 1;

                System.out.println("file created");
                FileWriter writer = new FileWriter(file, true);
                BufferedWriter bw = new BufferedWriter(writer);
                bw.write(headline);
                bw.close();

        }catch(IOException ex){
            ex.printStackTrace();
        }

    }else {
         product_id = services.max_id();

        System.out.println("The directory is already on the computer");
        try {
            FileWriter writer = new FileWriter(file, true);
            BufferedWriter bw = new BufferedWriter(writer);
            bw.write("\n"+ "("+ product_id + " , " +content);
            bw.close();
        }catch(IOException ex){
        ex.printStackTrace();
    }



    }
}

Solution

  • This error is probably due to no directory present with the given path.It needs to be created which can be done using mkdirs()

    /home/USER/My_Data/Budget_backup/Budget_app_data_expenses/bu‌​dget_back_up.txt (No such file or directory) 
    

    Once a directory is created initially then you should not face any errors

    Uncommenting directory.mkdirs() does work for me both in Mac and Fedora

     Files.createDirectories(
                        Paths.get(dataFolder),
                        PosixFilePermissions.asFileAttribute(perms));
               directory.mkdirs();
    

    Also Take a look at your code

     String dataFolder = System.getProperty("user.home") + "/My_Data/Budget_backup";
    
         File directory = new     File(dataFolder+File.separator+"Budget_app_data_expenses");
    
        File file = new File(directory+File.separator+ name_of_file);
    
        Path path = FileSystems.getDefault().getPath(dataFolder, name_of_file );
    

    In the path object you are trying to find the file in the incorrect directory /My_Data/Budget_backup"

    As you create the file in /My_Data/Budget_backup/Budget_app_data_expenses

    In this case your if condition searches in the incorrect directory for the file and the If condition will always be true as your path is incorrect