简体   繁体   English

用Java导出到CSV文件

[英]Exporting to a CSV File in Java

I am trying to export data using CSV. 我正在尝试使用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. 感谢任何可以提供帮助的人。

You usually cannot write/create files in root C:\\ folder because of permission issues. 由于权限问题,通常无法在根C:\\文件夹中写入/创建文件。

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. 您还可以写入您的temp文件夹。

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/ 如果要在“根”目录中创建文件,请按照以下说明进行操作: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. 免责声明,由于我没有Windows8,因此无法测试。

This access denied message is from the Operating System to Java. 此拒绝访问消息是从操作系统到Java。 So, the java program is not being allowed (by the Operating system) to access that file. 因此,不允许Java程序(由操作系统)访问该文件。 This could be because of multiple reasons, including 这可能是由于多种原因,包括

  • The user running the java program does not have access to C:\\ 运行Java程序的用户无权访问C:\\
  • The user running the java program does not have access to C:\\test.csv 运行Java程序的用户无权访问C:\\test.csv
  • test.csv is being edited from another program. 正在从另一个程序编辑test.csv

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+). 我将从将文件写入当前目录(运行程序的位置)或用户主目录(在Win 7+上为C:\\users\\userName\\AppData )开始。 User home can be got programatically by System.getProperty("user.home") 可以通过System.getProperty("user.home")以编程方式获取用户主目录

Also, C:\\ exists only on Windows, so hardcoding this is not a very good idea. 另外, C:\\仅在Windows上存在,因此对它进行硬编码不是一个好主意。 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) 使用user.home属性或外部化文件位置(从属性文件获取文件名。这样,您可以更改不同操作系统的位置)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM