File input
I've hit a roadblock in file input; I'm coding in Eclipse, and I've imported a file "db.m3db" for use as a resource. The problem is, I can't seem to figure out how to load it for a FileInputStream. I've currently tried stuff like getClass().getResourceAsStream(), but that gives me a type problem between InputStream and FileInputStream.
Also, while I'm asking one question, I'd like to ad another I'm curious about: I want to finally compile this program to run under Windows XP and OS X. It has to be able to read files from a directory substructure. What type of file am I going to be "exporting" as? So far for multiplatform software I've seen .jar files, but those seem to be sort of "all-in-one" compilations.
[737 byte] By [
thefila] at [2007-11-27 11:45:25]

in order to read from a file, ( assuming at this point its a text file.. )
private void readAFile( String fileName )
{
Scanner scnr = null;
try
{
scnr = new Scanner( new File( fileName ) );
while( scnr.hasNextLine() )
{
String line = scnr.nextLine();
//do whatever you want with each line of the file.
}
}
catch( FileNotFoundException ex )
{
System.out.println( "Cannot find the specified file." );
}
}
As far as exporting and distributing your application, a jar file is the way to go.
Message was edited by:
smithdale87
Thought up a few more quick questions, sorry for posting again so quick...
This program is an update on a previously Windows-only version I did in Visual Basic, and there is a database file that's pretty heavy duty and was built by hand over a period of time. MOST of the information will input properly, but I was wondering if Java will be able to parse the "#TRUE#"/"#FALSE#" boolean stuff and the "#2006-03-06"# Date stuff. If not, could I input that as a string and convert it to a different variable type after? Thanks!
> in order to read from a file, ( assuming at this
> point its a text file.. )
>
> [code]
>
> private void readAFile( String fileName )
> {
>Scanner scnr = null;
> try
>{
>scnr = new Scanner( new File( fileName ) );
>while( scnr.hasNextLine() )
>{
>String line = scnr.nextLine();
> //do whatever you want with each line of the
> file.
>}
>
>catch( FileNotFoundException ex )
> {
> System.out.println( "Cannot find the specified
> file." );
>}
> /code]
>
>
> As far as exporting and distributing your
> application, a jar file is the way to go.
>
> Message was edited by:
> smithdale87
Thanks for your fileInput help, it looks like a simpler way for me to do it (Scanner, I mean), but that still has me manually putting in a path for the file... I need to be able to move the program around without changing the directory each time.
Also, does the jar preserve directories then?
also one more thing, the scnr needs to be closed after finsihed reading.. so be sure to add the following after teh catch(..)
finally
{
if( scnr != null )
scnr.close();
}
Why not use a JFileChooser to choose the location of the input file? Instead of always hard coding it?
How does JFileChooser work?
*edit* Ah, I see... that COULD work, but it would be alot more complicated. If anyone knows how to do resource stuff with Eclipse, I'd really prefer that if possible.
Message was edited by:
thefil
sorry Im not familiar with Eclipse, i use Netbeans IDE for all my java coding..
but just for kicks you can do this..
private String getFile()
{
JFileChooser jfc = new JFileChooser( "." );
String fileName;
if( jfc.showOpenDialog(null) == jfc.APPROVE_OPTION )
fileName = jfc.getSelectedFile().getAbsolutePath();
return fileName;
}