Colin Koenig
Colin Koenig

Reputation: 65

Exporting to a CSV File in Java

I am trying to export data using CSV. I am not an advanced coder so it would be nice to keep that in mind while helping ;)

I am very confused on why it is denying my access and have not much of an idea on how to fix it.

package Testing;

import java.io.FileWriter;
import java.io.IOException;

public class Testing
{
   public static void main(String [] args)
   {
       generateCsvFile("c:\\test.csv"); 
   }

   private static void generateCsvFile(String sFileName)
   { 
    try
    {
        FileWriter writer = new FileWriter(sFileName);

        writer.append("DisplayName");
        writer.append(',');
        writer.append("Age");
        writer.append('\n');

        writer.append("MKYONG");
        writer.append(',');
        writer.append("26");
            writer.append('\n');

        writer.append("YOUR NAME");
        writer.append(',');
        writer.append("29");
        writer.append('\n');

        writer.flush();
        writer.close();
    }
    catch(IOException e)
    {
         e.printStackTrace();
    } 
    }
}

I am getting the fallowing error:

java.io.FileNotFoundException: c:\test.csv (Access is denied)
    at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileWriter.<init>(Unknown Source)
at Testing.Testing.generateCsvFile(Testing.java:17)
at Testing.Testing.main(Testing.java:10)

Thanks to anyone that could help.

Upvotes: 1

Views: 7234

Answers (2)

Nivas
Nivas

Reputation: 18344

This access denied message is from the Operating System to Java. So, the java program is not being allowed (by the Operating system) to access that file. This could be because of multiple reasons, including

  • The user running the java program does not have access to C:\
  • The user running the java program does not have access to C:\test.csv
  • test.csv is being edited from another program.

I would start with writing the file to the current directory (from where the program is being run) or to user home (which is C:\users\userName\AppData on Win 7+). User home can be got programatically by System.getProperty("user.home")

Also, C:\ exists only on Windows, so hardcoding this is not a very good idea. Use the user.home property or externalize the file location (get the file name from a properties file. This way you can change the location for different operating systems)

Upvotes: 2

cevaris
cevaris

Reputation: 5794

You usually cannot write/create files in root C:\ folder because of permission issues.

Though you can write/create files in your home directory at will. The following solution should work across operating systems.

generateCsvFile(System.getProperty("user.home") + "test.csv"); 

Yo can also write to your temp folder.

generateCsvFile(System.getProperty("java.io.tmpdir") + "test.csv"); 

In the case of wanting to create a file in the Root directory, follow these instructions http://www.mkyong.com/computer-tips/cant-create-file-in-the-c-drive-root-directory-windows-8/

Disclaimer, cannot test since I do not have Windows8.

Upvotes: 2

Related Questions