Results 1 to 13 of 13
- 07-13-2011, 11:58 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 23
- Rep Power
- 0
- 07-13-2011, 12:05 PM #2
can you explain more details. Usually csv file is a txt file.
Skype: petrarsentev
http://TrackStudio.com
- 07-13-2011, 12:10 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 23
- Rep Power
- 0
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]
- 07-13-2011, 12:25 PM #4
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 - JavaSkype: petrarsentev
http://TrackStudio.com
- 07-13-2011, 12:30 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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.
- 07-13-2011, 12:45 PM #6
Member
- Join Date
- Jun 2011
- Posts
- 23
- Rep Power
- 0
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);
}
}
}
- 07-13-2011, 12:46 PM #7
Member
- Join Date
- Jun 2011
- Posts
- 23
- Rep Power
- 0
where should i put the data so that my csv file would be converted to .dat file
- 07-13-2011, 12:48 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
And?
What is the problem?
Where are you stuck?
And what on earth does a DAT file have to do with this?
- 07-13-2011, 12:50 PM #9
Member
- Join Date
- Jun 2011
- Posts
- 23
- Rep Power
- 0
ok here it goes
These are my problems !
1. Reading a csv file
2.Saving it to a .dat file
thats my problem
- 07-13-2011, 12:59 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
OK.
Read the csv file.
Get that bit working.
- 07-13-2011, 12:59 PM #11
Member
- Join Date
- Jul 2011
- Posts
- 5
- Rep Power
- 0
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 fileLast edited by mahoraz; 07-13-2011 at 01:07 PM.
- 07-13-2011, 01:01 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Since I suspect this is a learning exercise I doubt using a CSV reader framework will be looked on as a Good Thing.
- 07-16-2011, 11:48 AM #13
Member
- Join Date
- Jun 2011
- Posts
- 23
- Rep Power
- 0
Similar Threads
-
converting byte array to bmp file
By Moorkh in forum New To JavaReplies: 2Last Post: 09-07-2010, 02:58 PM -
Read a file and converting this file into a string
By kostinio in forum New To JavaReplies: 7Last Post: 12-26-2009, 03:54 PM -
Converting excel file to text file
By saranya_v11 in forum New To JavaReplies: 2Last Post: 10-14-2009, 04:01 PM -
converting java class file to exe file
By satheeshtech in forum Advanced JavaReplies: 5Last Post: 07-18-2009, 11:47 PM -
Converting text file(.txt) to JPG file(.jpg) in java
By javadeveloper in forum Advanced JavaReplies: 0Last Post: 11-09-2007, 04:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks