• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java methods  RSS feed

 
Asrita Saran
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai ranchers...
i have one doubt..
Will the way java predefined methods behave differ with the operating system?
Will FileOutputStream behave one way in Windows and other way in LINUX..


Thanks in advance..
[ July 17, 2007: Message edited by: Asrita Saran ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Please don't post the same question to more than one forum; see our explanation here. I've deleted the other copy of this thread.

As to your question: FileOutputStream is implemented on top of each native platform's own APIs, so that it does whatever is appropriate for that platform. For the most part, the abstraction is the same on each platform, but of course Java can't make filesystems work differently. For example, on some Windows filesystems, the files FOO and foo might be the same file, whereas on Linux, they'd be distinct; Java does nothing to hide this fact, and so the sequence of calls

FileOutputStream f1 = new FileOutputStream("FOO");
FileOutputStream f2 = new FileOutputStream("foo");
....
f1.close();
f2.close();

would have very different effects on Windows and Linux.

Does this answer your question?
[ July 17, 2007: Message edited by: Ernest Friedman-Hill ]
 
Asrita Saran
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im really sorry..

Your answer just cleared some of my doubts.what aboutt write() and close()?I think both those methods will behave similarly on any OS.
Is that right or they behave differently based on OS?
 
Francesco Bianchi
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Asrita Saran:
what aboutt write() and close()?I think both those methods will behave similarly on any OS.
Is that right or they behave differently based on OS?


Yes, they SHOULD behave equally if the JVM you are using is strictly Java API compliant. But you probably experienced some problems on a OS with them which you did not experienced with another, didn't you?

If this is the case you probably forgot about the fact the Unix-like OS and Windows uses different separators in the pathname (one uses slash while the other uses backslash). When you instantiate a File object using a bad-formatted path you don't get the error. In effect, what you are doing is simply creating a File descriptor (used just by the JVM, not by the OS), something the JVM will use to "find" and "act on" the file/directory. But you don't get any control about the existence of the file. It's only when
you call on it some methods that you discover it doesn't exists (or, better, you didn't write well the path to find it).

Am I correct? Ernest?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One goal for Java was to make all platforms look as much alike as possible. I'd give Sun good marks overall. The number of OS-specific things you have to worry about is relatively small. A lot of times if one OS could do something that another could not, they just left it out.

My gut guess is that this file system area accounts for a big chunk of the differences. The look & feel of the IO is another tricky area. Does anybody here have to work around differences in other areas?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another difference are line endings (CR/LF on Windows, just LF on Unix et al., and just CR on Mac OS 9, for those old enough to remember). By default, the platform standard is used, but whether that's correct depends on what you're going to do with the file. (A bit more background about how this also applied to socket programming is given in: Don't println to a Socket).

There are several more involving the file system, as Stan pointed out, e.g. one-vs.-multiple file system roots, and little-endian vs. big-endian binary files.
[ July 17, 2007: Message edited by: Ulf Dittmer ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!