0

I have a CSV file that I copy in my work package, I want read it from my package not a path on my PC in order to not change the link every time I change my PC but I do not know how do this, I tried a solution that did not work for me this is my code :

public static void main(String[] args) {
    CSV obj = new CSV();
    obj.run();
}

public void run() {
    String csvFile = "C:/Users/Intervalle tech/Desktop/SV.csv";
    BufferedReader br = null;
    String line = "";
    //URL url = Thread.currentThread().getContextClassLoader().getResource("/BocOperations/src/operations/SV.csv");
    //File file = new File(url.getPath());

    try {
      br = new BufferedReader(new FileReader(csvFile));
      while ((line = br.readLine()) != null) {
          // use comma as separator
          // String[] Element = line.split(cvsSplitBy);
          System.out.println( line );
      }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
jotik
  • 17,044
  • 13
  • 58
  • 123
khadi8
  • 13
  • 1
  • 2
  • 4
  • You can load the CSV file from the resources folder. It is at the same root than your java folder, and has to have the same structure (packages). Put your CSV file here. Then load it with the code in the answer below, without path (not needed because it is now a resource file). It will be loaded as a resource. Just make sure that when you package your java project, it'll include resources fodler. – Aeldred Apr 11 '16 at 10:13
  • @Aeldred It is need to have a corresponding builder for `resources` folder, for an example Maven builder (in Eclipse). – v.ladynev Apr 11 '16 at 10:16
  • possible duplicate http://stackoverflow.com/questions/30275143/java-io-filenotfoundexception-the-system-cannot-find-the-path-specified-cvs-fi/30275207#30275207 – ELITE Apr 11 '16 at 10:16
  • @v.ladynev it's not a bad thing to have a Maven application for these purpose ;) – Aeldred Apr 11 '16 at 11:27

1 Answers1

1

This line is incorrect

URL url = Thread.currentThread().getContextClassLoader().getResource("/BocOperations/src/operations/SV.csv");

If you use getResource() method of a ClassLoader you shouldn't add a leading / and you need to specify a path begin from of a root package. So if SV.csv resides in the operations package (and operations in the root package)

URL url = Thread.currentThread().getContextClassLoader()
  .getResource("operations/SV.csv");

Please, don't use custom implementations to read CSV files

opencsv

Apache Commons CSV

An example of using opencsv

CSVReader reader = new CSVReader(
                    new FileReader("c:/xxx.csv"));
// Read all rows at once
List<String[]> allRows = reader.readAll();

Read a file from an URL

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

You can consider to specify an encoding of a reading file.

v.ladynev
  • 19,275
  • 8
  • 46
  • 67