Parsing a CSV File In JSP - java

I am creating a webApp that will download some data from Yahoo Finance into a CSV file and then (hopefully) be able to then read the created CSV data into a HTML table.
I have successfully got the program to connect to the Yahoo feed and then download that data into a CSV file and would not like to use the data from the file into a table.
Below is the code i used to create the CSV file:
String ticker = request.getParameter("stockSym");
URL url = new URL("http://finance.yahoo.com/d/quotes.csv?s=" + ticker + "&f=abc");
InputStream in = url.openStream();
BufferedInputStream bufIn = new BufferedInputStream(in);
File f=new File("stockInfo.csv");
FileOutputStream fop=new FileOutputStream(f);
for (;;)
{
int data = bufIn.read();
// Check for EOF
if (data == -1)
break;
else
fop.write((char) data);
}
fop.flush();
fop.close();
Are there any JSP programmers who would know how to open and then parse a CSV file into a table or would know of any good links to tutorials on how to accomplish this task?
Thanks.

Do not write the CSV parsing code yourself, use one of the many available libraries.
They can handle tricky things like line breaks, quotes, embedded commas and so on.

There is open source routine to parse CSV files here:
http://scm.opendap.org:8090/trac/browser/trunk/ODC/src/opendap/clients/odc/Utility.java
This utility class has a bunch a different functionality in it. Just pull out what is needed to parse the CSV.

Related

Is there any way to get records from a database as a comma seperated text file

