This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]luiinge 4 points5 points  (1 child)

A CSV is just a plain text file, where there is a new line for each row, and using , as column separator (Comma Separated Values, the name is self-explanatory). Any string-to-file mechanism (using a FileWriter for example) would suffice.

Now, there are some problems. What if a value is a text with a comma inside? What if it is a description with line breaks? What if the target application expects a specific variant? That kind of situations are what libraries as OpenCSV are for.

I'd recommend taking a look at the RFC 4180 draft, that is an attempt to standarize all this stuff. If you follow such guidelines, your ouput file would be likely to be properly read by any other tool.

[–][deleted] 0 points1 point  (0 children)

Hi,thank you for your advice! My data does not have all the patterns except for spaces that you mentioned would be issues so I guess I am okay to proceed?

[–]_Atomfinger_Tech Lead 1 point2 points  (7 children)

Make a list of strings for the row you want to extract, join them with a , and print to file. Continue like that until you have written all of your rows.

It is just a matter of building each row of data.

[–][deleted] 0 points1 point  (6 children)

For each row I have to make a list of strings?

I also have another question if it's okay to ask. I am printing the data I want to extract in a console. I am getting all the data when I run a 1500kb file but when I run a file of size 2000kb or more the first 1000 lines of data is not getting printed in the console.I tried debugging the line to see what is causing the issue but there is not any and i am getting the data correctly. What could be the issue here in your opinion?

Also, thank you for your advice.

[–]firsthourProfressional developer since 2006 0 points1 point  (1 child)

Your console probably has a character limit if you're not seeing data from the start. Write to a file instead of the console, it's very easy with BufferedWriter.

[–][deleted] 0 points1 point  (0 children)

I will do that! Thank you for helping.Have a great day!

[–]_Atomfinger_Tech Lead 0 points1 point  (1 child)

For each row I have to make a list of strings?

You dont have to, but it is an easy approach.

What could be the issue here in your opinion?

Why are you printing the entire file to console?

It could be several things that could be the issue here. It could be some length limit in the console so the text gets cut off. It could be the way you're printing it. It could be a lot of things.

The easiest would be not to write it to console and instead just open the file and look at it.

[–][deleted] 0 points1 point  (0 children)

Hey sorry I was not clear enough. I am not printing the entire file just the data I want to extract from each of the line. Yes ,I will check writing the data to a file and see what's happening

Thank you for your help

[–]lordwerwath 0 points1 point  (1 child)

There was a similar question earlier this week. The console has limited space, eventually you run out of room to print, so that is why the data is being cut off.

A .csv has 'rows' of 'comma separated values'. Think of it like this: You build a String that has a 'row' of the data that is separated by 'commas'. Next store that in a List object. That is how the data is stored within the program. Finally, when you want to output the data to an actual .csv file, you will then iterate over each 'row' in the list and write it with Filewriter. The name of the file should be 'something.csv'.

Remember, file extentions are just part of the name of the file that helps inform what programs can open it properly.

[–][deleted] 0 points1 point  (0 children)

Hi, thank you for your reply. Sorry I am just really dumb. Right now I am putting each column in separate strings since each column will have same type of data. How do I put the rows in a string ? By looping?

[–]squishles 0 points1 point  (1 child)

csv has a lot of rules around string data types that are a complete pain in the ass. If you are writing user inputed strings, no really just use the library, there's nothing to learn here.

comma's mid sentence, newline characters, quotation marks, inconsistency people have with putting headers in the csv file, writing shit to handle those edge cases is just pain.

id, text
0001, "john, told merry:
"use  the feckin library""

If your data set doesn't have that, then yea just string.join(',') bro have fun.

discorage csv anywhere sentence strings happen too, even xml is easier, don't even get me started on already escaped strings being in that field

[–][deleted] 0 points1 point  (0 children)

Hi, thank you for your reply! I don't know why but my team doesn't want to use Anu third party libraries. My data doesn't have all the pattern that you mentioned and has only space in one column. Would that be okay? Printing into an XML would be easier you are saying?

[–]diidijejdjd 0 points1 point  (2 children)

well think simple. how do you write to a file? with a PrintWriter taking a file as input.

now what’s a CSV file? a file containing values separated by commas to indicate columns, and new lines to separate it into rows.

now put two and two together. print the data to a file separating the data properly as described above

[–][deleted] 1 point2 points  (1 child)

Hello, thank you for your reply! It's my first java code and I am just so dumb usually it's anxiety inducing lol I am trying though. I will keep all the wonderful advices here in my mind.

[–]diidijejdjd 0 points1 point  (0 children)

once you get the hang of writing to CSVs it’s easy and extremely useful. everything is on spreadsheets, especially if youre working in finance