Results 1 to 2 of 2
  1. #1
    Noceo is offline Member
    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0

    Question Reading files with 'Scanner' class

    Hey,

    I'm struggling a bit with reading files using the 'Scanner' class. The problem is that I can't seem to figure out how to get it to read the file without using the full path, which seems somehow redundant. I placed both my java file and the file to be read in the same folder. I'm using Eclipse if that has anything to say?

    Hope you can help. Happy Monday!!!

    - Jake

    Edit:
    This is my code btw:
    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class ImageAnalysis {
    private String noRows, noCols;
    
    	// Constructs image as array
    	public ImageAnalysis(String filename) throws FileNotFoundException {
    		Scanner readImage = new Scanner(new File("C:/Users/Jakob/Dropbox/DTU/7_Semester/02101 Indledende Programmering/Eclipse Projects/Assignment 2/src/image001.ipi"));
    		readImage.useLocale(Locale.ENGLISH);
    		noRows = readImage.next();
    		noCols = readImage.next();
    		readImage.close();
    	}
    	
    	public void printRows(){
    	System.out.println(noRows);	
    	}
    	
    }
    Last edited by Noceo; 11-05-2012 at 09:23 PM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Reading files with 'Scanner' class

    If you are trying to use a relative path, then the file must be in the correct location relative to where the app is being executed.

    For example, if running from the command line, the java root is usually just outside the top level package. If from and IDE like eclipse or netbeans, then it's in the project folder itself. If it's a jar file, then it could be different depending on how you're trying to load the file.

Similar Threads

  1. File reading with Scanner
    By mik in forum New To Java
    Replies: 2
    Last Post: 08-31-2012, 09:44 AM
  2. Project Advice (txt files and scanner class)
    By Fargo94 in forum New To Java
    Replies: 11
    Last Post: 05-09-2012, 12:38 AM
  3. a little help with reading in files, and class usage =/
    By basketball8533 in forum New To Java
    Replies: 10
    Last Post: 10-08-2010, 06:01 AM
  4. Reading pattern using Scanner class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 12:02 PM
  5. Reading a line from console using Scanner class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 11:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •