working with files in java

Following is the code for reading data from a file and print to the console

But its not working

Even though the file exist and contains data.

Can anybody answer "what's wrong with the program?"

what changes i have to make.

plz reply

import java.lang.*;

import java.io.*;

import java.lang.String;

publicclass objReadwrong

{

objReadwrong()

{

String testStr=null;

try

{

FileInputStream file =new FileInputStream("IOTest.txt");

ObjectInputStream objInput =new ObjectInputStream(file);

testStr=(String)objInput.readObject();

file.close();

}

catch(Exception e)

{

System.out.println("Cannot read object");

}

System.out.println("Object read from IOTest.txt:"+testStr);

}

publicstaticvoid main(String args[])

{

new objReadwrong();

}

}

[1721 byte] By [Anjali_Kalpsana] at [2007-10-3 3:32:16]
# 1

1) What's the specific meaning of "not working"?

2) If you catch an exception and don't print error message and stack trace, you deserve all the problems you get.

3) If you don't read the API of the streams you use, you should also not be surprised about things not working the way you guess them to do.

CeciNEstPasUnProgrammeura at 2007-7-14 21:26:29 > top of Java-index,Java Essentials,Java Programming...
# 2
is iotest.txt in same directory as that of jvmif not please specify the complete pathassuming c:\\temp\\abc\\IOTest.txthenceFileInputStream file = new FileInputStream("c:\\temp\\abc\\IOTest.txt");
vipuluckya at 2007-7-14 21:26:29 > top of Java-index,Java Essentials,Java Programming...
# 3
not working meanseven though the file conatins datait catch exceptionand printsnull for s.o.p statementwatch the program againexception handlers are used
Anjali_Kalpsana at 2007-7-14 21:26:29 > top of Java-index,Java Essentials,Java Programming...
# 4
> is iotest.txt in same directory as that of jvmBS. The present working directory is whatever's in the "user.dir" system property..
CeciNEstPasUnProgrammeura at 2007-7-14 21:26:29 > top of Java-index,Java Essentials,Java Programming...
# 5
it is in the same directory even though its not workingeven i checked by giving the ffull path of the file "iotest.txt"
Anjali_Kalpsana at 2007-7-14 21:26:29 > top of Java-index,Java Essentials,Java Programming...
# 6

> not working means

> even though the file conatins data

> it catch exception

> and prints

> null for s.o.p statement

Yeah. And not printing the stack trace and using ObjectInputStreams for text data is pretty much the dumbest thing you could possibly do.

> watch the program again

> exception handlers are used

I did watch it, I did read this statement, I watched it again and I had a good laugh. Where are you handling any exception? You're smothering them, that's all. Having a catch block does not automaticaly mean you handle the exception.

CeciNEstPasUnProgrammeura at 2007-7-14 21:26:29 > top of Java-index,Java Essentials,Java Programming...
# 7
it is in the same directory even though its not workingeven i checked by giving the ffull path of the file "iotest.txt"
Anjali_Kalpsana at 2007-7-14 21:26:29 > top of Java-index,Java Essentials,Java Programming...
# 8
can u plz give me the correct code for reading the data from filesand print it to console
Anjali_Kalpsana at 2007-7-14 21:26:29 > top of Java-index,Java Essentials,Java Programming...
# 9

> can u plz give me the correct code for reading the

> data from files

> and print it to console

No. But I'll give you a few hints:

http://java.sun.com/j2se/1.4.2/docs/api/index.html

FileReader

BufferedReader

(works only for text data of course)

CeciNEstPasUnProgrammeura at 2007-7-14 21:26:29 > top of Java-index,Java Essentials,Java Programming...
# 10

By the way, a question like "can you give me code" instead of "can you explain how to do it correctly" tells me that you won't have much of a career in development if you don't change your attitude. A really good step to a better life is learning to read the docs.

An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream.

If you had read this instead of guessing, you wouldn't even have encountered your problem.

CeciNEstPasUnProgrammeura at 2007-7-14 21:26:29 > top of Java-index,Java Essentials,Java Programming...
# 11

TRY OUT

FileInputStream file = new FileInputStream("c:\\abc\\IOTest.txt");

BufferedReader br=new BufferedReader(file);

System.out.println(br.readline());

//Only first line of file will be printed

file.close();

