Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

problem reading file  RSS feed

 
Sophia Choi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally, my program can read csv files.
I change an excel.xls to excel.csv intentionally.
The file reads unexpected codes and runs without stop.
How can I detect this problem?
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sophia Choi:
The file reads unexpected codes and runs without stop.

Well, if you expect our help, explaining what "unexpected codes" means would be a good start. If you are getting an exception, printing out the stack trace and using it to determine the offending line in your code would help. My wild guess is that you are expecting to read in a certain value from the CSV that will indicate end-of-file rather than checking for the end-of-file condition from whatever input stream you are using. CSV's are just text files. You can open it in your editor and seeif Excel exported what you think it did. Use System.out.println to print lines as your program reads them in. Sometimes you aren't getting the data that you think you are. Last but not least, you can step through the program using a debugger in your IDE or a seperate product like JSwat
 
Sophia Choi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steps:
1. upload file, expected xxx.csv. In jsp, I can only check the extension.
If not csv, prompt out an error message.
2. read upload file content and compare to my db
3. result comes out.
The problem is someone changes the xx.xls to xx.cvs.
My program does it work actually. But infinitively
because they are machine codes.
try
FileReader fr= new FileReader(path + filename);
BufferedReader br = new BufferedReader(fr);
String Line = br.readLine(); <-- problem
.....
catch exception..
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might try to find a way to verify that the file is valid CSV before you read any further. Maybe open the file as a byte stream and see if the first ten or twenty bytes contain printable letters and numbers. Or examine XLS files in debug and see if they start with a common "signature" pattern. Either way, if the file looks good, close it and open again with your buffered reader. That's definitely an extra step with some performance cost, but it sounds like it will be worth while to protect yourself from unreadable data.
 
Sophia Choi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Yes,I check the char before reading the whole line.
FileReader fr = new FileReader(path + fileName);
char ch = (char)fr.read();
if((ch >='0' && ch <= '9') || (ch >='a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z')){
BufferedReader br = new BufferedReader(fr);
 
Richard Rodger
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If someone just changed the file extension then it is definitely not going to work - a .xls file is *not* a .csv file. You can use the Apache POI library to read xls files.

Of course, the real solution is to explain to the user that changing the file extension is not going to work - get them to save to csv from Excel instead.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!