• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem reading file

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
Author
Posts: 31
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic