I am trying to read tweet contents from my DB and write into a .csv file. However, some of the tweets have newline characters, so that is why; the csv file seems weird when I open it.
Could you please help me about how to fix this?
Thanks
//This is reading part
ResultSet rsForSelectTweetContentsQuery = null;
String selectTweetContentsQuery = "SELECT tweetContent FROM filteredtweets WHERE originalTweetId = 4515415454654";
String tweetContent = rsForSelectTweetContentsQuery.getString("tweetContent");
//Thisis writing part
try (BufferedWriter bufferedWriter = new BufferedWriter(csvFileWriter))
{
bufferedWriter.write(tweetId + "," + tweetContent);
bufferedWriter.newLine();
bufferedWriter.close();
}
catch (IOException ex)
{
}
Everybody thinks that CSV files are trivial, but that is actually far from the case. The CSV format has a formal specification, and covers cases like:
Comma itself is a value
A quoted string itself contains quotes
A value has new-lines
Unicode values
etc.
Basically, you don't want to write this yourself, you'll get it wrong. Use this library.
Related
I am managed to get the data out of an API and put into a CSV, but I have problems to put the data into the CSV in a loop because right now it always overwrites it in the CSV. And the next problem is that the date does not get shown in the CSV in different fields. In the CSV it looks
like this:
and I want all the data like in my console:
my code right now:
JSONArray jsonarr_1 = (JSONArray) jobj.get("infectedByRegion");
//Get data for Results array
for(int i=0;i<jsonarr_1.size();i++)
{
//Store the JSON objects in an array
//Get the index of the JSON object and print the values as per the index
JSONObject jsonobj_1 = (JSONObject)jsonarr_1.get(i);
//Store the JSON object in JSON array as objects (For level 2 array element i.e Address Components)
String str_data1 = (String) jsonobj_1.get("region");
Long str_data2 = (Long) jsonobj_1.get("infectedCount");
Long str_data3 = (Long) jsonobj_1.get("deceasedCount");
System.out.println(str_data1);
System.out.println("Infizierte: "+str_data2);
System.out.println("Tote: "+str_data3);
System.out.println("\n");
PrintWriter pw = null;
try {
pw = new PrintWriter(new File("C:/Users/stelz/OneDrive/Desktop/Corona Daten/28.04.2020.csv"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
StringBuilder builder = new StringBuilder();
String columnNamesList = "Bundesland,Infizierte,Tote";
// No need give the headers Like: id, Name on builder.append
builder.append(columnNamesList +"\n");
builder.append(str_data1+",");
builder.append(str_data2+",");
builder.append(str_data3);
builder.append('\n');
pw.write(builder.toString());
pw.close();
System.out.println("done!");
}
//Disconnect the HttpURLConnection stream
conn.disconnect();
Your CSV is probably a nice comma delimited file with UTF-8 encoding. The first line in the image from Excel is an evidence that in the file and as text the first line is correctly:
Thüringen,2170,80
But your Excel has probably national defaults for Western Europe, meaning semicolon (';') as CSV separator, and CP1252 (slight variation of the Latin1 charset) for encoding.
How to fix?
Excel is known to have a very poor support for CSV file: read it as text (notepad or notepad++) or use LibreOffice calc which allows to declare the separator and the encoding when reading a CSV file.
If you have to stick to Excel, carefully build your file for your own Excel, use ISO-8859-1 charset and semicolons:
...
PrintWriter pw = null;
try {
pw = new PrintWriter(new File("C:/Users/stelz/OneDrive/Desktop/Corona Daten/28.04.2020.csv"),
"ISO-8859-1");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
StringBuilder builder = new StringBuilder();
String columnNamesList = "Bundesland;Infizierte;Tote";
// No need give the headers Like: id, Name on builder.append
builder.append(columnNamesList +"\n");
builder.append(str_data1+";");
...
It looks like you're trying to create many different CSVs? If this is not what you want and you want all the records in one CSV, then I recommend looping through the array, building a CSV with StringBuilder or some other CSV tool of which there are many, and then creating the file outside of your for loop.
I am attempting to pull some values from a webpage, with the intention of writing them into a .txt file for manual validation. I have looked around the web and cannot find the way to achieve this in my scenario. I will be writing the code in java if possible.
I have the following html code available for the element:
<td class="value" data-bind="text:
$data.value">Windows Server 2012 R2 Standard 64-bit</td>
And the xpath for the element is:
html/body/div[4]/div/div[4]/div[2]/div[4]/div/div[1]/div[1]/table[1]/tbody/tr[2]/td[2]
Is anyone able to help me create a sample piece of code that will;
a) Pick up the value and write it into a text file. Preferably with a prefix of 'Operating system'.
b) Save the file with a unique ID, My thought is to suffix the filename with a datetime stamp.
c) I will have multiple elements to read from the webpage and then write to the text file, around 8 or so, is there any consideration I need to be aware of for writing multiple values to a .txt file and format them neatly?
Hopefully I have included everything I need to here, if not just ask!
Many thanks in advance. KG
Thats not as difficult as it seems. I use similar function for me to write a log into a txt file.
At first I would write all Information in one String variable. It's helpful to format the Information before writing it into the variable. If you collect all Informations your could write this String very simple to a txt file using the following Code:
private static void printToTxt(){
String info = "Collected Informations";
String idForTxtFile = new SimpleDateFormat("dd.MM.yyyy_HH.mm.ss").format(new Date());
File file = new File("Filename" + idForTxtFile);
try {
FileWriter fw = new FileWriter(file, true);
//if you want to write the linesperator ("\n) as they are in the txt you should use the following Code:
String lineSeparator = System.getProperty("line.separator");
String[] ouput = info.split("\n");
for (int i = 0; i <= output.length-1; i++) {
fw.write(output[i]);
fw.write(lineSeparator);
}
//instead you could only use:
fw.write(info);
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println(e.getLocalizedMessage);
}
Little bit late, but here is my version, maybe you could use some additional to yours!
I have reviewed the answers on the question How do I create a file and write to it in Java?, thanks for the nudge #Mardoz, and with some playing around I have it doing what I needed. Here is the final code, with the date also being tagged into the filename:
Date date = new Date() ;
SimpleDateFormat dateFormat = new SimpleDateFormat("DD-MM-yyyy HH-mm") ;
// Wait for the element to be available
new WebDriverWait(Login.driver,10).until(ExpectedConditions.visibilityOfElementLocated
(By.xpath("html/body/div[4]/div/div[4]/div[2]/div[4]/div/div[1]/div[1]/table[1]/tbody/tr[2]/td[2]")));
Writer writer = null;
// Find the value and write it to the text file 'Smoke_004 DD-MM-yyyy HH-mm.txt'
try {
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("Smoke_004 " + dateFormat.format(date) + ".txt"), "utf-8"));
writer.write("Operating System : " + Login.driver.findElement
(By.xpath("html/body/div[4]/div/div[4]/div[2]/div[4]/div/div[1]/div[1]/table[1]/tbody/tr[2]/td[2]"))
.getText());
} catch (IOException ex) {
// report
} finally {
try {
writer.close();
} catch (Exception ex) {
}
}
Thanks for your input #Mardoz and #ErstwhileIII
I am trying to write a Java program that simulates a record store shopping cart. The first step is to open up the inventory.txt file and read the contents which is basically what the "store has to offer". Then I need to read every line individually and process the id record and price.
The current method outputs a result that is very close to what I need, however, it picks up on the item id of the next line, as you can see below.
I was wondering if someone can assist me in figuring out how to process every line in the text document individually and store every piece of data in its own variable without picking up the id of the next item?
public void openFile(){
try{
x = new Scanner(new File("inventory.txt"));
x.useDelimiter(",");
}
catch(Exception e){
System.out.println("Could not find file");
}
}
public void readFile(){
while(x.hasNext()){
String id = x.next();
String record = x.next();
String price = x.next();
System.out.println(id + " " + record + " " + price);
break;
}
}
.txt document:
11111, "Hush Hush... - Pussycat Dolls", 12.95
22222, "Animal - Ke$ha", 9.95
33333, "Hanging By A Moment - Lifehouse - Single, 4.95
44444, "Have A Nice Day - Bon Jovi", 9.99
55555, "Day & Age - Killers", 10.99
66666, "She Wolf - Shakira", 15.99
77777, "Dark Horse - Nickelback", 12.99
88888, "The E.N.D. - Black Eyed Peas", 10.95
actual output
11111 "Hush Hush... - Pussycat Dolls" 12.95
22222
expected result
11111 "Hush Hush... - Pussycat Dolls" 12.95
So the problem here specifically is that you are breaking on commas, and you should be breaking on commas and newlines. But there are tons of other corner cases (for example, if your column is "abc,,,abc" you shouldn't break on those commas). Apache Commons comes with a CSVParser that handles all of these corner cases, you should use it:
http://commons.apache.org/csv/apidocs/org/apache/commons/csv/CSVParser.html
You can use a Pattern as the argument to Scanner.useDelimiter. Use this to provide alernates for the delimiter: either comma, or the line separator.
x.useDelimiter(",|" + System.getProperty("line.separator"));
Depending on what your input file uses as the line separator, you may need to change the second option.
The advice in other answers to use an existing CSV library is good: parsing CSV isn't as simple as breaking up the input around commas.
There are multiple ways to achieve this but going with your own way, you could use Scanner to first read lines (use Java's "line.separator" as delimiter) and then use Scanner class again with comma as delimiter.
The problem you're going to be facing is the CSV is more then just splitting a String on a comma. There are considerations to take into account with "escaped" commas (commas you don't want to delimante against).
I suggest you save your self a lot of time and head aches and use an existing API.
The Apache Commons has already been mentioned. I recently used OpenCSV and found it to be extremely simple to use and powerful
IMHO
An easy way to read in the entire file into a list of Strings (lines)...
public class Scanner {
public static List<String> readLines(String filename) throws IOException {
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
List<String> lines = new ArrayList<String>();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
lines.add(line);
}
bufferedReader.close();
return lines;
}
}
Then you can process the individual lines as before, as each line is it's own String object. That is, if you don't use a CSVParser.
public static void main(String args[])
{
try
{
File file = new File("input.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "000000", oldtext = "414141";
while((line = reader.readLine()) != null)
{
oldtext += line + "\r\n";
}
reader.close();
// replace a word in a file
//String newtext = oldtext.replaceAll("drink", "Love");
//To replace a line in a file
String newtext = oldtext.replaceAll("This is test string 20000", "blah blah blah");
FileWriter writer = new FileWriter("input.txt");
writer.write(newtext);writer.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
A couple suggestions on your sample code:
Have the user pass in old and new on the command line (i.e., args[0] and args1).
If it's sufficient to do this a line at a time, it's going to be much more efficient to read a line, replace old -> new, then stream it out.
Also check out StringUtils and IOUtils, which may make your life easier in this case.
Easiest is the String.replace(oldstring, newstring), or String.replaceAll(regex, newString) function, you can just read the one file and write the replacement into a new file (or do it line by line if you're concerned about file size).
After reading your last comment - that's a totally different story... the preferred solution would be to parse the css file into an object model (like DOM), apply the changes there and serialize the model to css afterwards. It's much easier to find all color attributes in DOM and change them compared to doing the same with search and replace.
I've found some CSS parser in the wild wild web, but none of them looked like being capable of writing CSS files.
If you wanted to replace the color names with search and replace, you'd search for 'color:<colorname>' and replace it with 'color:<youHexColorValue>'. You may have to do the same for 'color:"<colorname>"', because the color name can be set in double quotes (another argument for using a CSS parser..)
String.replaceAll() is the easiest way to do it. Just read the complete CSS file into one String, replace all as suggested above and write the new String to the same (or a temporary) file (first).
I want to read a file in java. And then, I want to delete a line from that file without the file being re-written.
How can I do this?
Someone suggested me to read/write to a file without the file being re-written with the help of RandomAccessFile. How to write data to a file through java?
Specifically, that files contains lines. One line contains three field - id, name and profession - separated by \t. I want to read that file through a Reader or InputStream or any other way and then search for a line that has the specified keyword (say 121) and then wants to delete that whole line.
This operation needs to be performed without the whole file being re-written
I don't think you can alter a file on a filesystem in any way without writing to it, including deleting a line.
Do you mean you want to write the file without altering the file's metadata, like the last modified time?
Based on your updated question:
I don't think you can do what you're asking to do here. You can't remove bytes from a file once the file has been written, note no deleteByte or removeByte methods in RandomAccessFile.
I suggest moving the content of your file to a database - that allows this kind of record-oriented operation.
The alternative is, you have to rewrite the file. Sorry!
"Lines" are an abstract concept -- they're just an arbitrary sequence of bytes terminated by "\n". BufferedWriters and their ilk don't support textual editing in this way, so you'll have to rewrite the file in its entirety.
In general, what you want to do is:
open a reader
read content into some suitable data structure
close the reader
change data/records which need to be changed in this data structure
open a FileWriter with append == false
write content of data structure to resulting file
close FileWriter
add a marker in your lines saying if your line is deleted or not : this will make a software delete instead od a hardware delete.
if you have to insert new lines, you then can reuse those that are marked as deleted.
The below code searchs the line or fields in a single text file reads the file line by line
then the line or fields can be replaced by " " or any other string. Here we use the pattern and Matcher classes.
If this not clearing your question do let me know.
import java.io.;
import java.util.regex.;
import java.util.Properties;
public class DeleteLine
{
public static void main(String[] args)
{
BufferedReader br = null;
try
{
String line=null;
File f = new File("d:/xyz.txt");
String replaceString=properties.getProperty("replaceAll.String");
;
br = new BufferedReader(new FileReader("d:/giri/scjp/");
while ( (line = br.readLine()) != null )//BufferedReader contains readline method
{
Pattern p=Pattern.compile(searchString);/*here u an specify the line u want to delete */
Matcher m=p.matcher(line);
line=m.replaceAll(replaceString);/*here replace String u can " " so that it will be emptied */
System.out.println(line);
}
//System.out.println(line);
}
}
}
br = new BufferedReader(new FileReader("d:/xyz.txt"));
String line = null;
}
catch (FileNotFoundException e)
{
System.out.println("File couldnt find");
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}