Trying to read a CSV file !
Hi an greetings !
I am trying to read a csv file, here's my code, can someone tell me where's the problem cause I can't understand the exception, (sorry for bad english :) )
Code Java:
package Leitura;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Scanner;
import java.util.StringTokenizer;
public class LeitorCSV {
static LeitorCSV LerCSV(ObjectInputStream in) throws IOException {
return new LeitorCSV( in.readUTF(),
in.readUTF(),
in.readUTF(),
in.readUTF(),
in.readUTF()) ;
}
private String num_aluno;
private String Freq_Ant;
private String Freq;
private String Exame;
private String Resultado;
public LeitorCSV(String a,String b,String c,String d,String e) {
this.num_aluno=a;
this.Freq_Ant=b;
this.Freq=c;
this.Exame=d;
this.Resultado=e;
}
public static LeitorCSV LerCSV(DataInputStream in) throws IOException {
return new LeitorCSV( in.readUTF(),
in.readUTF(),
in.readUTF(),
in.readUTF(),
in.readUTF()) ;
}
}
in the main here is :
note: I got some imports not used cause I am separating the problem first. once I solve this one I will go to the next :)
Re: Trying to read a CSV file !
Quote:
where's the problem
Is there a problem? Can you explain what it is?
Show: what the program does, why that is wrong and what you want it to do.
Copy and paste here the full text of any error messages.
Re: Trying to read a CSV file !
yes this is the problem, sorry , I forgot to post it with the code.
Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: 55433B55
at java.io.ObjectInputStream.readStreamHeader(ObjectI nputStream.java:782)
at java.io.ObjectInputStream.<init>(ObjectInputStream .java:279)
at Leitura.TesteLeitor.main(TesteLeitor.java:12)
Java Result: 1
Re: Trying to read a CSV file !
How was the file you are trying to read created?
The ObjectInputStream constructor doesn't think that the file is in the correct format.
Was the file created by an ObjectOutputStream?
Re: Trying to read a CSV file !
the file content is :
1010001;--- ;NF ; ;
1010002;--- ;16;14;15
1010010;--- ;12;8;10
1010016;--- ;NF ; ;
1010019;12;12;7;SM
the strings that I wanted to read separated by commas
no I created the file.
Re: Trying to read a CSV file !
That is not from an ObjectOutputStream. It looks like a text file.
Why are you using the ObjectInputStream?
There are other ways to read a text file. The Scanner class is a simple one.