Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

stoid

macrumors 601
Original poster
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
 

gekko513

macrumors 603
Oct 16, 2003
6,301
1
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.
 

stoid

macrumors 601
Original poster
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?
 

tag

macrumors 6502a
Apr 29, 2005
918
9
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.
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
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.
 

Palad1

macrumors 6502a
Feb 24, 2004
647
0
London, UK
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 :)
 

stoid

macrumors 601
Original poster
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.
 

stoid

macrumors 601
Original poster
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.
 

Palad1

macrumors 6502a
Feb 24, 2004
647
0
London, UK
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@machine$ java Foo ~/files/dataset
 

gekko513

macrumors 603
Oct 16, 2003
6,301
1
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?
 

stoid

macrumors 601
Original poster
gekko513 said:
What does the exception say if you print it as shown above?

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?
 

gekko513

macrumors 603
Oct 16, 2003
6,301
1
stoid said:
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 still open to suggestions about a better way to tackle this problem of building a large dataset.
And you're sure there's a file named PANTONEswatches.data on your desktop? If so, that's very, very strange indeed.
 

stoid

macrumors 601
Original poster
gekko513 said:
And you're sure there's a file named PANTONEswatches.data on your desktop? If so, that's very, very strange indeed.

yep
 

Attachments

  • Picture 1.png
    Picture 1.png
    12.8 KB · Views: 307

gekko513

macrumors 603
Oct 16, 2003
6,301
1
stoid said:
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"
 

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
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?
 

stoid

macrumors 601
Original poster
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
 

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
stoid said:
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.
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.
 

mbabauer

macrumors regular
Feb 14, 2006
105
0
stoid said:
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?

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
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
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).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.