Getting file into an array of byte..

G

gbattine

Hi guys,
i first thank you for all the help you've given in my precedent posts.
Now i've to ask you a question.
I need to convert some lines of a txt file(obtained from an excel
table) into an array of byte in the best possible manner.
My file can have a various number of rows and columns.
With this code


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);



I read the file, i change , with . and line to line i calculate the
number of value(that have to be the same,i do the control for showing
exception if necessary).

Now i know from my file what are the number of values in a row(columns
of my old excel table).
I have to convert this file into an array of byte.
How can i do it?
Suppose we have only 3 lines, the first line is ever only string,the
others lines have a string and other double values.....

nome giuseppe carmine paolo
valerio 23.3 34.3 43.3
paolo 21.2 34.5 12.2

each value is separated from another one by one or more whitespaces..

The most important thing is that in a second moment i have to read this
array of byte and showing its original content,so i have to put in the
array some different value and different line symbol delimiter.
Can you help me giving me some idea or code?(please be clear,i'm
inexpert of java...)
Thanks very much,i need your help
 
M

Moiristo

gbattine said:
Hi guys,
i first thank you for all the help you've given in my precedent posts.
Now i've to ask you a question.
I need to convert some lines of a txt file(obtained from an excel
table) into an array of byte in the best possible manner.
My file can have a various number of rows and columns.
With this code


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);



I read the file, i change , with . and line to line i calculate the
number of value(that have to be the same,i do the control for showing
exception if necessary).

Now i know from my file what are the number of values in a row(columns
of my old excel table).
I have to convert this file into an array of byte.
How can i do it?
Suppose we have only 3 lines, the first line is ever only string,the
others lines have a string and other double values.....

nome giuseppe carmine paolo
valerio 23.3 34.3 43.3
paolo 21.2 34.5 12.2

each value is separated from another one by one or more whitespaces..

The most important thing is that in a second moment i have to read this
array of byte and showing its original content,so i have to put in the
array some different value and different line symbol delimiter.
Can you help me giving me some idea or code?(please be clear,i'm
inexpert of java...)
Thanks very much,i need your help

I don't understand the problem. What should the bytearray contain?
Reading a file into bytearray is very easy, and if you use strings, the
String.getBytes() function could also be convenient. So, what's the
actual problem?
 
G

gbattine

thanks for your reply...
my file has about 50000 rows..........and i have to develop an
application that allows the upload of about 6000/7000 files....
so i have to optimize everythings......i want to use whitespace to
separate different value and ;different line

so in my array of bytes i want nome(converted in byte) whitespace(in
byte) giuseppe (in byte) whitespace(in byte)carmine(in byte)
whitespace(in byte) paolo (in byte) ;(in byte);
valerio(in byte) whitespace(in byte) 23.3 (in byte) whitespace(in byte)
34.3 (in byte) whitespace(in byte)43.3 (in byte) ;(in byte) etc.....

So i should have an array of byte lower than the array of byte obtained
converting each line as a string into bytes.......
do you understand what's my problem?
How can i create an array of byte without fixing its initial dimension
that is able to grows up for each line read?
Excuse for my poor english..
In the past i've used a vector to temporary store the data while
reading the file and at the end i copied the vector in the array of
byte but when i do upload in my application i had java heap size error.
So someone has suggested me to improve my storing method and i've
decided to eliminate vector....
can you help me......????
please....i need your help..



Moiristo ha scritto:
 
B

Brandon McCombs

gbattine said:
thanks for your reply...
my file has about 50000 rows..........and i have to develop an
application that allows the upload of about 6000/7000 files....
so i have to optimize everythings......i want to use whitespace to
separate different value and ;different line

so in my array of bytes i want nome(converted in byte) whitespace(in
byte) giuseppe (in byte) whitespace(in byte)carmine(in byte)
whitespace(in byte) paolo (in byte) ;(in byte);
valerio(in byte) whitespace(in byte) 23.3 (in byte) whitespace(in byte)
34.3 (in byte) whitespace(in byte)43.3 (in byte) ;(in byte) etc.....

So i should have an array of byte lower than the array of byte obtained
converting each line as a string into bytes.......
do you understand what's my problem?
How can i create an array of byte without fixing its initial dimension
that is able to grows up for each line read?
Excuse for my poor english..
In the past i've used a vector to temporary store the data while
reading the file and at the end i copied the vector in the array of
byte but when i do upload in my application i had java heap size error.
So someone has suggested me to improve my storing method and i've
decided to eliminate vector....
can you help me......????
please....i need your help..

Have you attempted to do your "upload" in stages so you do not do all
50000 rows at the same time but instead perform them in groups of 10000
for example?

Read data from your text file, process it, upload it, flush your
buffers, repeat. I'm not an experienced programmer (couple years) but
that's how I'd do it if I had the problem.
 
G

gbattine

thanks for you reply,
i think it's a good idea and i read about it on other forums....
but how can i do?
My questio is:
i use a vector to temporary store line to line the file and at the end
i copy the vector into the array of byte.
Someones hat told me this is the cause of my java heap size problem,
not only the big rows number, but using of vector.
Can you post me some code to show me how can i add different lines of
my file into an array of byte?
thanks very much,i need your help
 
M

Moiristo

gbattine said:
thanks for you reply,
i think it's a good idea and i read about it on other forums....
but how can i do?
My questio is:
i use a vector to temporary store line to line the file and at the end
i copy the vector into the array of byte.
Someones hat told me this is the cause of my java heap size problem,
not only the big rows number, but using of vector.
Can you post me some code to show me how can i add different lines of
my file into an array of byte?
thanks very much,i need your help

As Brandon said, I think you should not process all rows at once. Rather
collect some rows, do the processing for those rows, and again read some
rows. I don't know whether the vector is necessary, but if you don't add
so much data to it at once, you should be fine.
 
G

gbattine

thanks guys,
i was sure of your help.....
an only question:
how can i read 10000 rows of 50000?
With this function i read the file into an array of string(the first
line) and into an arraylist of riga objects(riga is composed by a
string and an array ou double).
How can i modify this code to stop reading at 10000 rows?
Can you help me?

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++;
}
 
M

Moiristo

gbattine said:
String line = null;
while ((line = br.readLine()) != null) {

I would just introduce a counter:

String line = null;
int counter = 0;

while ((line = br.readLine())!=null && counter<10000){
...proces...
counter++;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,113
Latest member
KetoBurn
Top