vipuluckya at 2007-7-14 21:26:29 > top of Java-index,Java Essentials,Java Programming...
# 12
thank u
Anjali_Kalpsana at 2007-7-14 21:26:29 > top of Java-index,Java Essentials,Java Programming...
# 13
i am a learneri have just stepped in the development fieldn thank for learning something from u
Anjali_Kalpsana at 2007-7-14 21:26:29 > top of Java-index,Java Essentials,Java Programming...
# 14
are u through with itsome of them sound hard here never mind keep goingRegardsvicky
vipuluckya at 2007-7-14 21:26:29 > top of Java-index,Java Essentials,Java Programming...
# 15
> are u through with it> some of them sound hard here never mind keep going> > Regards> vickysome of us are trying to raise the standards and prevent mis information
ScarletPimpernela at 2007-7-21 10:12:18 > top of Java-index,Java Essentials,Java Programming...
# 16
why don't u join some expert forum and try yrself their
vipuluckya at 2007-7-21 10:12:18 > top of Java-index,Java Essentials,Java Programming...
# 17
> why don't u join some expert forum and try yrself> theirWhy dont you check your facts before posting rubbish.
ScarletPimpernela at 2007-7-21 10:12:18 > top of Java-index,Java Essentials,Java Programming...
# 18

> i am a learner

> i have just stepped in the development field

> n thank

> for learning something from u

If that's directed at me: Great that you learnt something! You need to recognize that it's not enough to look at code examples. It's essential for a good developer to know his tools (by reading their documentations) and to be able to look up information himself. Also, it's important to be able to transform a problem description (specification) into code, without having to be given concrete code examples. Learning Java is important, but there's lots more to programming that you have to know than just the language.

Good luck! :)

CeciNEstPasUnProgrammeura at 2007-7-21 10:12:18 > top of Java-index,Java Essentials,Java Programming...
# 19
> why don't u join some expert forum and try yrself> theirDid you know that your code example doesn't even compile because the BR c'tor won't take an InputStream?
CeciNEstPasUnProgrammeura at 2007-7-21 10:12:18 > top of Java-index,Java Essentials,Java Programming...
# 20
[url= http://java.sun.com/docs/books/tutorial/essential/io/]Java Basic IO Tutorial[/url]
mlka at 2007-7-21 10:12:18 > top of Java-index,Java Essentials,Java Programming...
# 21

i need to learn much from u

can i get the email addr of urs

so that i can mail u regarding any types of queries.

may be a silly

plz help me i am a learner

from so many days i was trying for such a website where we can ask question n get answers

plzzzzzzzzzz

Anjali_Kalpsana at 2007-7-21 10:12:18 > top of Java-index,Java Essentials,Java Programming...
# 22

> i need to learn much from u

> can i get the email addr of urs

> so that i can mail u regarding any types of queries.

Mine? No, sorry. Teaching programming over the net is very tedious. For that, better look around for some people who could teach you personally, It's much easier, because it's simpler for you to ask questions and for him/her to see where you still lack understanding. You can also stay around here and occasionally read and/or post to some threads - you learn a lot from being here. And lastly: practice, practice, practice. Read about OO design principles, patterns, testing, usability... the web is a rich source of information.

Also, if you have a "what is ..." question, just put it into Google, maybe add "java" when in doubt. If you have a problem you can't solve or a question that can't seem to find an answer for, feel free to post here (but only after searching). I or someone else will help you solving it or tell you where to look up a solution.

And if someone's rude and just tells you to read the docs or search, then do it. Most likely that person knows that the docs contain what you need to know. :)

CeciNEstPasUnProgrammeura at 2007-7-21 10:12:18 > top of Java-index,Java Essentials,Java Programming...
# 23
ok fine i will go through the documentation.thank u
Anjali_Kalpsana at 2007-7-21 10:12:18 > top of Java-index,Java Essentials,Java Programming...
# 24
> i am a learner> i have just stepped in the development field> n thank > for learning something from upls tp n fl wds as this is n english lng frm, not indglish. if u use txt speak u exps uslf to rdcule n scrn.tnx.
marklawforda at 2007-7-21 10:12:18 > top of Java-index,Java Essentials,Java Programming...
# 25
> some of them sound hard here never mind keep goingnever mind even if you consistently post incorrect replies. At least, you are consistent; that's a remarkable quality.
ansi-boya at 2007-7-21 10:12:18 > top of Java-index,Java Essentials,Java Programming...