• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

couple of qs

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.can a method have two returns:
return(exp1);
return(exp2);
what is the significannce of return(exp) anyways ?

2. if we have any array
int keys[] = new int[10];
and we call it using keys[-1], what happens ?

3. if a file already exists on our hard disk what is the result of the following ?
1. creating that file using just File
2. creating that file using FileOutputStream
3. creating the file using FileWriter
please explain:
a) any exceptions that may be thrown, comp. errors\
b) if new contents are appended, or overwritten(ie the previous contents of file are erased)

thanks
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can return different things from a method by doing something like this:-
if (flag==true)
return flag;
else
return otherFlag;
But you only return one thing - you can't have a method that returns two things by doing:-
return x;
return y;
If you want to return several things, you'll have to do that using a vector, array or object - something like that which lets you bundle lots of variables up.
On the second question, I'm not entirely certain on this so maybe you should write some code to confirm it or maybe someone else can back me up. You won't get a compile error. You will get an ArrayOutOfBounds exception thrown when you run it.
Hope this helps.
Kathy

[This message has been edited by Kathy Rogers (edited December 24, 2000).]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sarim,
Clarifications to your queries on File IO are mentioned below.
[Query]if a file already exists on our hard disk what is the result of the following ?
1. creating that file using just File
[Clarification]We cannot create a file with just a 'File' object. The File class just provides us a handle to obtain some platform independent representation of a pathname and its components.
For creating a new empty file, use the boolean createNewFile() method as illustrated in the CreateFile.java example program given at the end of this reply.
2. creating that file using FileOutputStream
[Clarification]FileOutputStream is used for writing data as a byte array to a file. Contents of the existing file is overwritten.
3. creating the file using FileWriter
[Clarification]FileWriter is a convenience class for writing data as a character array to a file. Contents of the existing file is overwritten.
please explain:
a) any exceptions that may be thrown, comp. errors\
[Clarifications]All the exceptions thrown by File IO operations are subclasses of java.io.IOException. Since IOException is a checked exception, we need to explicitly declare it in a 'throws' clause in the method header or enclose our code in a 'try-catch' construct. Otherwise there would be a compilation error.
b) if new contents are appended, or overwritten(ie the previous contents of file are erased)
[Clarification] Clarified above.
Best wishes,
Balu
Example Program
---------------
import java.io.*;
public class CreateFile
{
public static void main(String args[])
{
try
{
File f1 = new File("TextFile1.txt"); //creates a new file instance
File f2 = new File("TextFile2.txt"); //creates a new file instance

System.out.println("Created a new empty file TextFile1.txt "+f1.createNewFile());
System.out.println("Created a new empty file TextFile2.txt "+f1.createNewFile());
// createNewFile creates a new file and returns 'true' if the file
// is not existing. If the specified file is already existing, this
// method returns false and does not create any file. That means that
// the existing file (if any) remains untouched.

FileOutputStream fos = new FileOutputStream(f1);// FileOutputStream is a byte stream.
byte b[] = {1,2,3,4,5};
fos.write(b); // results in overwriting the original contents of the file
// with some bytes.
fos.close();

FileWriter fw = new FileWriter(f2);
char ch[] = {'a','b','c','d'};
fw.write(ch);//FileWriter is a character stream
fw.close();

}
catch(IOException ioe)
{
System.out.println("Exception while writing to TextFile.txt "+ioe);
}
}
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic