Results 1 to 15 of 15
- 01-28-2011, 11:58 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
Having trouble opening and reading a txt file
Hello, I have to do a project where I have to be able to read a text file and then be able to search by characters and replace them with other characters. I want to start off small, so I am trying to create a small program that just opens files and reads them, due to my inexperience with IO, I came to an error and is stuck with it.
What I did, in Netbeans I created a blank file and then wrote on it couple sentances and then named the file someFile.txt. This file is also located in the same package where I made my class called SearchFile, and it goes like this:
ackage searchfile;
import java.io.*;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.Reader;
/**
*
* @author Dennis
*/
public class SearchFile {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
String fileName = "someFile.txt";
Scanner someFile = null;
System.out.println("The file " + fileName + " contains the following lines:");
try {
someFile = new Scanner(new File(fileName));
}
catch (FileNotFoundException e) {
System.out.println("Error opening the file: " + fileName);
System.exit(0);
}
while (someFile.hasNextLine()) {
String line = someFile.nextLine();
System.out.println(line);
}
someFile.close();
}
}
Everytime I ran the program, it failed to open the file. Can anyone here help me please? Perhaps I made a syntax error somewhere...
- 01-29-2011, 12:09 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
Define "it failed"....does it throw an exception?
- 01-29-2011, 12:29 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
Yes, it throws FileNotFoundException e, and the string "Error opening the file" pops up as the result.
- 01-29-2011, 12:42 AM #4
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
Stop using netbeans for a while, and remove the "package" line for this time. Then run your program through the command prompt. make sure you have someFile.txt in the directory where you run your program.
- 01-29-2011, 12:53 AM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
When you just put "someFile.txt" it means that this file is place same with the path of your project. Try to put the absolute path temporarily to test if it will run. If it runs with the absolute file it means the someFile.txt is not located same as your project's location.
- 01-29-2011, 01:08 AM #6
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
Unfortunatly, I'm not very familliear with running the program through command prompt, I'm used to Netbeans.
What do you mean by absosulute path? Do you want me to implement it as "absolute" instead of "someFile" or is that somewhere in the settings?
What I did is I created a file right under SearchFile.java, they are both located in the source package, or at least thats what it seems to me lol.Last edited by DBaskov; 01-29-2011 at 01:13 AM.
- 01-29-2011, 01:16 AM #7
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
The complete path.
D:\MyProjectFolder\someFile.txt
And in java you have to us double slash or backslash so the complete path will look like this
GoodLuck!Java Code:String fileName = "D:\\MyProjectFolder\\someFile.txt" //[b]OR[/b] String fileName = "D:/MyProjectFolder/someFile.txt"
- 01-29-2011, 01:26 AM #8
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
Well I tryed typing in file's location like in your example and still same error. Wow, this is a lot frustrating than I though it ws going to be...
The file's location is C:/Users/Dennis/Music/array2/Search File/srcsearchfile , however I also added /someFile.txt to it to make it specific.Last edited by DBaskov; 01-29-2011 at 01:30 AM.
- 01-29-2011, 01:39 AM #9
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Same error? As I remember your error a while ago is FileNotFoundException.
FileNotFoundException is thrown when file does not exist or it is a read-only file.
Check if
1. the path you typed is correct, just to make sure.
2. your file is read-only.
Is search file being accessed from another class?
- 01-29-2011, 01:55 AM #10
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
The location is correct because I copied and pasted it from properties, the only differance is that I added /someFile.txt to it as I mentioned earlier because without that I don't think it would find it.
I'm not sure what exactly makes the file read only, but I can ask Netbeans by right click and "open in the system" and the file pops out in the notepad window.
You know how when you compile a project, the sometimes there would be several files in some location, well thats how my text file and SearchFile class are. What I did is, I created the class, and then I right clicked on the class and said new blank file, and then I filled it and named it someFile.txt
- 01-29-2011, 02:23 AM #11
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
I guess we have to check it at runtime using File class exist() and canRead() method.
I add some on your code.
Java Code:try { [b] File myFile = new File(fileName); //If file does not exist it will print if(! myFile.exists()) { System.out.println("File does not exist!\n should not continue"); } //If file cannot read it will print a message if(! myFile.canRead()) { System.out.println("File exist but cannot read!\n " + "should not continue either."); } [/b] someFile = new Scanner(myFile); [b][i]//!Notice that I use myFile instead[/i][/b] }
- 01-29-2011, 02:37 AM #12
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
Ok I tried it but I took out "try" because there is no catch statement, and it gave me bunch of errors and exceptions. It also printed both "file not found" and "file find but cannot read" strings. lol this is a big mess...
- 01-29-2011, 02:41 AM #13
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
No, do not remove catch. I just only show what I change, and it happens that the this I change are only at try statement.
To make it clear, I repost the code and what I changed
Java Code:package searchfile; import java.io.*; import java.util.Scanner; import java.io.FileNotFoundException; import java.io.Reader; public class SearchFile { public static void main(String[] args) throws IOException { String fileName = "someFile.txt"; Scanner someFile = null; System.out.println("The file " + fileName + " contains the following lines:"); try { //[b] File myFile = new File(fileName); //If file does not exist it will print if(! myFile.exists()) { System.out.println("File does not exist!\n should not continue"); } //If file cannot read it will print a message if(! myFile.canRead()) { System.out.println("File exist but cannot read!\n " + "should not continue either."); } //[/b] someFile = new Scanner(myFile); //[b][i]!Notice that I use myFile instead[/i][/b] } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println("Error opening the file: " + fileName); System.exit(0); } while (someFile.hasNextLine()) { String line = someFile.nextLine(); System.out.println(line); } someFile.close(); } }
- 01-29-2011, 02:51 AM #14
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
Hmm, well I edited it and it threw out those to strings out again; saying that it didn't find a fail and another one saying it did but can't read and I also got an exception saying that file is not found in the system. So I guess I location is false, but idk what to do. Maybe I should relocated the file to a more simple location...
- 01-29-2011, 02:55 AM #15
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
Wow and it worked, phew. I relocated it to a simpler place and I guess I pasted the wrong thing. I shouldn't have copied the location, I should have copied object name in properties because it displays the location and the text's name. So I guess it works now lol.
Thanks for the help everyone, especially Mine, you're statement worked this time. I almost lost my cool over this little thing.Last edited by DBaskov; 01-29-2011 at 03:00 AM.
Similar Threads
-
Trouble with Try Catch blocks and file reading.
By theBurgh22 in forum New To JavaReplies: 2Last Post: 11-30-2010, 01:11 AM -
Help opening and reading a file
By viperlasson in forum New To JavaReplies: 0Last Post: 09-21-2010, 07:32 AM -
Opening mp3 file ( not reading )
By Char in forum New To JavaReplies: 15Last Post: 08-29-2010, 07:09 AM -
Having trouble reading external txt file to an array
By Metastar in forum New To JavaReplies: 18Last Post: 07-21-2010, 11:29 PM -
Problem in opening a file
By Raghav kv in forum New To JavaReplies: 1Last Post: 08-11-2007, 01:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks