Below is the method I used to create test.kryo, which was converted from a CSV file: vertices.txt
package mypkg;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.util.Arrays;
public class CsvToJavaObject {
public static void main(String[] args) {
String csvFile = "/home/dev/wanmeng/kryo/mypkg/vertices.txt";
BufferedReader br = null;
String line = "";
String csvSplitBy = "\n";
List<Long> list = new ArrayList<Long>();
File file;
FileOutputStream fop = null;
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
//trim the line breaks at the end of line.
line = line.trim();
// adding Long objects to a list.
list.add(Long.parseLong(line));
//System.out.println(list);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
try{
Kryo kryo = new Kryo();
file = new File("test.kryo");
fop = new FileOutputStream(file);
//if the file doesnt exist, then create it.
if(!file.exists()) {
file.createNewFile();
}
Output output = new Output(fop);
kryo.writeObject(output, list);
output.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
try{
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void deserialize() {
File file;
FileInputStream fip = null;
// deserialize
try{
Kryo kryo = new Kryo();
file = new File("test.kryo");
fip = new FileInputStream(file);
if(!file.exists()) {
file.createNewFile();
}
Input input = new Input(fip);
//ArrayList<Long> listin = kryo.readObject(input, Long.class);
Long listin = kryo.readObject(input, Long.class);
//System.out.println(Arrays.toString(listin.toArray()));
System.out.println(listin);
input.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
try{
if (fip != null) {
fip.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
javac -d . CsvToJavaObject.java