Java help with reading from a file

Discussion in 'Software' started by Sasquat, Jan 16, 2006.

  1. Sasquat

    Sasquat Private E-2

    Hi guys,

    Please could someone advise me on inpuuting from a file?

    I'm a beginner and the thing that I can't get past is

    File inputFile = new File("D:\\Academic\\Uni Work\\Salford MSc\\Java Programming\\sudoku\\99easy.txt");

    FileReader in = new FileReader(inputFile);


    The problem is that it just keeps saying that it can't find the file - I have used the whole path and just the name of the file. I am using BlueJ so am working with a "project" and the text file is within this folder(the same as the .java file containing this).

    Thanks for any help
    Sarah
     
  2. MutD

    MutD Specialist

    So you are trying to access a txt file called 99easy.txt?

    If you have put this within the same directory as the other class files then all you need to enter is "99easy.txt" and not the entire directory of the file. So the line would be;

    File inputFile = new File("99easy.txt");

    If you had to put the entire directory in then there wouldn't be much point moving it to the same location as the other class files.

    Hope this helps
     
  3. Sasquat

    Sasquat Private E-2

    Actually I have found a solution.

    I thought exactly the same as you but that did not work and confused me for a good while. The solution is that in the path directory I have directories with spaces in the names and Java was not happy with this - as soon as I chnaged the spaces to underscores it could find the file!
     
  4. MutD

    MutD Specialist

    Mmm, strange.

    When I'm reading from a text file I always use the mehod I suggested, well Im glad you got it working anyway.

    I see your at Salford uni, Im at Manchester. Small world Isn't it!
     
  5. Sasquat

    Sasquat Private E-2

    Maybe you don't use spaces in your directory names? I have just gotten out of that habit since I stopped using Linux - I have found similar problems with that!

    I am at Salford, although I was at Manchester.........that's a long story though!
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds