G
gbattine
Hi guys,
i'm a pratical question for you.
I've developed a jas application that reads a txt file and convert it
into an array of byte,fro putting it into a blob field of a db mysql.
With big file i go in heap size memory!
I want to improve my function that makes it,but i'm a bit
inexpert...can you help me?
I have a file so made...
Each line has the same number of value,but i know this number only when
i read the file
string1 string2 string3................
stringx double double......(ever double)
stringy double double......(ever double)
and other lines equals to them from the second.
Only the first line has only strings.
I have read this file using a string array to read the first line and
an arraylist of "Riga" object to read the others lines.....i've used a
vector to store partial data from lines reading and at the end i've
created an array of byte in which i copy the vector.
Can you help me improving my code?
The file has about 50000 rows......so i go in heap memory size...
i know my code isn't too optimized.....can you help me?Please,i'm a
newbie,help me with code if possible...
When i encode the objects i've created into an array of byte i use a
whitespace to separe different value and a ; to separe different lines.
I've done it because in a second moment i've to read the array of byte.
Thanks...this is my code
public class MyBean {
private UploadedFile myFile;
private ArrayList rows = new ArrayList();
private List lines = new ArrayList();
public MyBean(){
};
public List getLines(){
return lines;
}
public void setLines(List lines){
this.lines=lines;
}
public boolean insRighe(Riga nuovo){
return rows.add(nuovo);
}
public UploadedFile getMyFile() {
return myFile;
}
public void setMyFile(UploadedFile myFile) {
this.myFile = myFile;
}
public ArrayList getRows() {
return rows;
}
public void setRows(ArrayList rows) {
this.rows = rows;
}
public String carica() throws IOException {
Riga r;
Double val[];
Head h;
int col=0;
int row=0;
byte middlerow=' ';
byte endrow=';';
byte[] data=null;
Vector temp=new Vector();
int numberOfNumericColumns=0;
String geneid=null;
String g=null;
String[]intest=null;
BufferedReader br = new BufferedReader(new
InputStreamReader(myFile.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
line = line.replace (',', '.');
StringTokenizer st = new StringTokenizer(line);
numberOfNumericColumns = (st.countTokens()-1);
col=(numberOfNumericColumns+1);
//se siamo nella prima riga(contatore segna 0)
if(row==0){
intest=new String[col];
int j=0;
while(st.hasMoreTokens()){
intest[j]=(st.nextToken().trim());
j++;
}
h=new Head(intest);//crei l'oggetto head
String []qa=h.getHvalues();
String asd="";
for(int i=0;i<=qa.length-1;i++){
asd=asd.concat(qa+" ");
}
System.out.println("head "+asd);//stampo contenuto
dell' head
row=1;
}//fine if
else
{
Double[] values=new Double[numberOfNumericColumns];
int z=0;
geneid=st.nextToken();
while (st.hasMoreTokens()) {
String app=st.nextToken();
values[z]=Double.valueOf(app);
z++;
}
r=new Riga(geneid,values); //crei l'oggetto riga
System.out.println("riga");
System.out.println(r.getgeneid());
values=r.getvalues();
for(int e=0;e<values.length;e++){
System.out.println(values[e]);
}
insRighe(r); //aggiungi
}
row++;
}
int i = 0;
while (i < intest.length) {
byte[] bytesnew = intest.getBytes();
// temp.addAll(bytesnew);
// memorizza in byte un elemento del vettore alla volta
for (byte b : bytesnew)
temp.add(new Byte(b)); // provare Byte
// temp.addElement(intest.getBytes());
temp.addElement(Byte.valueOf(middlerow));
i++;
}
temp.addElement(Byte.valueOf(endrow));
System.out.println("Intestazione convertita in byte");
for (int l = 0; l < rows.size(); l++) {
r = (Riga) rows.get(l);
g = r.getgeneid();
// temp.addElement(g.getBytes());
byte[] byte2 = g.getBytes();
for (byte c : byte2)
temp.add(new Byte(c));
temp.addElement(Byte.valueOf(middlerow));
val = r.getvalues();
byte[] tempByte1;
for (int e = 0; e <= val.length - 1; e++) {
// Returns a string representation of the double argument.
tempByte1 = Double.toString(val[e]).getBytes();
for (int j = 0; j < tempByte1.length; j++) {
temp.addElement(Byte.valueOf(tempByte1[j]));
}
temp.addElement(Byte.valueOf(middlerow));
}
temp.addElement(Byte.valueOf(endrow));
}
data = new byte[temp.size()];
for (int t = 0; t < temp.size(); t++) {
data[t] = (((Byte) temp.elementAt(t)).byteValue());
}
return data;
}
i'm a pratical question for you.
I've developed a jas application that reads a txt file and convert it
into an array of byte,fro putting it into a blob field of a db mysql.
With big file i go in heap size memory!
I want to improve my function that makes it,but i'm a bit
inexpert...can you help me?
I have a file so made...
Each line has the same number of value,but i know this number only when
i read the file
string1 string2 string3................
stringx double double......(ever double)
stringy double double......(ever double)
and other lines equals to them from the second.
Only the first line has only strings.
I have read this file using a string array to read the first line and
an arraylist of "Riga" object to read the others lines.....i've used a
vector to store partial data from lines reading and at the end i've
created an array of byte in which i copy the vector.
Can you help me improving my code?
The file has about 50000 rows......so i go in heap memory size...
i know my code isn't too optimized.....can you help me?Please,i'm a
newbie,help me with code if possible...
When i encode the objects i've created into an array of byte i use a
whitespace to separe different value and a ; to separe different lines.
I've done it because in a second moment i've to read the array of byte.
Thanks...this is my code
public class MyBean {
private UploadedFile myFile;
private ArrayList rows = new ArrayList();
private List lines = new ArrayList();
public MyBean(){
};
public List getLines(){
return lines;
}
public void setLines(List lines){
this.lines=lines;
}
public boolean insRighe(Riga nuovo){
return rows.add(nuovo);
}
public UploadedFile getMyFile() {
return myFile;
}
public void setMyFile(UploadedFile myFile) {
this.myFile = myFile;
}
public ArrayList getRows() {
return rows;
}
public void setRows(ArrayList rows) {
this.rows = rows;
}
public String carica() throws IOException {
Riga r;
Double val[];
Head h;
int col=0;
int row=0;
byte middlerow=' ';
byte endrow=';';
byte[] data=null;
Vector temp=new Vector();
int numberOfNumericColumns=0;
String geneid=null;
String g=null;
String[]intest=null;
BufferedReader br = new BufferedReader(new
InputStreamReader(myFile.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
line = line.replace (',', '.');
StringTokenizer st = new StringTokenizer(line);
numberOfNumericColumns = (st.countTokens()-1);
col=(numberOfNumericColumns+1);
//se siamo nella prima riga(contatore segna 0)
if(row==0){
intest=new String[col];
int j=0;
while(st.hasMoreTokens()){
intest[j]=(st.nextToken().trim());
j++;
}
h=new Head(intest);//crei l'oggetto head
String []qa=h.getHvalues();
String asd="";
for(int i=0;i<=qa.length-1;i++){
asd=asd.concat(qa+" ");
}
System.out.println("head "+asd);//stampo contenuto
dell' head
row=1;
}//fine if
else
{
Double[] values=new Double[numberOfNumericColumns];
int z=0;
geneid=st.nextToken();
while (st.hasMoreTokens()) {
String app=st.nextToken();
values[z]=Double.valueOf(app);
z++;
}
r=new Riga(geneid,values); //crei l'oggetto riga
System.out.println("riga");
System.out.println(r.getgeneid());
values=r.getvalues();
for(int e=0;e<values.length;e++){
System.out.println(values[e]);
}
insRighe(r); //aggiungi
}
row++;
}
int i = 0;
while (i < intest.length) {
byte[] bytesnew = intest.getBytes();
// temp.addAll(bytesnew);
// memorizza in byte un elemento del vettore alla volta
for (byte b : bytesnew)
temp.add(new Byte(b)); // provare Byte
// temp.addElement(intest.getBytes());
temp.addElement(Byte.valueOf(middlerow));
i++;
}
temp.addElement(Byte.valueOf(endrow));
System.out.println("Intestazione convertita in byte");
for (int l = 0; l < rows.size(); l++) {
r = (Riga) rows.get(l);
g = r.getgeneid();
// temp.addElement(g.getBytes());
byte[] byte2 = g.getBytes();
for (byte c : byte2)
temp.add(new Byte(c));
temp.addElement(Byte.valueOf(middlerow));
val = r.getvalues();
byte[] tempByte1;
for (int e = 0; e <= val.length - 1; e++) {
// Returns a string representation of the double argument.
tempByte1 = Double.toString(val[e]).getBytes();
for (int j = 0; j < tempByte1.length; j++) {
temp.addElement(Byte.valueOf(tempByte1[j]));
}
temp.addElement(Byte.valueOf(middlerow));
}
temp.addElement(Byte.valueOf(endrow));
}
data = new byte[temp.size()];
for (int t = 0; t < temp.size(); t++) {
data[t] = (((Byte) temp.elementAt(t)).byteValue());
}
return data;
}