I am kind of stuck, I usually know how to create single csv, it looks like I am missing or disconnecting from this code. I am not able to create multiple csv file from Pojo class. The file usually is more than 15mb, but I need to split into multiple csv file like 5mb each. Any suggestion would be great helped. Here is sample code that I am trying but failing.
public static void main(String[] args) throws IOException {
getOrderList();
}
public static void getOrderList() throws IOException {
List<Orders> ordersList = new ArrayList<>();
Orders orders = new Orders();
orders.setOrderNumber("1");
orders.setProductName("mickey");
Orders orders1 = new Orders();
orders1.setOrderNumber("2");
orders1.setProductName("mini");
ordersList.add(orders);
ordersList.add(orders1);
Object [] FILE_HEADER = {"orderNumber","productName"};
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int rowCount = 0;
int fileCount = 1;
try {
BufferedWriter fileWriter = new BufferedWriter(new OutputStreamWriter(byteArrayOutputStream));
CSVPrinter csvFilePrinter = new CSVPrinter(fileWriter,
CSVFormat.DEFAULT.withRecordSeparator("\n"));
csvFilePrinter.printRecord(FILE_HEADER);
for (Orders patient : ordersList) {
rowCount++;
patient.getOrderNumber();
patient.getProductName();
if (rowCount <= 1) {
csvFilePrinter.printRecord(patient);
csvFilePrinter.flush();
}
if (rowCount > 1 ) {
csvFilePrinter.printRecord(patient);
fileCount++;
csvFilePrinter.flush();
}
}
} catch (IOException e) {
throw new RuntimeException("Cannot generate csv file", e);
}
byte[] csvOutput = byteArrayOutputStream.toByteArray();
OutputStream outputStream = null;
outputStream = new FileOutputStream("demos" + fileCount + ".csv");
byteArrayOutputStream = new ByteArrayOutputStream();
byteArrayOutputStream.write(csvOutput);
byteArrayOutputStream.writeTo(outputStream);
}
public static class Orders {
private String orderNumber;
private String productName;
public String getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
}
Related
I'm writing a fairly simple program, which needs to save some simple data between runs. This data is defined by UserData, outlined below:
public class UserData implements Serializable {
/**
*
*/
private static final long serialVersionUID = -3542558265070011448L;
public static ArrayList<String> projectList;
public static ArrayList<Bill> billList;
public static String userName;
public static String userEmail;
public UserData() {
}
public UserData(String name, String email) {
super();
userName = name;
userEmail = email;
projectList = new ArrayList<String>();
billList = new ArrayList<Bill>();
}
public String getUserName() {
return userName;
}
public String getUserEmail() {
return userEmail;
}
public ArrayList<Bill> getBillList() {
return billList;
}
public ArrayList<String> getProjectList() {
return projectList;
}
public void setBillList(Bill theBill) {
billList.add(theBill);
}
public void setProjectList(String projectName) {
projectList.add(projectName);
}
}
I Then have a class which handles serializing/deserializing of this data and it's instance to a file, with the various calls done directly or indirectly by events in a separate Gui Class. This is it:
public class FileHandler implements Serializable {
/**
*
*/
private static final long serialVersionUID = 473118590700911358L;
private static JFileChooser fileChooser;
public UserData myUserData;
public FileHandler() throws ClassNotFoundException, IOException {
fileChooser = new JFileChooser();
initData();
}
public FileHandler(UserData newUser) {
fileChooser = new JFileChooser();
myUserData = newUser;
System.out.println("Entered User: " + myUserData.getUserName());
System.out.println("Entered User: " + myUserData.getUserEmail());
}
private void createProgramData() throws FileNotFoundException, IOException {
FileOutputStream fileOut = new FileOutputStream("data\\ProgramData.diy");
ObjectOutputStream encoderp = new ObjectOutputStream(fileOut);
System.out.println("createProgramData: " + myUserData.getUserName());
System.out.println("createProgramData: "+ myUserData.getUserEmail());
encoderp.writeObject(myUserData);
encoderp.close();
fileOut.close();
}
public void exportData() throws FileNotFoundException, IOException {
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
final int selectedFile = fileChooser.showOpenDialog(null);
ObjectOutputStream encodere = null;
if (selectedFile == JFileChooser.APPROVE_OPTION) {
final File selectedPath = fileChooser.getSelectedFile();
FileOutputStream fileOut = new FileOutputStream(selectedPath + "\\UserData.diy");
encodere = new ObjectOutputStream(fileOut);
System.out.println("Writing Data: " + myUserData.getUserName());
System.out.println("Writing Data: " + myUserData.getUserEmail());
encodere.writeObject(myUserData);
encodere.close();
fileOut.close();
}
}
public void importData() throws ClassNotFoundException, IOException {
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
final int selectedFile = fileChooser.showSaveDialog(null);
ObjectInputStream decoderim = null;
if (selectedFile == JFileChooser.APPROVE_OPTION) {
final File selectedPath = fileChooser.getSelectedFile();
FileInputStream fileIn = new FileInputStream(selectedPath);
decoderim = new ObjectInputStream(fileIn);
myUserData = (UserData)decoderim.readObject();
decoderim.close();
fileIn.close();
System.out.println("importing Data: " + myUserData.getUserEmail());
System.out.println("importing Data: " + myUserData.getUserName());
}
}
private void initData() throws IOException, ClassNotFoundException {
FileInputStream fileIn = new FileInputStream("data\\ProgramData.diy");
ObjectInputStream decoderi = new ObjectInputStream(fileIn);
myUserData = (UserData)decoderi.readObject();
decoderi.close();
fileIn.close();
System.out.println("initializing Data: " + myUserData.getUserName());
System.out.println("Initializing Data: " + myUserData.getUserEmail());
}
public UserData getUserData() {
return myUserData;
}
A problem I'm having with the ObjectInputStream and possibly ObjectOutputStream seems to be that when I serialize UserData, exit my program, and then re-enter and try to import that file, the instance of UserData remains unchanged. I can't seem to figure out what I'm missing. Even pointing out something I've overlooked helps.
UserData only has static fields, and static fields aren't serialized. From the look of your code they should all be instance members. Don't make anything static unless you know exactly why you are doing so.
I am experimenting with OpenNlp 1.7.2 and maxent-3.0.0.jar to train for thai language , below is the code that reads thai train data and creates the bin model.
public class TrainPerson {
public static void main(String[] args) throws IOException {
String trainFile = "/Documents/workspace/ThaiOpenNLP/bin/thaiPerson.train";
String modelFile = "/Documents/workspace/ThaiOpenNLP/bin/th-ner-person.bin";
writePersonModel(trainFile, modelFile);
}
private static void writePersonModel(String trainFile, String modelFile)
throws FileNotFoundException, IOException {
Charset charset = Charset.forName("UTF-8");
InputStreamFactory fileInputStream = new MarkableFileInputStreamFactory(new File(trainFile));
ObjectStream<String> lineStream = new PlainTextByLineStream(fileInputStream, charset);
ObjectStream<NameSample> sampleStream = new NameSampleDataStream(lineStream);
TokenNameFinderModel model;
try {
model = NameFinderME.train("th", "person", sampleStream , TrainingParameters.defaultParams(), new TokenNameFinderFactory());
} finally {
sampleStream.close();
}
BufferedOutputStream modelOut = null;
try {
modelOut = new BufferedOutputStream(new FileOutputStream(modelFile));
model.serialize(modelOut);
} finally {
if (modelOut != null) {
modelOut.close();
}
}
}}
Thai data looks like as attached in the file trainingData
I am using the output model to detect person name as shown in the below programme. It fails to identify the name.
public class ThaiPersonNameFinder {
static String modelFile = "/Users/avinashpaula/Documents/workspace/ThaiOpenNLP/bin/th-ner-person.bin";
public static void main(String[] args) {
try {
InputStream modelIn = new FileInputStream(new File(modelFile));
TokenNameFinderModel model = new TokenNameFinderModel(modelIn);
NameFinderME nameFinder = new NameFinderME(model);
String sentence[] = new String[]{
"จอห์น",
"30",
"ปี",
"จะ",
"เข้าร่วม",
"ก",
"เริ่มต้น",
"ขึ้น",
"บน",
"มกราคม",
"."
};
Span nameSpans[] = nameFinder.find(sentence);
for (int i = 0; i < nameSpans.length; i++) {
System.out.println(nameSpans[i]);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
What am i doing wrong.
I have a 'Person' class where i stored data like name, surname etc. I make 5 object type Person, add them to ArrayList, and save this ArrayList to file. Next i'm loading from this file ArrayList and i have 5 person. Problem is when i want save again for example 10 object Person. When i'm loading ArrayList from file i'm getting only 5 person from first writing. If i repeat this still i will have load data from first writing to this file. How i can fix this ?
public class Data {
static List<Person> persons = new ArrayList<Person>();
public static void main(String[] args) throws IOException {
Data.savePersons(5);
Data.loadPersons();
/** Clean 'persons' array for TEST of load data */
persons.removeAll(persons);
System.out.println("\n-----------\nNext Round\n-----------\n");
Data.savePersons(10);
Data.loadPersons();
}
/** Save a couple of Person Object to file C:/data.ser */
public static void savePersons(int noOfPersonToSave) throws IOException {
FileOutputStream fout = null;
ObjectOutputStream oos = null;
/** Make 5 'Person' object and add them to ArrayList 'persons' for example */
for (int i = 0; i < noOfPersonToSave; i++) {
Person personTest = new Person("name" + i, "surname" + i, "email" +i, "1234567890" +i);
persons.add(personTest);
}
try {
fout = new FileOutputStream("C:\\data.ser", true);
oos = new ObjectOutputStream(fout);
oos.writeObject(persons);
System.out.println("Saving '" + persons.size() + "' Object to Array");
System.out.println("persons.size() = " + persons.size());
System.out.println("savePersons() = OK");
} catch (Exception ex) {
System.out.println("Saving ERROR: " + ex.getMessage());
} finally {
if (oos != null) {
oos.close();
}
}
}
/** Load previously saved a couple of Person Object in file C:/data.ser */
public static void loadPersons() throws IOException {
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream("C:\\data.ser");
ois = new ObjectInputStream(fis);
persons = (List<Person>) ois.readObject();
//persons.add(result);
System.out.println("-------------------------");
System.out.println("Loading '" + persons.size() + "' Object from Array");
System.out.println("persons.size() = " + persons.size());
System.out.println("loadPersons() = OK");
} catch (Exception e) {
System.out.println("-------------------------");
System.out.println("Loading ERROR: " + e.getMessage());
} finally {
if (ois != null) {
ois.close();
}
}
}}
class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String surname;
private String mail;
private String telephone;
public Person(String n, String s, String m, String t) {
name = n;
surname = s;
mail = m;
telephone = t;
}
public String getName() {
return name;
}
public String getSurname() {
return surname;
}
public String getMail() {
return mail;
}
public String getTelephone() {
return telephone;
}}
new FileOutputStream("C:\\data.ser", true)
You're passing true for the append parameter. So you're appending a list of 10 persons to the file, after the already existing list of 5 people. And since you only read one list, you read the first you wrote, which contains 5 persons.
Pass false instead of true.
I am struggling to find a good example on how to read and write data in my android app using GSON. Could someone please show me or point me to a good example? I am using this for data persistence between activities.
My professor gave this example to for writing:
Vector v = new Vector(10.0f, 20.0f);
Gson gson = new Gson();
String s = gson.toJson(v);
How would I go about saving that to a file?
How to save your JSON into a file on internal storage:
String filename = "myfile.txt";
Vector v = new Vector(10.0f, 20.0f);
Gson gson = new Gson();
String s = gson.toJson(v);
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(s.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
How to read it back:
FileInputStream fis = context.openFileInput("myfile.txt", Context.MODE_PRIVATE);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
String json = sb.toString();
Gson gson = new Gson();
Vector v = gson.fromJson(json, Vector.class);
Simple Gson example:
public class Main {
public class Power {
private String name;
private Long damage;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getDamage() {
return damage;
}
public void setDamage(Long damage) {
this.damage = damage;
}
public Power() {
super();
}
public Power(String name, Long damage) {
super();
this.name = name;
this.damage = damage;
}
#Override
public String toString() {
return "Power [name=" + name + ", damage=" + damage + "]";
}
}
public class Warrior {
private String name;
private Power power;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Power getPower() {
return power;
}
public void setPower(Power power) {
this.power = power;
}
public Warrior() {
super();
}
public Warrior(String name, Power power) {
super();
this.name = name;
this.power = power;
}
#Override
public String toString() {
return "Warrior [name=" + name + ", power=" + power.toString() + "]";
}
}
public static void main(String[] args) {
Main m = new Main();
m.run();
}
private void run() {
Warrior jake = new Warrior("Jake the dog", new Power("Rubber hand", 123l));
String jsonJake = new Gson().toJson(jake);
System.out.println("Json:"+jsonJake);
Warrior returnToWarrior = new Gson().fromJson(jsonJake, Warrior.class);
System.out.println("Object:"+returnToWarrior.toString());
}
}
Anyways checkout the documentation.
And to persist something in your application you can start with something simple like ORMlite.
Hope this help! :]
UPDATE:
If you really want write the json in a file:
File myFile = new File("/sdcard/myjsonstuff.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =new OutputStreamWriter(fOut);
myOutWriter.append(myJsonString);
myOutWriter.close();
fOut.close();
And if you want to read:
File myFile = new File("/sdcard/myjsonstuff.txt");
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = ""; //Holds the text
while ((aDataRow = myReader.readLine()) != null)
{
aBuffer += aDataRow ;
}
myReader.close();
Also add: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
to your manifest.
But, seriously is so much better use a ORM and store the records in the db. I don't know why you need save the json data in a file, but if I was you, I will use the ORM way.
Maybe in more recent version, but toJson accepts writer that directly writes to file.
ex.:
Vector v = new Vector(10.0f, 20.0f);
Gson gson = new GsonBuilder().create();
Writer writerJ = new FileWriter("keep.json");
gson.toJson(v,writerJ);
Save your class in SharedPrefrences using
public static void saveYourClassInSharedPref(ClassToSave ClassToSave) {
try{
String json = "";
if(ClassToSave != null){
json = new Gson().toJson(ClassToSave);
}
SharedPref.save(KeysSharedPrefs.ClassToSave, json);
}catch (Exception ex){
ex.printStackTrace();
}
}
public static ClassToSave readYourClassFromSharedPref() {
ClassToSave ClassToSave;
try{
String json = SharedPref.read(KeysSharedPrefs.ClassToSave, "");
if(!json.isEmpty()){
ClassToSave = new Gson().fromJson(json, ClassToSave.class);
return ClassToSave;
}
}catch (Exception ex){
ex.printStackTrace();
}
return null;
}
where SharedPref.java
public class SharedPref {
public static String read(String valueKey, String valueDefault) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(App.context);
return prefs.getString(valueKey, valueDefault);
}
public static void save(String valueKey, String value) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(App.context);
SharedPreferences.Editor edit = prefs.edit();
edit.putString(valueKey, value);
edit.commit();
}
}
You can also do this entirely with streams and avoid an intermediate object:
Vector v;
// This should be reused, so private static final
Gson gson = new GsonBuilder().create();
// Read from file:
try (InputStream fileIn = context.openFileInput("myfile.txt", Context.MODE_PRIVATE);
BufferedInputStream bufferedIn = new BufferedInputStream(fileIn, 65536);
Reader reader = new InputStreamReader(bufferedIn, StandardCharsets.UTF_8)) {
gson.fromJson(reader, Vector.class);
}
v = new Vector(10.0f, 20.0f);
// Write to file
try (OutputStream fileOut = context.openFileOutput(filename, Context.MODE_PRIVATE);
OutputStream bufferedOut = new BufferedOutputStream(fileOut, 65536);
Writer writer = new OutputStreamWriter(bufferedOut)) {
gson.toJson(v, writer);
}
Choose buffer sizes appropriately. 64k is flash-friendly, but silly if you only have 1k of data. try-with-resources might also not be supported by some versions of Android.
Is there any way InputStream wrapping a list of UTF-8 String? I'd like to do something like:
InputStream in = new XyzInputStream( List<String> lines )
You can read from a ByteArrayOutputStream and you can create your source byte[] array using a ByteArrayInputStream.
So create the array as follows:
List<String> source = new ArrayList<String>();
source.add("one");
source.add("two");
source.add("three");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (String line : source) {
baos.write(line.getBytes());
}
byte[] bytes = baos.toByteArray();
And reading from it is as simple as:
InputStream in = new ByteArrayInputStream(bytes);
Alternatively, depending on what you're trying to do, a StringReader might be better.
You can concatenate all the lines together to create a String then convert it to a byte array using String#getBytes and pass it into ByteArrayInputStream. However this is not the most efficient way of doing it.
In short, no, there is no way of doing this using existing JDK classes. You could, however, implement your own InputStream that read from a List of Strings.
EDIT: Dave Web has an answer above, which I think is the way to go. If you need a reusable class, then something like this might do:
public class StringsInputStream<T extends Iterable<String>> extends InputStream {
private ByteArrayInputStream bais = null;
public StringsInputStream(final T strings) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
for (String line : strings) {
outputStream.write(line.getBytes());
}
bais = new ByteArrayInputStream(outputStream.toByteArray());
}
#Override
public int read() throws IOException {
return bais.read();
}
#Override
public int read(byte[] b) throws IOException {
return bais.read(b);
}
#Override
public int read(byte[] b, int off, int len) throws IOException {
return bais.read(b, off, len);
}
#Override
public long skip(long n) throws IOException {
return bais.skip(n);
}
#Override
public int available() throws IOException {
return bais.available();
}
#Override
public void close() throws IOException {
bais.close();
}
#Override
public synchronized void mark(int readlimit) {
bais.mark(readlimit);
}
#Override
public synchronized void reset() throws IOException {
bais.reset();
}
#Override
public boolean markSupported() {
return bais.markSupported();
}
public static void main(String[] args) throws Exception {
List source = new ArrayList();
source.add("foo ");
source.add("bar ");
source.add("baz");
StringsInputStream<List<String>> in = new StringsInputStream<List<String>>(source);
int read = in.read();
while (read != -1) {
System.out.print((char) read);
read = in.read();
}
}
}
This basically an adapter for ByteArrayInputStream.
You can create some kind of IterableInputStream
public class IterableInputStream<T> extends InputStream {
public static final int EOF = -1;
private static final InputStream EOF_IS = new InputStream() {
#Override public int read() throws IOException {
return EOF;
}
};
private final Iterator<T> iterator;
private final Function<T, byte[]> mapper;
private InputStream current;
public IterableInputStream(Iterable<T> iterable, Function<T, byte[]> mapper) {
this.iterator = iterable.iterator();
this.mapper = mapper;
next();
}
#Override
public int read() throws IOException {
int n = current.read();
while (n == EOF && current != EOF_IS) {
next();
n = current.read();
}
return n;
}
private void next() {
current = iterator.hasNext()
? new ByteArrayInputStream(mapper.apply(iterator.next()))
: EOF_IS;
}
}
To use it
public static void main(String[] args) throws IOException {
Iterable<String> strings = Arrays.asList("1", "22", "333", "4444");
try (InputStream is = new IterableInputStream<String>(strings, String::getBytes)) {
for (int b = is.read(); b != -1; b = is.read()) {
System.out.print((char) b);
}
}
}
In my case I had to convert a list of string in the equivalent file (with a line feed for each line).
This was my solution:
List<String> inputList = Arrays.asList("line1", "line2", "line3");
byte[] bytes = inputList.stream().collect(Collectors.joining("\n", "", "\n")).getBytes();
InputStream inputStream = new ByteArrayInputStream(bytes);
You can do something similar to this:
https://commons.apache.org/sandbox/flatfile/xref/org/apache/commons/flatfile/util/ConcatenatedInputStream.html
It just implements the read() method of InputStream and has a list of InputStreams it is concatenating. Once it reads an EOF it starts reading from the next InputStream. Just convert the Strings to ByteArrayInputStreams.
you can also do this way create a Serializable List
List<String> quarks = Arrays.asList(
"up", "down", "strange", "charm", "top", "bottom"
);
//serialize the List
//note the use of abstract base class references
try{
//use buffering
OutputStream file = new FileOutputStream( "quarks.ser" );
OutputStream buffer = new BufferedOutputStream( file );
ObjectOutput output = new ObjectOutputStream( buffer );
try{
output.writeObject(quarks);
}
finally{
output.close();
}
}
catch(IOException ex){
fLogger.log(Level.SEVERE, "Cannot perform output.", ex);
}
//deserialize the quarks.ser file
//note the use of abstract base class references
try{
//use buffering
InputStream file = new FileInputStream( "quarks.ser" );
InputStream buffer = new BufferedInputStream( file );
ObjectInput input = new ObjectInputStream ( buffer );
try{
//deserialize the List
List<String> recoveredQuarks = (List<String>)input.readObject();
//display its data
for(String quark: recoveredQuarks){
System.out.println("Recovered Quark: " + quark);
}
}
finally{
input.close();
}
}
catch(ClassNotFoundException ex){
fLogger.log(Level.SEVERE, "Cannot perform input. Class not found.", ex);
}
catch(IOException ex){
fLogger.log(Level.SEVERE, "Cannot perform input.", ex);
}
I'd like to propose my simple solution:
public class StringListInputStream extends InputStream {
private final List<String> strings;
private int pos = 0;
private byte[] bytes = null;
private int i = 0;
public StringListInputStream(List<String> strings) {
this.strings = strings;
this.bytes = strings.get(0).getBytes();
}
#Override
public int read() throws IOException {
if (pos >= bytes.length) {
if (!next()) return -1;
else return read();
}
return bytes[pos++];
}
private boolean next() {
if (i + 1 >= strings.size()) return false;
pos = 0;
bytes = strings.get(++i).getBytes();
return true;
}
}