Tokenize file

Tokenize file 

Create a program that takes in three parameters: an input file name, an output file name, and a variable that decides between tokenization and serialization.  The program should open the file and, based on the final variable, either turn the contents into tokens or deserialize them.

If they are tokens, have the program perform an operation on them of your choosing.  Printing them out in the same order they were read is not acceptable.

If they are objects, have the program invoke a method of the object class to each element.

Then it should write the new versions to the output file.

The objects you use should either be objects already understood by Java or you should provide the class file as part of your submission. 

Solution 

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package Solution;

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

import java.io.*;

importjava.lang.*;

importjava.util.LinkedList;

importjava.util.Queue;

importjava.util.Scanner;

public class Solution {

public static void main (String[] args) throws FileNotFoundException,InterruptedException,IOException, ClassNotFoundException{

Scanner in = new Scanner(System.in);

System.out.println(“give input file name”);

String input = in.next();

System.out.println(“give output file name”);

String out = in.next();

System.out.println(“input 1 for tokenization or 2 for deserialization”);

int v = in.nextInt();

boolean status = true;

while(status){

if(v==1){

// My aim is to seperate words and numbers in the stream.

FileReader reader = new FileReader(input);

BufferedReaderbufferread = new BufferedReader(reader);

StreamTokenizerst = new StreamTokenizer(bufferread);

Queue<String> q = new LinkedList<String>();

FileOutputStream fop = null;

File file;

file = new File(out);

fop = new FileOutputStream(file);

while(st.nextToken() != StreamTokenizer.TT_EOF){

if(st.ttype == StreamTokenizer.TT_WORD) {

q.add(st.sval);

} else if(st.ttype == StreamTokenizer.TT_NUMBER) {

double content = st.nval;

System.out.println(content);

String c = Double.toString(content);

c =  ” “+c+” “;

byte[] contentInBytes = c.getBytes();

fop.write(contentInBytes);

fop.flush();

} else if(st.ttype == StreamTokenizer.TT_EOL) {

System.out.println();

}

}

while(q.size()!=0){

String s = q.remove();

s =  ” “+s+” “;

byte[] contentInBytes = s.getBytes();

fop.write(contentInBytes);

fop.flush();

System.out.println(s);

}

fop.close();

status = false;

}

else if(v==2){

FileInputStreamfis = null;

FileOutputStreamfoso = null;

FileOutputStreamfos = null;

/*    foso = new FileOutputStream(input);

ObjectOutputStreamooso = new ObjectOutputStream(foso);

String s=”sa”;

for(int i=0;i<10;i++){

ooso.writeObject(s);

}

foso.close();*/

fis = new FileInputStream(input);

ObjectInputStreamois = new ObjectInputStream(fis);

fos = new FileOutputStream(out);

ObjectOutputStreamoos = new ObjectOutputStream(fos);

while(fis.available()>0){

Object si = ois.readObject();

System.out.println(si.toString());

oos.writeObject(si);

}

fis.close();

fos.close();

ois.close();

oos.close();

status = false;

}

else{

System.out.println(“please enter 1 for tokenization or 2 for deserialization”);

v = in.nextInt();

}

 

}

}

}