I have CSV page and I want read all columns contains CSV file and display in JSP page
I am using following code:
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
String[] country = line.split(cvsSplitBy);
wordList = Arrays.asList(country);
//System.out.println("data is"+wordList.size());
}
How to display ArrayList in JSP page using struts with display tags.
I am not able to write setter methods of properties because I do not know a CSV file contained how many columns in it as it changes file to file
From what I have understood from your post is that you have the array list of strings and you cannot display it in JSP. Is this what you want?
A quick solution can be to create a new String from that list, and display just a String.
Related
I am new to Java. I was successfully able to read my CSV file from my local file location and was able to identify which column needed to be deleted for my requirements. However, I was not able to delete the required column and write the file into my local folder. Is there a way to resolve this issue? I have used the following code:
CSVReader reader = new CSVReader(new FileReader(fileName));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
System.out.println(nextLine[15]);
}
All I would like to do is to remove the column having index 15 and write the file as a CSV file in my local folder.
I'm assuming you're using the OpenCSV library.
In order to make your code work, you have to fix 2 issues:
You need a writer to write your modified CSV to. OpenCSV provides a CSVWriter class for this purpose.
You need to convert your line (which is currently a String array) into a list to be able to remove an element, then convert it back into an array to match what the CSVWriter.writeNext method expects.
Here's some code that does this:
CSVReader reader = new CSVReader(new FileReader(fileName));
CSVWriter writer = new CSVWriter(new FileWriter(outFileName));
String[] origLine;
while ((origLine = reader.readNext()) != null) {
List<String> lineList = new ArrayList<>(Arrays.asList(origLine));
lineList.remove(15);
String[] newLine = lineList.toArray(new String[lineList.size()]);
writer.writeNext(newLine, true);
}
writer.close();
reader.close();
Some additional remarks:
The code probably needs a bit more error handling etc if it's to be used in a production capacity.
List indices in Java start at 0, so remove[15] actually removes the 16th column from the file.
The code writes its output to a separate file. Trying to use the same file name for input and output will not work.
I am reading a CSV file and using OpenCSV to read it and CircularFifoBuffer to split the data into columns and assign the value from each column to a string. This works fine for reading a specific row in the csv file, however I wish to read the csv file line by line starting at the beginning and working downwards to the final row.
Then each time a row is read the string values will be compared and provided a given condition is satisfied the next row will be read.
I can handle all of the above bar processing the CSV data line by line.
Any pointers would be greatly appreciated.
Directly from the FAQ:
If you want to use an Iterator style pattern, you might do something like this:
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
System.out.println(nextLine[0] + nextLine[1] + "etc...");
}
Or, if you might just want to slurp the whole lot into a List, just call readAll()...
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
List myEntries = reader.readAll();
which will give you a List of String[] that you can iterate over. If all else fails, check out the Javadoc.
I am using jxl api to process contents from an excel file and load them into a json string. I then parse the json string and display contents in various TextViews in my screen. If the excel has any underlined/bold/italics text, then it is not being displayed accordingly in the TextView. Can someone suggest how to make sure any text with underline/bold/italics done in excel gets displayed in textview as well.
Below is the code I am using to process string from excel file
w = Workbook.getWorkbook(inputWorkbook);
Sheet sheet = w.getSheet(0);
Cell storyNameCell = sheet.getCell(1,1);
String Title = storyNameCell.getContents();
//get more cells into Strings
//form the json string from all the String contents above
And this is how I am saving the JSON string above into a local file on android device
String FILENAME = getString(R.string.app_name)+"_"+storyTitle;
FileOutputStream output = openFileOutput(FILENAME,MODE_PRIVATE);
OutputStreamWriter writer = new OutputStreamWriter(output, "UTF-8");
writer.write(jObjStoryTitle.toString());
writer.flush();
writer.close();
And finally I am getting any JSON string out of the file for display purpose on user request.
BufferedReader in = new BufferedReader(new InputStreamReader(this.getBaseContext().openFileInput( quizFileName), "UTF-8"));
while ((str = in.readLine()) != null)
fileContent.append(str);
fileString = new String(fileContent);
jObjStoryTitle = new JSONObject(fileString);
I was finally able to get around it. Here is what I did.
-used apache POI api for reading xls file in my android app. Jxl api was not helpful as that does not have capability to recognize any font/style embedded within the part of the text in a cell.
-So using the Apache-POI I then could figure out which part of the text contains underlined characters.
-I then embedded html tags before/after that part
- Then used the Html object to display in Textview as below
myTextView.setText(Html.fromHtml(underlinedText));
I have a text file in the following format:
Details.txt
The file is a .txt file. I want to read course title from this file and print corresponding textbook and instructor information. But i am not sure about what process to follow ? storing the information in an array won't be efficient! How should i proceed ? NOTE: I can't change the info in the file, it should not be changed!! obviously the file will be read by the following code:
File newFile=new File("C:/details");
but how should i extract the data from this file according to the labels course title, textbook and instructor!?
First read the file correctly line by line, and search for your entered course title, lets consider "Java"
Now you hit your title and you know you need 3 consecutive lines from your file as all information related to that title are there.
if(str.startsWith(title)); { // for title = "Java"
line1 = 1st line // contains ISBN and First Name
line2 = 2nd line // Title and Last Name
line3 = 3rd line // Author and Department
line4 = 4th line // Email
break; // this will take you out of while loop
}
Now on those four lines do string operations and extract your data as you need and use it.
I am home so I can't give you exact code. But if you follow this it will solve your issue. Let me know if any problem you got while doing this.
Follow this to get some info on String operations
Use String Tokenizer and separate each string and then store them in a Linked List or Array List. Have Separate List for each title like course title, instructor etc. and then print them
//Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"file.txt");
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
}
catch (IOException e) {
//You'll need to add proper error handling here
}
//Find the view by its id
TextView tv = (TextView)findViewById(R.id.text_view);
//Set the text
tv.setText(text);
you can use FileUtils.readFileToString(new File(""C:/details.txt");
Now you can extract the required data based on your wish
Use Scanner class
Scanner s=new Scanner(new File("C:/Details.txt"));
while(s.hasNext())
{
System.out.println(s.nextLine());
}
if you want in work by word then use String Tokenizer
see this article
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Fast CSV parsing
How to properly parse CSV file to 2d Array?
I'm very new to java file handling. Please, can any one tell me what is "CSV file format," and how to parse this type of file?
I want to take an input of employee data from a CSV file and save it in a hash map.
CSV stands for Comma Separated Values.
Here data is stored as so:
ID,Name,Age
20,"abcd xyz",33
30,asdf,28
OpenCSV is one good library for parsing CSV files.
There are other cousins of the CSV such as TSV (Tab Separated Values) and PSV (Pipe Separated Values). The below link should give you a head start:
http://en.wikipedia.org/wiki/Delimiter-separated_values
I also like the CSVReader library. The site includes code samples.
CSV - Comma Separated Value. The datas are separated using comma delimiter.
Eg. 1,"johnson","software Engineer"
Code:
Public static void main (String args[])
{
try{
String fileName= "D:\\sample.csv";
File file = new File(fileName);
BufferedReader bufRdr = new BufferedReader(new FileReader(file));
String line = null;
ArrayList arraylist=new ArrayList();
// Read each line of text in the file
while((line = bufRdr.readLine()) != null)
{
String[] data=line.split(",");
HashMap hm=new HashMap();
hm("employeeID",data[0]);
hm("employeeName",data[1]);
hm("employeeDesignation",data[2]);
arraylist.add(hm);
}
}catch(Exception e){}
}