File I/O Problem in Ubuntu (can't open text file)

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

36 Replies - 6405 Views - Last Post: 24 April 2011 - 07:18 AM Rate Topic: -----

#1 ndimario  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 20-April 11

File I/O Problem in Ubuntu (can't open text file)

Posted 20 April 2011 - 09:53 PM

I've used Pascal, VB6, C, C++, C# and various web languages. I've been playing with Java for the last week and have been stumped by one of the basics. What also makes this difficult is that I'm also new to Ubuntu 10.04 linux. Although it is somewhat easy to use, programming in it seems to be a whole different challenge. Here is the code below that I'm using:

try
{
reader = new BufferedReader(new FileReader("file.txt"));
while ((line = reader.readLine()) != null)
Source += line + "\n\r";
reader.close();
}
catch (IOException e)
{
return "";
}

I've never gone to school for programming, so there will probably be holes in my knowledge. However I use NetBeans 'Clean&Build', then go to the /home/user/NetBeansProjects/projectname/dist' directory, copy 'file.txt' there, then execute the program using 'java -jar projectname.jar' and the file does not read. I'm not sure whats wrong...

This post has been edited by ndimario: 20 April 2011 - 09:54 PM


Is This A Good Question/Topic? 0
  • +

Replies To: File I/O Problem in Ubuntu (can't open text file)

#2 ndimario  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 20-April 11

Re: File I/O Problem in Ubuntu (can't open text file)

Posted 20 April 2011 - 10:24 PM

I've attempted to see if it was an access problem and used 'chmod +x' onto the text file and it did nothing. For some reason the program will work in NetBeans if I place the text file into the /home/user/NetBeansProjects/projectname directory and go to Run -> Run Main Project. The file reads successfully that way and only that way.. My goal right now is to get the file to be read within the same directory as the executable jar file.
Was This Post Helpful? 0
  • +
  • -

#3 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,692
  • Joined: 13-March 10

Re: File I/O Problem in Ubuntu (can't open text file)

Posted 21 April 2011 - 02:57 AM

I think there is no problem with your settings but more with your code.

Look. You have a while loop that iterates through lines of the text file. But after reading the first line you close the BufferedReader by calling close() inside the while loop. After this line is executed, the reader is not able to read anymore.

More this line of code outside of while loop.

while((line = reader.readLine()) != null)
{
//read lines of text


}

//here close the bufreader
reader.close();





Hope it helps.
Was This Post Helpful? 1
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2780
  • View blog
  • Posts: 11,765
  • Joined: 20-September 08

Re: File I/O Problem in Ubuntu (can't open text file)

Posted 21 April 2011 - 03:29 AM

Quote

My goal right now is to get the file to be read within the same directory as the executable jar file.


There are ways to achieve that, but they would different based on whether you need to write to it from within the program. Do you?

In order to understand the problem, you need to understand the concept of 'current directory' so it's probably a good idea to do some preliminary reading on that if you're not 100% certain of what it means.
Was This Post Helpful? 1
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: File I/O Problem in Ubuntu (can't open text file)

Posted 21 April 2011 - 03:46 AM

View Postdarek9576, on 21 April 2011 - 09:57 AM, said:

Look. You have a while loop that iterates through lines of the text file. But after reading the first line you close the BufferedReader by calling close() inside the while loop.

Look closer. The while loop has no code block, so only the first line after the while loop will be considered a part of the loop.

// His code:
while ((line = reader.readLine()) != null)
Source += line + "\n\r";
reader.close();

// Is equivelant to:
while ((line = reader.readLine()) != null) {
    Source += line + "\n\r";
}
reader.close();


Was This Post Helpful? 1
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2780
  • View blog
  • Posts: 11,765
  • Joined: 20-September 08

Re: File I/O Problem in Ubuntu (can't open text file)

Posted 21 April 2011 - 03:57 AM

Quote

The while loop has no code block, so only the first line after the while loop will be considered a part of the loop.


Yes, but that's not a problem (though not good practice) ;) And to prove it:

Quote

if I place the text file into the /home/user/NetBeansProjects/projectname directory and go to Run -> Run Main Project. The file reads successfully that way and only that way.

This post has been edited by g00se: 21 April 2011 - 03:57 AM

Was This Post Helpful? 1
  • +
  • -

#7 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: File I/O Problem in Ubuntu (can't open text file)

Posted 21 April 2011 - 04:16 AM

Yea, I know it's not a problem. That was the point I was trying to make :)

I don't really mind it when people skip the code block as such, just as long as it's indented properly, and there is perhaps a line separating the next line.
Was This Post Helpful? 1
  • +
  • -

#8 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2045
  • View blog
  • Posts: 4,233
  • Joined: 11-December 07

Re: File I/O Problem in Ubuntu (can't open text file)

Posted 21 April 2011 - 04:43 AM

catch (IOException e)
{
return "";
}


This here is a problem. Your program is failing under a particular set of circumstances and the IOException would give you a lot of information about that. For a start, it will give you the line the of your code where the problem occurred. From there you can deduce a lot about the problem. Why would an IOException be thrown in your code?

The FileReader will throw a FileNotFoundException if it couldn't find the file in the first place; and the readLine() method will throw an IOException if there is a problem reading in a line.

At the very least print the stack trace but really, you're probably better off just declaring that the method throws an IOException. Some people advocate catching every possible Exception but that really only makes sense if you can deal with it in a sensible manner. The very worst thing you can do is just ignore the exception as if nothing had happened.

At the very least, change your code to this to help diagnose the problem.
catch (IOException e) {
    e.printStackTrace();
    return "";
}

Was This Post Helpful? 1
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2780
  • View blog
  • Posts: 11,765
  • Joined: 20-September 08

Re: File I/O Problem in Ubuntu (can't open text file)

Posted 21 April 2011 - 04:59 AM

The problem is entirely path-related, but i'm waiting for a response from the OP
Was This Post Helpful? 2
  • +
  • -

#10 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2045
  • View blog
  • Posts: 4,233
  • Joined: 11-December 07

Re: File I/O Problem in Ubuntu (can't open text file)

Posted 21 April 2011 - 05:28 AM

Agreed. :)
Was This Post Helpful? 1
  • +
  • -

#11 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,692
  • Joined: 13-March 10

Re: File I/O Problem in Ubuntu (can't open text file)

Posted 21 April 2011 - 06:14 AM

Ooops. My bad. You are right. Its not about close() method :). I think if it was indented properly then i would not have made that mistake :). Soz.
Was This Post Helpful? 1
  • +
  • -

#12 ndimario  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 20-April 11

Re: File I/O Problem in Ubuntu (can't open text file)

Posted 21 April 2011 - 07:14 AM

Yeah it is path related, though the suggestions are appreciated. The program fully works only if the text file is placed in the base folder of the netbeans project. It doesn't work if its placed into the same folder as the program itself.

for example this is the code, it should try to find a program within the same directory (if the tutorials i have read or correct):
reader = new BufferedReader(new FileReader("file.txt"));

however while working on the project, i placed the text file into the /home/user/NetBeansProjects/projectname directory and it worked. However I can't get it to work anywhere else with the compiled jar file. Even the jar file from the /dist directory won't work with the text file in the base folder of the project.

Note: the return "" statement is from a function that takes a single argument (the file name) and then uses the file name to open a text file thats within the same directory as the running program, then returns the text thats within the file. Failure to read/locate the file results in an empty string as the return result.

This post has been edited by ndimario: 21 April 2011 - 07:18 AM

Was This Post Helpful? 0
  • +
  • -

#13 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2045
  • View blog
  • Posts: 4,233
  • Joined: 11-December 07

Re: File I/O Problem in Ubuntu (can't open text file)

Posted 21 April 2011 - 07:26 AM

Yes, but you should still print the stack trace so you can debug the program. Have you tried finding out where Java thinks the current directory is as g00se suggested?
Was This Post Helpful? 1
  • +
  • -

#14 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2780
  • View blog
  • Posts: 11,765
  • Joined: 20-September 08

Re: File I/O Problem in Ubuntu (can't open text file)

Posted 21 April 2011 - 07:30 AM

ndimario: you didn't answer the question i asked you...
Was This Post Helpful? 1
  • +
  • -

#15 ndimario  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 20-April 11

Re: File I/O Problem in Ubuntu (can't open text file)

Posted 21 April 2011 - 07:45 AM

Goose:
The program never writes so I've never tested that. However it does work under the specified instance (from my previous post). As fur understanding 'current directory'. Does my version of 'current directory' exist in Java? Its easy to do in any other language that I've used and in windows. Why is this so difficult for Java and Ubuntu?

My goal is for this:
projectname.jar
file.txt

Both files are within the same directory and I need the jar file to be able to read the file.txt. I don't want to specify a specific path because i want the program to be dropped into the directory of the log file that they are going to be using the program with.

Cfoley:
I ran the program once with:

File file = new File("file.txt");
System.out.printn(file.getAbsolutePath());

The program would print out /home/user. The same path is returned when I would try to get the path using 'user.dir' though i forgot the code for that. Whats more strange about that is that the path does not go into the NetBeansProjects folder according to that (/home/user/NetBeansProjects/projectname).

This post has been edited by ndimario: 21 April 2011 - 07:47 AM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3