1

In my project i want to read data from several textfiles, therefore I have organized them into folders likte following:

  1. textfiles
    • folder1
      • subfolder1
        • file.txt
      • subfolder2
    • folder2
      • subfolder3

My problem is that I don“t know where to put my folder or how to referance my textfiles. I have read Where to put a textfile I want to use in eclipse? and have a setup looking like this:
Folder location:
workspace/porjectX/src/textfiles
Code:

FileInputStream fis= new FileInputStream("/textfiles/folder1/subfolder1/file.txt"); 

The result i get is a "FileNotFoundException"...

I have checked spelling, everything is lowercase and with no spaces. Any thoughts?

Thanks for any help!

Community
  • 1
  • 1
Ludste
  • 39
  • 6

1 Answers1

0

First you have to make sure the folder testfiles is on your build path (Project->Properties->Java Build Path). Then you can get the inputstream for the file the following way:

this.getClass().getClassLoader().getResourceAsStream("folder1/subfolder1/file.txt");

See also: getResourceAsStream() vs FileInputStream

Community
  • 1
  • 1
Christian
  • 4,543
  • 1
  • 22
  • 31