-1

How to handle forward slash in file path. My requirement is to read the contents of a csv file. But giving something like

String fileName = "MyNewFile.csv" is converting / to \. My operating system is Windows. I am using RAD IDE. Using File.Separator didn't help. Giving me a The system cannot find the path specified. error.

Tried this:

InputStream is = SampleClass.class.getClassLoader().getResourceAsStream("MyNewFile.csv");
BufferedReader br = new BufferedReader(new InputStreamReader(is));

And this:

The moment I try to escape / as \/ IDE shows me a compilation error Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )

Tried moving the file directly under the project(moved it out from src folder) and changed the code as br = new BufferedReader(new FileReader(csvFile)); But still seeing The system cannot find the file specified. error

Can anyone please help ???

jh314
  • 27,144
  • 16
  • 62
  • 82
Matt
  • 13
  • 5
  • What language are you programming in? Java? If so edit your question to have the appropriate tag. – Dijkgraaf Jun 03 '15 at 02:40
  • Can you elaborate on "causing issues"? What exactly isn't working as expected? – Mureinik Jun 03 '15 at 10:13
  • Try escaping it using \ as \/src\/MyNewFile.csv – Balaji Katika Jun 03 '15 at 10:15
  • 1
    What is your exact issue? What do you expect, what do you get? For sure, `String fileName = "/src/MyNewFile.csv"` will **not** convert anything - how did you prove this assumption? Also, make sure that you understand the different flavours of resource loading: http://stackoverflow.com/a/6608848/1611055 – Andreas Fester Jun 03 '15 at 10:18
  • Especially, note that ClassLoader resource paths are always absolute - there is no need to use a leading slash, and in certain situations it can be even wrong – Andreas Fester Jun 03 '15 at 10:24
  • Use `SampleClass.class.getResourceAsStream("MyNewFile.csv")`. If you call `.getResourceAsStream(...)` on a `ClassLoader` it expects the full path. – Christoffer Hammarström Jun 03 '15 at 14:56
  • Even under Windows a slash inside a file or directory name is problematic. Maybe a `File.listFiles` with a file name filter might be a hack. – Joop Eggen Jun 03 '15 at 14:58
  • This has nothing to do with slashes. Forward slashes will always work and there is no reason to use backslashes. The problem is because `Class.getResourceAsStream()` accepts relative paths and `ClassLoader.getResourceAsStream()` only accepts absolute paths. – Christoffer Hammarström Jun 03 '15 at 15:05

2 Answers2

0
               SampleClass.class
                .getClassLoader().getResourceAsStream(
                        "com/xyz/abc/" + MyNewFile.csv);

You can use the above give package name where the where the csv is located

SarthAk
  • 1,628
  • 3
  • 19
  • 24
0

Placing the file in a temporary directory outside my project solved the issue. Below is how I am reading it. But this is just a temporary solution

br = new BufferedReader(new FileReader("c:\\temp\\MyNewFile.csv"));

But I would still need to know how to read the contents if it is under the project

Matt
  • 13
  • 5