Domanda

My goal is to write a csv file to a specific location on my computer. The problem is that I keep getting the FileNotFoundException error when I compile my program. I know my where my problem is.

Its here,

FileWriter fw = new FileWriter("://Users/michaelrichards/NetBeansProjects/UniProject/src");

Can someone please tell me the format when typing out a path in terminal.

import java.io.*;
public class WriteToFile
{
public static void main(String[]args) throws IOException
{
    FileWriter fw = new FileWriter("://Users/michaelrichards/NetBeansProjects/UniProject/src");
    BufferedWriter bw = new BufferedWriter(fw);

    String myArray[] = {"one", "two", "three"};
    String writableString = "";

    for(String item : myArray)
    {
        writableString += item + ",";
    }

    writableString = writableString.substring(0, writableString.length() - 1);

    bw.write(writableString);
    bw.close();
}
}
È stato utile?

Soluzione

I am not a Mac user, but as far as I know, your path should be /Users/michealrichards/.... You could also use the Java system property user.home to get the home directory of the current user, independent of the platform:

String path = System.getProperty("user.home") + "/NetBeansProjects/UniProject/src";

Edit: I just noticed that you are trying to open a directory (src) as a file! This won't work anyway. You have to pass a path to a file to the FileWriter constructor.

Altri suggerimenti

OS X is a Unix based system so your path should be

/Users/michaelrichards/NetBeansProjects/UniProject/src

If you are using Windows use like D://Folder/folder/raghu.txt

in linux /home/(user name)/Desktop/raghu.txt

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top