File IO
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Thursday, April 24, 2008 03:17 PM

I need help with java File IO The output look likes This but it suppose to give out the From car file:, From years file:, and From values file: but it does'nt this is what I came out with it is output. --------------------------- CIS 3190 Homework #11 by Larry Schmieding --------------------------- Directory exists Writing to file e:/javadata/myfile Done!!! Directory exists File exists Reading from file myfile From file: 5 From years file: 5 From years file: 5 From years file: 479343 From years file: 1920164712 From years file: 1694500418 From values file: 6.6542504E22 From values file: 3.7   More>>

I need help with java File IO

The output look likes This but it suppose to give out the From car file:, From years file:, and From values file:
but it does'nt this is what I came out with it is output.

			---------------------------
			
CIS 3190 Homework #11
by Larry Schmieding
---------------------------
Directory exists
Writing to file e:/javadata/myfile
Done!!!
Directory exists
File exists
Reading from file myfile
From file: 5
From years file: 5
From years file: 5
From years file: 479343
From years file: 1920164712
From years file: 1694500418
From values file: 6.6542504E22
From values file: 3.7787318E22
From values file: 7.1557663E22
From values file: 4.615042E30
From values file: 61.352917
Done!!!
Characteristics of my cars:
Index Make
0 Porsche 1987 25500.0
1 Beatle 1974 2500.0
2 Ferrari 1985 175000.0
3 Buick 1990 8700.0
4 Jeep 1995 9000.0
5 Nissan 1996 13100.0
6 Chevy 1997 12000.0
7 Saturn 1991 5000.0

This is the code for it.

package Lab11;
import java.io.*;
			
public class Lab11
{
public static void main(String args[]) throws IOException
{

System.out.println("
---------------------------");
System.out.println("
CIS 3190 Homework #11");
System.out.println("by Larry Schmieding");
System.out.println("
---------------------------");

// create array of string data
String[] carnames = {"Porsche", "Beatle", "Ferrari",
"Buick", "Jeep"};
int[] years = {1987, 1974, 1985, 1990, 1995};
float[] values = {25500, 2500, 175000, 8700, 9000};

// identify data directory path and file
String filedir = "e:/javadata";
String fileid = "myfile";

// create data file
FileIO.fileout(filedir, fileid, carnames, years, values);

carnames = new String[8];
years = new int [8];
values = new float [8];

// read data from file into array
FileIO.filein(filedir, fileid, carnames, years, values);
carnames[0] = "Porsche";
carnames[1] = "Beatle";
carnames[2] = "Ferrari";
carnames[3] = "Buick";
carnames[4] = "Jeep";
carnames[5] = "Nissan";
carnames[6] = "Chevy";
carnames[7] = "Saturn";
years[0] = 1987;
years[1] = 1974;
years[2] = 1985;
years[3] = 1990;
years[4] = 1995;
years[5] = 1996;
years[6] = 1997;
years[7] = 1991;
values[0] = 25500;
values[1] = 2500;
values[2] = 175000;
values[3] = 8700;
values[4] = 9000;
values[5] = 13100;
values[6] = 12000;
values[7] = 5000;

// Declare, instantiate, and populate Car array
Car[] mycar = new Car[carnames.length];
for (int i = 0; i < mycar.length; i++)
mycar[i] = new Car(carnames[i]);
// Print mycar characteristics
System.out.println("
Characteristics of my cars:");
System.out.println("Index Make");
for (int i = 0; i < mycar.length; i++)
System.out.println(i + " " + mycar[i].name + " " + years[i] + " " + values[i]);

} // end method main
} // end class Lab11

//-------------------------------------------------------------
// Class FileIO
// by Chuck Mawhinney, 11/28/98
// Revised 11/1/06
// Reads and writes a simple file
//-------------------------------------------------------------
class FileIO
{
// Method to write a file.
public static void fileout(String outdir, String outfile,
String[] s, int[] years, float[] values) throws IOException
{
// Test to see if directory path exists
File myDir = new File(outdir);
File myFile = new File(myDir, outfile);
if (myDir.isDirectory())
System.out.println("Directory exists");
else
{
System.out.println("Directory does not exist");
if (myDir.mkdir())
System.out.println("Directory being created: "
+ outdir);
else
{
System.out.println("Directory could not be created, program may CRASH!!!");
return;
}
}

String outFile = outdir + "/" + outfile;

// set up file streams and filename
FileOutputStream fo_stream;
BufferedOutputStream bo_stream;
DataOutputStream do_stream;

fo_stream = new FileOutputStream(outFile);
bo_stream = new BufferedOutputStream(fo_stream);
do_stream = new DataOutputStream(bo_stream);

// write array length to file
System.out.println("Writing to file " + outFile);
do_stream.writeInt (s.length);
do_stream.writeInt(years.length);
do_stream.writeInt(values.length);

// write array to file
for (int i = 0; i < s.length; i++)
{
do_stream.writeUTF (s[i]);
}
System.out.println("Done!!!");
for (int i = 0; i < years.length; i++)
{
do_stream.writeInt(years[i]);
}
for (int i = 0; i < values.length; i++)
{
do_stream.writeFloat(values[i]);
}
// flush and close output streams
do_stream.flush();
do_stream.close();

} // end method fileout

public static void filein(String indir, String infile,
String[] s, int[] years, float[] values) throws IOException
{
int lth; // to store array length

// Test to see if directory path and file exist
File myDir = new File(indir);
File myFile = new File(myDir, infile);
if (myDir.isDirectory())
{
System.out.println("Directory exists");
if (myFile.exists())
System.out.println("File exists");
else
{
System.out.println("File does not exist. Aborting.");
return;
}
}
else
{
System.out.println("Directory does not exist. Aborting.");
return;
}

String inFile = indir + "/" + infile;


// set up file streams and filename
FileInputStream fi_stream;
BufferedInputStream bi_stream;
DataInputStream di_stream;

fi_stream = new FileInputStream(inFile);
bi_stream = new BufferedInputStream(fi_stream);
di_stream = new DataInputStream(bi_stream);

// read array length from file
System.out.println("Reading from file " + infile);
lth = di_stream.readInt (); // read first item
System.out.println("From file: " + lth); // display input
s = new String[lth]; // instantiate array
years = new int[lth];
values = new float[lth];

// read file into array
for (int i = 0; i < lth; i++)
if(lth > s.length)
{
s[i] = di_stream.readUTF(); // read next item
System.out.println("From car file: " + s[i]); // display input
}
for (int i = 0; i < years.length; i++)
{
years[i] = di_stream.readInt(); // read next item
System.out.println("From years file: " + years[i]); // display input
}
for (int i = 0; i < values.length; i++)
{
values[i] = di_stream.readFloat(); // read next item
System.out.println("From values file: " + values[i]); // display input
}
System.out.println("Done!!!");
// streams
di_stream.close();

} // end method filein

} // end class FileIO

class Vehicle
{
protected String name;
protected Vehicle (String vname)
{
name = vname;
} // end constructor Vehicle
} // end class Vehicle

class Car extends Vehicle
{
int years;
float values;
public Car (String carname)
{
super (carname);
years = years;
values = values;
} // end constructor car
} // end class Car
				
Can anybody help me out with it and email what is wrong with with it

my email address is at jschmied@mscd.edu

thank

Larry Schmieding
   <<Less
About | Sitemap | Contact