Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Trying to read a CSV file !

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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 :

    package Leitura;
     
    import java.io.*;
    import java.util.LinkedList;
     
    public class TesteLeitor {
     
        private static String ficheiro = "C:\\csv_file.csv";
        public static void main(String[] args) throws IOException,ClassNotFoundException {
     
            FileInputStream file = new FileInputStream(ficheiro);
            ObjectInputStream in = new ObjectInputStream(file);
     
            LinkedList <LeitorCSV> L = new LinkedList<LeitorCSV>();
     
            while( file.available() > 0 ) {
                L.add(LeitorCSV.LerCSV(in));
            }
            in.close();
     
            System.out.println("Leitura concluida...");
     
            for (LeitorCSV c : L) {
                System.out.println( c.toString() );
            }
        }
    }

    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trying to read a CSV file !

    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.

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    Last edited by Norm; June 2nd, 2011 at 01:32 PM.

Similar Threads

  1. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM
  2. PROGRESS BAR ON FILE READ
    By jai in forum Threads
    Replies: 2
    Last Post: February 17th, 2011, 01:12 PM
  3. how to read file name inside .gz file....
    By kathir0301 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 6th, 2010, 10:06 AM
  4. Read excel file
    By chamil in forum JDBC & Databases
    Replies: 1
    Last Post: October 25th, 2010, 03:00 AM
  5. how to read a PDF file?
    By epezhman in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 15th, 2010, 06:26 AM