-1

I know this a really simple question, but I am doing something wrong and it's driving me crazy! I have a file named input.csv that I want to convert to a tab delimited file.

This is what I have, but it is saying no such file or directory. The file is saved on my desktop, am I just missing something very simple? any tips would help!

public class Main {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner sc = new Scanner(new File("input.csv"));
    }
}
Teo
  • 3,143
  • 2
  • 29
  • 59
Rassisland
  • 169
  • 1
  • 3
  • 16
  • 2
    It's a path issue pure and simple. You're not using the correct ***relative*** path to the file. .... Or if it's in a jar file, then you need to obtain it as a resource, not as a file. – Hovercraft Full Of Eels Jan 24 '16 at 23:19
  • Test the path to the user's directory, put in `System.out.println(System.getProperty("user.dir"));`, and then use this information to figure out what the relative path should be based on. – Hovercraft Full Of Eels Jan 24 '16 at 23:27
  • 1
    I had answered something similar [here](http://stackoverflow.com/questions/34941269/where-the-file-will-be-located-while-using-io-streams-in-java-in-eclipse-ide/34941501#34941501). It should solve your problem. Let me know if it doesn't. – Debosmit Ray Jan 24 '16 at 23:28

1 Answers1

-1

Input the full path to your file (C:\Users\youruser\Desktop\thefile.csv). It's not throwing a permissions error so you just need to tell it where it is.

Will
  • 810
  • 6
  • 21
  • 1
    no, don't use the full path, use the relative path if at all possible. – Hovercraft Full Of Eels Jan 24 '16 at 23:19
  • Otherwise you're guaranteeing that the code will fail on all but one computer -- not good. – Hovercraft Full Of Eels Jan 24 '16 at 23:26
  • Sure, in this particular case though I assumed it was a one off utility. If it's a user-facing system most likely you would take some sort of input as to where the file is. – Will Jan 24 '16 at 23:27
  • See my comment -- he needs to find his user directory and then base the relative path on this. – Hovercraft Full Of Eels Jan 24 '16 at 23:28
  • In that particular case I'm not sure how much good your answer is vs mine, he's talking about a CSV file so I can't imagine it's particularly likely to be something that multiple users have in the same relative location. But yes, that would be best practice for something that not only yourself is using. – Will Jan 24 '16 at 23:30