f



Reading a file FileReader, doesn't read a file

Hello
I wanted to write a simple program that gets a string from a file. But
i don't know where  to put the file that i could read it. I puted the
plik.txt file in the package where my Main class is.  I UseNetBeans if
that makes a diference.  Here is my code.

package czytaniezpliku

import java.io.*;

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

		try {
                    String wejscie;
			BufferedReader br =3D new BufferedReader(new
FileReader("plik.txt"));
                        //FileReader we =3D new FileReader("pliki/
plik1.txt");
                        wejscie=3Dbr.readLine();
                        System.out.println(wejscie);
                } catch (IOException e) {
			System.out.println("B=B3=B1d przy czytaniu");
		}

	}
}

0
kgbol (9)
6/4/2007 2:48:32 PM
comp.lang.java.help 13127 articles. 0 followers. Post Follow

6 Replies
801 Views

Similar Articles

[PageSpeed] 1

Hi Kgbol,

Kgbol wrote:
> Hello
> I wanted to write a simple program that gets a string from a file. But
> i don't know where  to put the file that i could read it. I puted the
> plik.txt file in the package where my Main class is.  I UseNetBeans if
> that makes a diference.

Indeed that may make a difference. In Java, it is always a good idea to 
either use the full path name (e.g. "c:/...") (which you might get using 
a (J)FileDialog), because the "working directory" might not the one you 
expect it to be, or you might not directly read a file at all and use 
Class#getResourceAsStream or ClassLoader#getResourceAsStream instead.

It depends on your application and how you want to deploy it.

Ciao,
Ingo

0
Ingo
6/4/2007 3:14:58 PM
Kgbol wrote:
...

'What Ingo said'.  But in addition to that..

>package czytaniezpliku

Please refrain from posting code that is not english.
You obviously speak English (whereas I am clueless 
when it comes to ..Polish?) so it might help both me,
and any number of other developers who read the code 
later, to have package (class, method and attribute) 
names in English.
...
>			BufferedReader br = new BufferedReader(new
aaaaaaaaaaaaaaaaaaaaaaaaaaa
>FileReader("plik.txt"));

Code posted with 'tabs' often becomes ridiculously
wide when posted to usenet.  The string of 'a's above,
indicates where the 'BufferedRe..' text starts on the
previous line.  Please replace tabs with a few spaces
before posting..

But to get to the point.  That line of code is very bad
for purposes of debugging.  Better to do something like..

  File theFile = new File( "plik.txt" );
  System.out.println( theFile.exists() + " " + file.getCanonicalPath() );
  // ..go on to make a BufferedReader..

>                } catch (IOException e) {
>			System.out.println("Błąd przy czytaniu");

And this.. (ick).

Note that 'e.printStackTrace()' not only 'speaks all languages'
(in a sense), but provides more information, and is also shorter.

-- 
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-setup/200706/1

0
Andrew
6/4/2007 3:32:46 PM
I modified my code Like Andrew said:

package readfromfile;

import java.io.*;

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

try {

File theFile = new File("plik.txt");
System.out.println( theFile.exists() + " " +
theFile.getCanonicalPath() );
BufferedReader br = new BufferedReader( new
FileReader(theFile.getCanonicalPath()));

} catch (IOException e) {
e.printStackTrace();}
}
}

But i have no clue why it still don't work.

0
Kgbol
6/4/2007 4:26:28 PM
Hi,

What's the error-message?

Ciao,
Ingo

0
Ingo
6/5/2007 6:53:09 AM
The error message is:
"java.io.FileNotFoundException:
C:\Documents and Settings\brat 011101\
Moje dokumenty\Marcin\szkola\semestr4\Java\CzytanieZPliku\plik.txt
(Nie mo=BFna odnale=BC=E6 okre=B6lonego pliku) at
java.io.FileInputStream.open(Native Method)"

But if i write the full path by myself:

"BufferedReader br =3D
new BufferedReader( new FileReader("C:/Documents and Settings/brat
011101/Moje dokumenty/Marcin/szkola/semestr4/Java/CzytanieZPliku/src/
czytaniezpliku/Marcin.txt"));"

the file is readed. Everything is ok. I wonder why it happens becouse
code that don't want to work.
CODE:

File theFile =3D new File("plik.txt");
BufferedReader br =3D new BufferedReader( new
FileReader(theFile.getCanonicalPath()));

Is equivalent to the working code.

theFile.getCannonicalPath returns String

0
Kgbol
6/6/2007 10:59:50 AM
Hi,

Kgbol wrote:
> The error message is:
> "java.io.FileNotFoundException:
> C:\Documents and Settings\brat 011101\
> Moje dokumenty\Marcin\szkola\semestr4\Java\CzytanieZPliku\plik.txt
> (Nie mo�na odnale�� okre�lonego pliku) at
> java.io.FileInputStream.open(Native Method)"
> 
> But if i write the full path by myself:
> 
> "BufferedReader br =
> new BufferedReader( new FileReader("C:/Documents and Settings/brat
> 011101/Moje dokumenty/Marcin/szkola/semestr4/Java/CzytanieZPliku/src/
> czytaniezpliku/Marcin.txt"));"
> 
> the file is readed. Everything is ok.

But you have noticed that these are different files, yes!?

 > I wonder why it happens becouse
> code that don't want to work.

Sorry?

> CODE:
> 
> File theFile = new File("plik.txt");

Please do not ignore my first advice in this thread (4.6.2007, 17:14):

"Indeed that may make a difference. In Java, it is always a good idea to 
either use the full path name (e.g. "c:/...") (which you might get using 
a (J)FileDialog), because the "working directory" might not the one you 
expect it to be, or you might not directly read a file at all and use 
Class#getResourceAsStream or ClassLoader#getResourceAsStream instead.

It depends on your application and how you want to deploy it. "

Hth,
Ingo

0
Ingo
6/6/2007 11:22:53 AM
Reply: