How to read text file and write to Excel - java

I have a text file with several lines of data like this line:
04498203648;5067670000002534;15:44:50.987;15:44:51.533;546
04498203649;5067670000002344;15:44:50.988;15:44:51.534;546
I wish I could open the file, read each line and every semicolon separated value and insert them into a row in Excel, keeping one value per cell. Also I need to put some format into these cells, like keeping the format "[h]:mm:ss.000" for the timestamp values.
I can read the text file line by line, obtain each semicolon separated value, but I am a bit lost in putting them into the Excel file. Any easy way or tricks for handling this?
I am thinking of something like assigning each element of a line to 5 different variables and then writing these variables in line to Excel.
Java newbie here. Thanks in advance.

Related

How to find the last use of a string in a csv table and finding the value of another column on that row

Using Java Processing I'm trying to find the last time that a specific string value is found in a table column and get another value from the cell next to it.
How would I do something like this?
Use any of the several available CSV parsing tools to read your file. For example, Apache Commons CSV.
As you read in each line, test if it contains your target value. If so, remember that row by stashing in a variable. Each time you find a hit, replace the previously stashed value with the latest, storing in that same variable.
When done reading the file, the last row meeting your criteria is in that variable. Now read the adjacent cell from that row. VoilĂ , your result.
You will find many examples of reading CSV files in existing Questions and Answers. Again, I recommend you use a library for reading and parsing the CSV rather than perform that chore yourself.

overwrite existing table rows using DOCX4j

I am facing a problem in writing in MS Word file using Docx4j. The scenario is something like:
First user writes into the word file and supposedly he takes about 10 rows of the table and saves the file.
Second user comes and writes about 5 rows and saves the file again. But after sometime he again opens the file and modify something through java application.
Modified text is being added into the file instead of replacing the old text.
Now I want to replace his old text and want to save new paragraphs in table.
In simple words I want to replace old table rows with new one in Docx4j.
How can I reach to my desired rows to replace them?
Can anybody help me in this? Thanks
Use Replace to what you try do is just replace the String
Check this
https://www.docx4java.org/forums/docx-java-f6/replace-text-into-docx-within-a-table-t6.html

Dealing with specific lines when parsing XML docs in Java

I have a huge xml file from wiktionary that I need to parse for a class project. I only need to extract data from a set of 200 lines, which start at line 395,000. How would I go about only scanning that small number of lines? Is there some sort of built in property for line number?
If line boundaries are significant in your data then it's not true XML. Accept it for what it is, a line-oriented file, and start by processing it using line-oriented text tools. Use these to extract the XML (if you can), and then pass this XML to an XML parser.
There is no built in property for line numbers.
If you want to look at all of the data from line 395,000 to 395,200 programatically, you can do so by counting new line characters.
Each line in the file ends with a new line ("\n"), so you could count 349,999 of them, and then look at the data until you see 200 more.

java how to populate multiple JTextFields reading data from a .txt file

I have to read some data from a .txt file. Each row contains a string.
Each string has to be displayed in a specific JTextField.
How can i solve the problem?
Thanks
Read the file line by line as described in the Java Tutorial | Reading, Writing, and Creating Files.
Call setText to set the text in a specific JTextField. e.g.
jTextField.setText(line);

Excel or text file, which one to use?

I need to suggest an input, excel file or text file.
assuming the input is large number of lines where I need to read the first String, for example:
A,B,C,D....
I need to read the first String (in this case A) to identify the matching row, should I use excel file and use POI to read the first cell of each row? or text file where each line tokens are separated by delimiter and to parse each line reading the first token.
Use a text file. Because computers like it more. If business requires it, rename that text file into a "csv" file and you've got an Excel file.
If humans are going to enter data then use Excel. If the file is used as a communication channel between two systems use as simple as possible file.
If at all possible, use text file - much easier to handle/troubleshoot, easier to generate, uses less memory, does not have restrictions on number of rows, etc. In general - more predictable.
If you go with text files and you have people manually preparing those text files, and you are dealing with non-ASCII text, you better make sure everybody will send you the files in correct encoding (usually UTF-8 would be the best). This is not an issue with Excel.
The only reason to use Excel workbook would be when you need some "business-people" to produce those input files, then that input effectively becomes a user interface to your system - Excel is usually considered more user friendly than Notepad. ;-)
If you do go with Excel, make sure that the people producing those Excel files will give you the correct version (I assume you would want the "old" XLS format, not the new XLSX format).
Rule of thumb: use a text file. It's more interchangeable and way easier to handle by any other software you may need to support in a few years.
If you need some humans to edit those data and you need some beautiful/color display the Excel can provide, consider creating a macro that would store data in csv.

Categories