Results 1 to 13 of 13
  1. #1
    aviolently is offline Member
    Join Date
    Jun 2011
    Posts
    23
    Rep Power
    0

    Default Converting .csv file to .txt file...

    Someone help me here cause i dont have any idea on this thing !

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    can you explain more details. Usually csv file is a txt file.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    aviolently is offline Member
    Join Date
    Jun 2011
    Posts
    23
    Rep Power
    0

    Default

    this is my code:
    and i do not know why it doesnt read .csv file to so im using .txt ... but i need to use csv file cause it is a requirement;;


    this is my code;


    [QOUTE]

    import java.io.*;
    import java.awt.*;
    public class StudentTableMain {

    public static Student[] readFromFile(String filename) throws IOException, ClassNotFoundException {
    //reading the data file
    ObjectInputStream fileReader = new ObjectInputStream(new FileInputStream(filename));
    Student[] m = (Student[]) fileReader.readObject();
    fileReader.close();
    return m;
    }

    //main
    public static void main(String[] args) {

    Student[] students = null;
    try {
    //reading the data file
    students = readFromFile("students.csv");
    }
    //catching if the file exist or not.
    catch (FileNotFoundException e) {
    System.out.println("File not found.");
    System.exit(0);
    } //catching if there's an error.
    catch (Exception e) {
    System.out.println("An error occured:");
    e.printStackTrace();
    System.exit(0);
    }
    // instantiating tabelModel to model. Overwriting // Overidding
    tableModel model = new tableModel(students);

    view view = new view(model, " +STUDENT TABLE+");
    controlCenter controller = new controlCenter (model , view);


    view.pack();
    view.setVisible(true);
    view.setLocationRelativeTo(null);
    view.setSize(460,560);

    }
    }
    [/QOUTE]

  4. #4
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    Oh, You confuse about CSV format and serializations objects.
    CSV file is just text file, which has special structure. like that

    name;soname;telephone;marks;
    petr;petr;1234;5;

    And have to just read file by line and create your object. Reading in a *.csv file and loading the data into an Array - Java
    Skype: petrarsentev
    http://TrackStudio.com

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    ObjectInputStream reads in objects (ie stuff from Java classes) that have previously been serialised (ie written) out to a file.
    Since this format is most definitely not a csv format, then this sort of stream is not at all useful to you.

    Use a BufferedReader wrapped around a FileReader. You can look in the API for how they function.

    Then read in each line and process it based on the comma separator, turning each into a Student object. Again, I'm sure Google will bring up numerous examples of how to do that.

  6. #6
    aviolently is offline Member
    Join Date
    Jun 2011
    Posts
    23
    Rep Power
    0

    Default

    ok here is my new code .....


    import java.io.*;
    import java.util.StringTokenizer;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import java.lang.*;


    public class ReaderSample {

    public static void main(String[] args) {

    try
    {
    //csv file containing data
    String strFile = "students.csv";

    //create BufferedReader to read csv file
    BufferedReader br = new BufferedReader( new FileReader(strFile));
    String strLine = "";
    StringTokenizer st = null;
    int tokenNumber = 0;

    //read comma separated file line by line
    while( (strLine = br.readLine()) != null)
    {

    //break comma separated line using ","
    st = new StringTokenizer(strLine, ",");

    while(st.hasMoreTokens())
    {
    //display csv values
    tokenNumber++;
    System.out.println(st.nextToken());
    }

    //reset token number
    tokenNumber = 0;

    }


    }
    catch(Exception e)
    {
    System.out.println("Exception while reading csv file: " + e);
    }
    }

    }

  7. #7
    aviolently is offline Member
    Join Date
    Jun 2011
    Posts
    23
    Rep Power
    0

    Default

    where should i put the data so that my csv file would be converted to .dat file

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    And?
    What is the problem?
    Where are you stuck?

    And what on earth does a DAT file have to do with this?

  9. #9
    aviolently is offline Member
    Join Date
    Jun 2011
    Posts
    23
    Rep Power
    0

    Default

    ok here it goes

    These are my problems !
    1. Reading a csv file
    2.Saving it to a .dat file

    thats my problem

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    OK.
    Read the csv file.
    Get that bit working.

  11. #11
    mahoraz is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    you can try this method described here
    CSV code examples - Java CSV

    oh...and *.dat file can contain anything in any format.... you forgot to specify the format in whitch you will save the data in the dat file
    Last edited by mahoraz; 07-13-2011 at 01:07 PM.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Since I suspect this is a learning exercise I doubt using a CSV reader framework will be looked on as a Good Thing.

  13. #13
    aviolently is offline Member
    Join Date
    Jun 2011
    Posts
    23
    Rep Power
    0

    Default

    i dont get it at all....

    Reading a .CSV and writing or saving it to a .DAT file?

    some one help please!

Similar Threads

  1. Converting excel file to text file
    By saranya_v11 in forum New To Java
    Replies: 3
    Last Post: 07-05-2013, 10:22 AM
  2. converting byte array to bmp file
    By Moorkh in forum New To Java
    Replies: 2
    Last Post: 09-07-2010, 02:58 PM
  3. Read a file and converting this file into a string
    By kostinio in forum New To Java
    Replies: 7
    Last Post: 12-26-2009, 03:54 PM
  4. converting java class file to exe file
    By satheeshtech in forum Advanced Java
    Replies: 5
    Last Post: 07-18-2009, 11:47 PM
  5. Converting text file(.txt) to JPG file(.jpg) in java
    By javadeveloper in forum Advanced Java
    Replies: 0
    Last Post: 11-09-2007, 04:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •