The Artima Developer Community
Sponsored Link

Java Answers Forum
File I/O problems

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ja Ja Binks

Posts: 14
Nickname: ds99bwood9
Registered: Apr, 2004

File I/O problems Posted: Apr 15, 2004 12:34 PM
Reply to this message Reply
Advertisement
I have a few problems when attempting to create a java program that will read an address from various inputs and printed to a standard output. Can anyone help? I'm supplying the code from Address.java that needs to be modified as I'm unsure. N.B part 1, which just takes the address and outputs it to whatever is required e.g. text file has already been done and is fine.

If possible, an explanation of what has been modified would be much appreciated - as I have stated in previous posts my programming isn't great and I merely want to get to a stage where I myself am confident in my own work.


ADDRESSREADER.JAVA

import java.io .*;
import java.util .*;

public class AddressReader
{

public static Address address = new Address("Beech 49", "Oak House", "1 Moseley Road", "Fallowfield", "Manchester", "M14 6HX", "UK");
public static String addressString = address.toString();
String outFile;
//File outFile;
FileOutputStream outStream;
//PrintWriter outStream;
//DataOutputStream outDataStream;
ObjectOutputStream outObjectStream;


public static void main (String []args) throws IOException
{

if (args.length ==0)
{

System.out.println(address.toString());
}

else if (args.length == 2)
{
try

{
String fileName = args[1];
File outFile = new File(fileName);
FileOutputStream outFileStream = new FileOutputStream(outFile);
if((args)[0].compareTo("-t") == 0)
{
PrintWriter outStream = new PrintWriter(outFileStream);
outStream.println(address.toString());
outStream.close();

}


else if(args[0].compareTo("-b") == 0)
{
DataOutputStream inDataStream = new DataInputStream(outFileStream);
String addr = address.dataRead(inDataStream);
outFileStream.close();
/**if (done) {
System.out.println (addr);
}
else {
System.out.println ("Object read failed");
}*/
}
else if(args[0].compareTo("-s") == 0)
{
ObjectOutputStream inObjectStream = new ObjectInputStream(outFileStream);
Address a = address.writeAddressObject(inObjectStream);
outFileStream.close();
/**if (done) {
System.out.println (a.toString());
}
else {
System.out.println ("Object read failed");
}*/
}
else
{
System.out.println("Invalid data range entered");


}
}


catch (IOException error)
{
System.err.println("Exception occured");
System.err.println(error.getMessage());
System.exit(1);


}

//{
//System.out.println("Invalid range entered");

//}


}


}

}

ADDRESS.JAVA

public void writeData(DataOutputStream out)
throws IOException
{
// Replace this comment and the next line with your code.
out.writeBytes(toString());
}

/**
* Replace me with documentation.
*/
public static Address readData(DataInputStream in)
throws IOException
{
// Replace this comment and the next lines with your code.
System.err.println("** Address.readData() - NOT WRITTEN YET **");

// Blank address so can compile code.
// Your address will need some data!
Address me = new Address();

// Must return the Address that has just been read.
return me;
}

Topic: Java Mail problem in WAS 5.0 Previous Topic   Next Topic Topic: convert string to image

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use