aspose file tools*
The moose likes Beginning Java and the fly likes FileNotFoundException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "FileNotFoundException" Watch "FileNotFoundException" New topic
Author

FileNotFoundException

Daenen Omega
Greenhorn

Joined: Jul 24, 2004
Posts: 5
Why won't java find the file "FFile.txt" in this code? The file is located in the same directory as the .java file?!? I am going INSANE!!!

----------------------------------------------------------------------------

import java.io.*;
import java.util.*;

public class SortF {
public static void main(String[] args) {
BufferedReader in = new BufferedReader(
new FileReader("FFile.txt"));
String s2 = new String();
s2 = in.toString();
in.close();

StringTokenizer FTokens = new StringTokenizer(s2);

String[] F = {FTokens.nextToken(), FTokens.nextToken(), FTokens.nextToken(), FTokens.nextToken(), FTokens.nextToken(), FTokens.nextToken(), FTokens.nextToken(), FTokens.nextToken(), FTokens.nextToken()};

System.out.println();

for(int x = 0; x < F.length; x++)
System.out.println(F[x]);
System.out.println();

Arrays.sort(F);

for(int x = 0; x < F.length; x++)
System.out.println(F[x]);
System.out.println();

}
}
Darin Niard
Ranch Hand

Joined: Jun 08, 2004
Posts: 118

Change that to...

...please

That won't fix your problem, but

Perhaps you have extensions disabled, and it is really FFile.txt.txt or something similar?

edit: broke tables
[ July 24, 2004: Message edited by: Darin Niard ]
Daenen Omega
Greenhorn

Joined: Jul 24, 2004
Posts: 5
Thanks for the split() tip and I added and IOException but now I have this:
----------------------------------------------------------------------------
import java.io.*;
import java.util.*;

public class SortF {
public static void main(String[] args) {

try {

BufferedReader in = new BufferedReader(
new FileReader("FFile.txt"));
String s2 = new String();
s2 = in.toString();
in.close();

String[] F = s2.split(" ");
System.out.println();

for(int x = 0; x < F.length; x++)
System.out.println(F[x]);
System.out.println();

Arrays.sort(F);

for(int x = 0; x < F.length; x++)
System.out.println(F[x]);
System.out.println();

} catch (IOException ioe) {

}
}
}
----------------------------------------------------------------------------
and these are my errors:
in NetBeans:

java.lang.NoClassDefFoundError: practice/SortF (wrong name: SortF)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:537)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
Exception in thread "main"

and in DOS I get:

java.io.BufferReader@1ddebc3

java.io.BufferReader@1ddebc3

What the ???
Darin Niard
Ranch Hand

Joined: Jun 08, 2004
Posts: 118
Originally posted by Daenen Omega:
s2 = in.toString();

...

java.io.BufferReader@1ddebc3

You should be using readLine() not toString().
Daenen Omega
Greenhorn

Joined: Jul 24, 2004
Posts: 5
I just started to learn Java so I am not really saying anything against it, yet. However, I just crushed this problem using C++ in record time:
-----------------------------------------------
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
char str[9][80];
char t[80];
char fname[80];

cout << "ENTER COMPLETE FILE NAME: ";
cin >> fname;

ifstream in(fname);
if(!in) {
cout << endl << "YOU'RE SO WRONG!" << endl << endl;
system("PAUSE");
return 1;
}

for(int i=0; i<10 ; i++) in >> str[i];

in.close();
int size = 9;

//use C++ bubble sort to sort the array.

for(int a=1; a<size; a++)

for(int b=size-1; b>=a; b--) {

if(strcmp(str[b-1],str[b])>0) {

strcpy(t, str[b-1]);
strcpy(str[b-1], str[b]);
strcpy(str[b], t);
}
}

for(int x=0; x<10; x++) cout << str[x] << endl;

system("PAUSE");
return 0;
}
Daenen Omega
Greenhorn

Joined: Jul 24, 2004
Posts: 5
Tada - Java!
----------------------------------------------------------------------------
import java.io.*;
import java.util.*;

public class SortF {
public static void main(String[] args) {

try {

BufferedReader in = new BufferedReader(
new FileReader("FFile.txt"));
String[] F = new String[9];
for(int i=0; i<9; i++) F[i] = in.readLine();
in.close();

System.out.println();

for(int x = 0; x < F.length; x++)
System.out.println(F[x]);
System.out.println();

Arrays.sort(F);

for(int x = 0; x < F.length; x++)
System.out.println(F[x]);
System.out.println();

} catch (IOException ioe) {

}
}
}
----------------------------------------------------------------------------
I would like to thank all of the goodly people that helped me through this difficult time - Thanks!

One last quandry though. This program runs great in DOS but in NetBeans the output is BLANK??? Any thoughts?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Daenen Omega:
One last quandry though. This program runs great in DOS but in NetBeans the output is BLANK??? Any thoughts?


If you put an ioe.printStackTrace() in the catch block, you will probably see the FileNotFoundException again.

I guess that Netbeans is running your program in a different directory. I don't know this IDE, but you should be able to configure that somewhere - it's probably called "working directory" or something.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Robert Konigsberg
Ranch Hand

Joined: Jun 23, 2004
Posts: 172
I forget which method you should use, but there has got to be a method that tells you what the current directory is. You might want to start by looking at System.getProperty("user.dir");

RK


SCJP 1.4 (91%)<br />SCJD 1.4 (376/400, 94%)
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
You'll probably have a better chance of getting IDE (Netbean) -specific help in the IDE forum.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Sonny Pondrom
Ranch Hand

Joined: Jun 05, 2001
Posts: 128
Daenen

You should checkout the tutorial on input/output
Daenen Omega
Greenhorn

Joined: Jul 24, 2004
Posts: 5
Thanks for all of your help friends!!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: FileNotFoundException