PDA

View Full Version : xcode/java fileIO




stoid
May 18, 2006, 12:23 AM
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.

BufferedReader in = new BufferedReader(new FileReader(new File("PANTONEswatches.data")));

any ideas (besides learning Objective-C :rolleyes: )?

Thanks,
BK



gekko513
May 18, 2006, 12:30 AM
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
May 18, 2006, 12:50 AM
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
May 18, 2006, 01:42 AM
Perhaps try line by line instead of condensing it into one? Not sure if it would make a difference but you never know.

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
May 18, 2006, 05:39 AM
You need to use the full unix path of the file. (eg "~/Desktop" for the desktop)
you use 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
May 18, 2006, 05:46 AM
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
May 18, 2006, 08:47 AM
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.

gekko513
May 18, 2006, 09:02 AM
What's your current code and what exactly does the exception say?

stoid
May 18, 2006, 09:22 AM
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
May 18, 2006, 09:29 AM
Reading args from the command line is easy:

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:
user@machine$ java Foo ~/files/dataset

gekko513
May 18, 2006, 09:36 AM
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) {
e.printStackTrace();
System.out.println("Error in Loading Data file.");
}
What does the exception say if you print it as shown above?

stoid
May 18, 2006, 09:41 AM
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
May 18, 2006, 09:42 AM
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
May 18, 2006, 09:53 AM
And you're sure there's a file named PANTONEswatches.data on your desktop? If so, that's very, very strange indeed.

yep

gekko513
May 18, 2006, 09:58 AM
yep
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
May 18, 2006, 04:03 PM
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
May 18, 2006, 09:34 PM
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
May 18, 2006, 09:55 PM
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 (http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/index.html#//apple_ref/doc/uid/TP30001163) and Programming in Objective-C (http://www.amazon.com/gp/product/0672325861/qid=1143893107/sr=2-1/ref=pd_bbs_b_2_1/103-8462982-0472631?s=books&v=glance&n=283155) by Stephan Kochan. Most folks agree the best Cocoa intro book is Cocoa Programming for Mac OS X (http://www.amazon.com/gp/product/0321213149/ref=pd_bbs_null_1/103-8462982-0472631?s=books&v=glance&n=283155) by Aaron Hillegass. For Xcode specifically, try Step into Xcode : Mac OS X Development (http://www.amazon.com/gp/product/0321334221/qid=1148007244/sr=2-1/ref=pd_bbs_b_2_1/104-1610895-1583942?s=books&v=glance&n=283155).

Palad1
May 19, 2006, 12:14 AM
Why don't you bundle your file in a resource and stuff everything in a Jar then?

mbabauer
May 24, 2006, 10:10 PM
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
May 25, 2006, 10:28 AM
//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).