xcode/java fileIO

Discussion in 'Mac Programming' started by stoid, May 17, 2006.

  1. macrumors 601

    stoid

    Joined:
    Feb 17, 2002
    Location:
    So long, and thanks for all the fish!
    #1
    Hey, I'm having difficulty getting a data file to load into my java based Cocoa app in Xcode.

    The data file is located in the same directory as the java source file. The line that would normally work in a regular java project is throwing me an IOException.

    Code:
    BufferedReader in = new BufferedReader(new FileReader(new File("PANTONEswatches.data")));
    any ideas (besides learning Objective-C :rolleyes: )?

    Thanks,
    BK
     
  2. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #2
    When you try to open a file using just its file name, it will try to look for a file with that name in the directory you're runnning from. That doesn't have to be where the source file is at all, it just can be if you're working from the Terminal.

    When a Java program is run with Xcode I think the running directory is set to your user's home directory.
     
  3. thread starter macrumors 601

    stoid

    Joined:
    Feb 17, 2002
    Location:
    So long, and thanks for all the fish!
    #3
    First off, thanks!


    but...

    I tried adjusting the file name so it was coming from my user folder, and I even dragged to file name from the Xcode file browser and from Finder into the java code (Xcode and Finder both gave me a file://localhost/... filename). None of those worked.




    Other ideas?
     
  4. tag
    macrumors 6502a

    tag

    Joined:
    Apr 29, 2005
    Location:
    PA, US
    #4
    Perhaps try line by line instead of condensing it into one? Not sure if it would make a difference but you never know.

    Code:
    File file = new File("PANTONEswatches.data");
    FileReader fileReader = new FileReader(file);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    And these are inside a try/catch syntax block correct? I don't know why but I remember that being kind of necessary, though I could be wrong. (I haven't played around with java for a while so I guess I'm below java noob on the ranking system now, but figured I'd try to help out at least :p ).

    Oh too, you said you tried using file://localhost/whateverlocation. That got me thinking, I threw a text document into Safari and got this instead (file:///Users/rob/Documents/get.rtf) perhaps ditch the localhost part? or just start out from the root directory (/Users/you/whereever). Just a thought.
     
  5. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #5
    You need to use the full unix path of the file. (eg "~/Desktop" for the desktop)
    you use
    Code:
    File f=new File("~/Desktop/testFile.data");
    FileInputStream fs=new FileInputStream(f);
    BufferedReader br=new  BufferedReader(new InputStreamReader(fs));
    f=null;
    fs=null;
    
    this should work (in theory, though i have alway's used the Scanner class to read in files, i have used the second half to read in data from the command line, and the first half to read in objects.
     
  6. macrumors 6502a

    Palad1

    Joined:
    Feb 24, 2004
    Location:
    London, UK
    #6
    Better yet, don't hardcode pathnames and use arguments / properties files.
    This will save you a recompile each time you want to debug ;)

    Cheers,
    Florian

    ps: Has anyone checked-out Google's ajax api? I'll give it a go this weekend on my G4 :)
     
  7. thread starter macrumors 601

    stoid

    Joined:
    Feb 17, 2002
    Location:
    So long, and thanks for all the fish!
    #7
    Thanks for the continued input.

    I tried loading from the desktop and using the UNIX file pathname. Still getting the IOException.

    All I want to do is load a dataset of about 2000 lines, and I want to put that data in another file so it's not bloating out my main procedure. The file does not need to be modified from the user's perspective, it's just basic operating data for the program.

    I like the arguments / properties idea, but don't know where to start looking for information on building that.
     
  8. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #8
    What's your current code and what exactly does the exception say?
     
  9. thread starter macrumors 601

    stoid

    Joined:
    Feb 17, 2002
    Location:
    So long, and thanks for all the fish!
    #9
    Code:
    try {
    	File f=new File("~/Desktop/PANTONEswatches.data");
    	FileInputStream fs=new FileInputStream(f);
    	BufferedReader br=new  BufferedReader(new InputStreamReader(fs));
    	f=null;
    	fs=null;
    
    	br.close();
    
    } catch (IOException e) {
    	System.out.println("Error in Loading Data file.");
    }
    And when I run the code, I get the "Error in Loading Data file." out in the run log window.
     
  10. macrumors 6502a

    Palad1

    Joined:
    Feb 24, 2004
    Location:
    London, UK
    #10
    Reading args from the command line is easy:
    PHP:
    public class Foo{
    // sorry, I'm doing c# right now, I may get the wrong signature for the method...
    public static void main(String[] args){
      if(
    args.length==0){
        throw new 
    Exception("Please specify a path as first argument");
      }

      
    string suppliedPath=args[0]; 
      
    Foo me=new Foo();  
      
    me.loadData(suppliedPath);

    }

    private 
    void loadData(String suppliedPath){

      
    File file=new File(suppliedPath);
      
    // does it exist?
      
    if(!file.exists){
       throw new 
    FileNotFoundException("Not found: "+file.getAbsolutePath());
      }

      
    BufferedReader br=new  BufferedReader(FileInputStream(file));
      try{
        
    // dostuff
      
    }finally{
        if(
    br!=null){ // never know ;)
          
    br.close();
        }
      }
    }
    And invoke your app from the terminal:
    PHP:
    user@machinejava Foo ~/files/dataset 
     
  11. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #11
    Code:
    try {
    	File f=new File("~/Desktop/PANTONEswatches.data");
    	FileInputStream fs=new FileInputStream(f);
    	BufferedReader br=new  BufferedReader(new InputStreamReader(fs));
    	f=null;
    	fs=null;
    
    	br.close();
    
    } catch (IOException e) {
            [B]e.printStackTrace();[/B]
    	System.out.println("Error in Loading Data file.");
    }
    What does the exception say if you print it as shown above?
     
  12. thread starter macrumors 601

    stoid

    Joined:
    Feb 17, 2002
    Location:
    So long, and thanks for all the fish!
    #12
    OK, so it's specifically a FileNotFoundException. (No such file or directory).

    Why I didn't think of putting in a stack trace is beyond me :rolleyes: . it's been awhile since I've done serious java code.

    I'm still open to suggestions about a better way to tackle this problem of building a large array without having to load a data file based on a path that will change depending on the computer the app is loaded on. Would it be best to create a class specifically for loading the data into the array?
     
  13. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #13
    And you're sure there's a file named PANTONEswatches.data on your desktop? If so, that's very, very strange indeed.
     
  14. thread starter macrumors 601

    stoid

    Joined:
    Feb 17, 2002
    Location:
    So long, and thanks for all the fish!
    #14
    yep
     

    Attached Files:

  15. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #15
    And does the same thing happen if you write the whole path? It's not certain that the JRE bothers to expand the '~'.

    "/Users/<you>/Desktop/PANTONEswatches.data"
     
  16. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #16
    Also, is it possible there's another hidden extension that you're not seeing? For example, maybe the file is really named "PANTONEswatches.data.txt", but the Finder is hiding the last extension. Do a Get Info on the file in the Finder. You can also drag a file from the Finder right into your Xcode editor window and it'll insert the full UNIX path. Also, I forget how Java works exactly, but I know I used to have a lot of problems with the Java PATH environment variable need to be set in order to use files...is that still the case?
     
  17. thread starter macrumors 601

    stoid

    Joined:
    Feb 17, 2002
    Location:
    So long, and thanks for all the fish!
    #17
    OK, so it was that I had to put /Users/<me>/Desktop/PANTONEswatches.data to get the program to find the path. However, I want it to be contained within the compiled application, so it looks like I'm going to have to make a data class to create the array.

    I think that trying to stick to my java roots is only going to dampen my Mac OS X app development. What is a good book that I can start with to learn the ins and outs of Xcode, and whatever Apple's preferred language is. Preferably something that will speak in a programmer to programmer language and not in techno baby talk.

    Thanks again all,
    BK
     
  18. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #18
    If you really want to do Mac-only development I would strongly recommend you use Objective-C and Cocoa and not Java. Nothing against Java, but your life will be much, much easier in that scenario.

    For Objective C, start with Apple's Objective-C Programming Language and Programming in Objective-C by Stephan Kochan. Most folks agree the best Cocoa intro book is Cocoa Programming for Mac OS X by Aaron Hillegass. For Xcode specifically, try Step into Xcode : Mac OS X Development.
     
  19. macrumors 6502a

    Palad1

    Joined:
    Feb 24, 2004
    Location:
    London, UK
    #19
    Why don't you bundle your file in a resource and stuff everything in a Jar then?
     
  20. macrumors regular

    Joined:
    Feb 14, 2006
    #20
    If the file will always be the same, you *could* put it in the jar and load it as a resource. Here are a couple of links that show various ways to do this:

    http://www.javaworld.com/javaworld/javatips/jw-javatip49.html
    http://forum.java.sun.com/thread.jspa?threadID=589495&messageID=3066438
     
  21. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #21
    Code:
    
    //Read file in from classpath
    InputStream in = getClass( ).getResourceAsStream("PANTONEswatches.dat");
    
    BufferedReader bReader;
    
    if (in != null) {
    	//Create the BufferedReader
    	 bReader = new BufferedReader(new InputStreamReader(in));
    } else {
    	//handle your error...
    }
    
    when you build the jar file, make sure the file path in the code matches the location of the dat file in the folder strucuture of your jar file (no leading slash required).
     

Share This Page