Friends,
I have stuck in this job
I have a table empdb having 1000 rows like below
id name desgn
--- ------- ----------------
1 Mike Analyst
2 Jim Manager
3 John Engg
etc . etc so on
I want to write a servlet that will query this table
and download in text format say "emp_info.txt" like below
1,sam,Engg
2,Mike,Excecutive
3,Jim,Manager
I mean every record in db should be on seperate line
Please guide
We can't get directly records from db as csv file, there are many third-party libraries available for working with CSV files,
Let's take a look at a few of them:
Open CSV: Another popular and actively-maintained CSV library
Apache Commons CSV: Apache's CSV offering for working with CSV Files
Flatpack: An open-source CSV library being actively developed
CSVeed: Open-source and actively-maintained.
Sample code for open csv, add your open csv jar to you class path.
create one csv file and add its location to the code below
File file = new File(filePath);
try {
// create FileWriter object with file as parameter
FileWriter outputfile = new FileWriter(file);
// create CSVWriter object filewriter object as parameter
CSVWriter writer = new CSVWriter(outputfile);
// adding header to csv
String[] header = { "id", "name", "design" };
writer.writeNext(header);
// add data to csv
// in loop
String[] data = {}
writer.writeNext(data);
// execute this two line until your data remains.
// closing writer connection
writer.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
After creating the file set content type as csv and flush the buffer the browser will download the file for end user.
resp.setContentType("application/csv");
resp.setHeader(HttpHeaders.CONTENT_DISPOSITION, file);
inStrm = new FileInputStream(file);
FileCopyUtils.copy(inStrm, resp.getOutputStream());
resp.flushBuffer();
I assume that you want the user who comes to your web app to be able to download all data in this "empdb" DB table as an "emp_info.txt" file. Just to be clear what is servlet:
Servlet - resides at server side and generates a dynamic web page
You should split this task to different parts:
Connect your database with java application by using JPA and e.x Hibernate, also implement a repository.
Create service that will user repository to fetch all data from the table and write it to .txt file.
Implement button or other functionality in GUI which will use the service you have created and will send the file for the user.

Output Streaming multiple files into a zip file

Hie , I m generating report in csv format using solr , angularjs , Jax-rs and java. The input stream contain a csv response already because we have specified wt=csv while querying solr. Size of csv created from every input Stream might be 300mb .At java layer code is some thing like :
enter code here
InputStream is1;
InputStream is2;
// for is1 let file is csv1
// for is2 let file is csv2
// csvzip is the csv created form both two files
// now csvzip need to be downloaded through a popup
Creating big size file and zipfile in memory will not be a good approach surely.
Is there is any way to handle this?

Reading and displaying large formatted text documents in Android

I'm developing an Android app which is supposed to present large text files (books for example), for the user to browse, read, and search.
My question is as follows:
How should I read and present the text file, which is currently in either a PDF or Word format, and is formatted?
What file should the text be in (.doc, .txt, .xml, .html)?
What controls/elements and code should I use to read it on the app so that it should be presented efficiently and formatted correctly (TextView, WebView, PDF reader, or some other way)?
Thanks.
It really depends on your application and programming skills,
Grabbing texts from PDFs, word files, etc.. can be done using
libraries (lots are available)
you can save your files as raw text files (they could be readable
manually) if you want to want to secure it you can encrypt it before
saving and you can save with custom extension( .lib,.abc etc..).
TextViews are the easiest as you can change the colors ,
fonts and text size, it's really fast and easy to deal with.
Edit : example of reading a text file
File myFile = new File("/sdcard/filename.txt");
FileInputStream iStr = new FileInputStream(myFile);
BufferedReader fileReader = new BufferedReader(new InputStreamReader(iStr ));
String TextLine= "";
String TextBuffer = "";
while ((TextLine= fileReader.readLine()) != null) {
TextBuffer += TextLine+ "\n";
}
textView1.setText(TextBuffer );
fileReader.close();
example of writing a text file :
File myFile = new File("/sdcard/filename.txt");
myFile.createNewFile();
FileOutputStream oStr = new FileOutputStream(myFile);
OutputStreamWriter fileWriter= new OutputStreamWriter(oStr);
fileWriter.append(textView1.getText());
fileWriter.close();
fOut.close();

Error in read .doc and .docx file's content

I want to read a .txt, .doc and .docx files and print the contents of those files.when i run the below code some .doc and .txt files are read but many files are not able to read.
import java.io.File;
import javax.swing.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
public class FindYourDocx
{
public static void main(String[] args)
{
String text = "";
int read, N = 1024 * 1024;
char[] buffer = new char[N];
try {
JFileChooser openFile=new JFileChooser();
openFile.setCurrentDirectory(new File("."));
openFile.showOpenDialog(null);
File f1=openFile.getSelectedFile();
String file1=f1.toString();
File f =new File(file1);
JOptionPane.showMessageDialog(null,f);
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
while(true) {
read = br.read(buffer, 0, N);
text += new String(buffer, 0, read);
System.out.println("Follows"+text+" ");
if(read < N) {
break;
}
System.out.println("Follows"+text+" "); }
} catch(Exception ex) {
ex.printStackTrace();
}
}}
by executing the above code (for some files) i got some wired messages as follows
http://i.stack.imgur.com/RwNWM.jpg
Someone please help me to solve this issues....
to read .docx i came across something like XWPFDocument using apacheio ....what is this ?
First of all you should think about your problem: What do different file types look like as a file, what is their structure, what's the content which you would like to print and what does "printing" mean at all? What your are doing is reading files, treating them as text and printing them to STDOUT. Does "printing" mean this in your case? I interpret "printing" as being able to send content to a printer and get some paper.
Another hint: Doc and Docx are binary files, which contain "printable" text "somewhere". You can't just read the files and do something with the data. You need to know how those file formats look like, were the content is etc. Java can't do that out of the box, you need additional libraries to parse those file formats and do something with them.
There are many tutorials and questions around formats like docx:
How to read docx file content in java api using poi jar
to read .docx i came across something like XWPFDocument using apacheio ....what is this ?
You mean Apache POI. To find out more, check the website. In brief, both Apache POI and docx4j (which I note you have tagged) are Java libraries aimed at reading, manipulating, and writing Microsoft Office files.
'doc' files are Microsoft proprietary binary files. If you try to read them in and display them using the Java IO API alone, all you will see is a representation of the binary data. It won't be useful to you. You need to use an API specifically for loading up and traversing Word files, which is where Apache POI or docx4j come in.
'docx' files are a newer XML-based Microsoft Office format. A docx file is essentially a zipped folder containing the various assets that make up a Word file.
As I said, in order to read a Word file properly, you will need to use one of the libraries mentioned. Both the Apache and docx4j websites contain plenty of example code to get you started opening and traversing Word documents (note that POI can work with the older .doc format, whereas docx4j is only for .docx files).
http://www.docx4java.org
http://poi.apache.org

Commons Net FTPClient retrieved file encoding issue

I'm retrieving a file from a FTP Server. The file is encoded as UTF-8
ftpClient.connect(props.getFtpHost(), props.getFtpPort());
ftpClient.login(props.getUsername(), props.getPassword());
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
inputStream = ftpClient.retrieveFileStream(fileNameBuilder
.toString());
And then somewhere else I'm reading the input stream
bufferedReader = new BufferedReader(new InputStreamReader(
inputStream, "UTF-8"));
But the file is not getting read as UTF-8 Encoded!
I tried ftpClient.setAutodetectUTF8(true); but still doesn't work.
Any ideas?
EDIT:
For example a row in the original file is
...00248090041KENAN SARÐIN 00000000015.993FAC...
After downloading it through FTPClient, I parse it and load in a java object, one of the fields of the java object is name, which for this row is read as "KENAN SAR�IN"
I tried dumping to disk directly:
File file = new File("D:/testencoding/downloaded-file.txt");
FileOutputStream fop = new FileOutputStream(file);
ftpClient.retrieveFile(fileName, fop);
if (!file.exists()) {
file.createNewFile();
}
I compared the MD5 Checksums of the two files(FTP Server one and the and the one dumped to disk), and they're the same.
I would separate out the problems first: dump the file to disk, and compare it with the original. If it's the same as the original, the problem has nothing to do with UTF-8. The FTP code looks okay though, and if you're saying you want the raw binary data, I'd expect it not to mess with anything.
If the file is the same after transfer as before, then the problem has nothing to do with FTP. You say "the file is not getting read as UTF-8 Encoded" but it's not clear what you mean. How certain are you that it's UTF-8 text to start with? If you could edit your question with the binary data, how it's being read as text, and how you'd expect it to be read as text, that would really help.
Try to download the file content as bytes and not as characters using InputStream and OutputStream instead of InputStreamReader. This way you are sure that the file is not changed during transfer.

Categories