I'm not very good at writting in
java so I wrote this code in c++. but now I need to convert it into java. Can someone help me do this?
[code]
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main(int argc, char *argv[]){
if (argc !=2){
cerr << endl << "Error: syntax for command should be: " << argv[0] << " filename\n\n";
exit(1);
}
int i = 0, elements = 0;
char *ch;
char *buffer[13];
// Open file specified by argument one
ifstream fin(argv[1], ios::in);
if (!fin || fin.bad() || fin.eof() ){
cerr << endl << "Error: file not found\n\n" ;
exit(1);
}
// Get file size
cout << "Attempting to read file length... ";
fin.seekg(0,ios::end);
i = fin.tellg();
fin.seekg(0);
// Allocate memory for file content
ch = new char [i];
// read contents of infile into buffer
while(!fin.eof()){
fin.getline(ch, i);
buffer[elements] = ch;
cout<< "ch [" << elements << "]:" << ch << endl;
cout<< "buffer [" << elements << "]:" << buffer[elements] << endl;
elements++;
}
cout << "\nNumber of elements in file: " << elements << "\n";
// display file contents
for (i = 0; i < elements; i++)
cout << "Element [" << i << "]: " << buffer[i] << endl;
delete[] ch;
cout << "\nData read succesfully.\n\n" ;
fin.close();
return 0;
}
[\code]