Generate vCard from Contact Data by java - java

I would like to be able to generate a vCard given the data contained within person_contact.
Here is my code.But I am unable to generate the vcard properly.
private void m1() throws IOException {
Contact contact = buildContact();
File f = new File("contact.vcf");
FileOutputStream fop = new FileOutputStream(f);
String outputImageFilePath = "contact.vcf";
createDirectoryIfNotExists(outputImageFilePath);
if (f.exists()) {
String str = "BEGIN:VCARD\n" + "VERSION:4.0\n" + "N:" + contact.getFull_name() + ";;;\n"
+ "FN:" + contact.getFull_name() + "\n" + "ORG:" + contact.getOrganization_name()
+ "\n" + "TITLE:" + contact.getTitle() + "\n" + "TEL;TYPE="
+ contact.getPhone_2_type() + ";VALUE=uri:" + contact.getPhone_2() + "\n"
+ "TEL;TYPE=" + contact.getPhone_3_type() + ",voice;VALUE=uri:tel:"
+ contact.getPhone_3() + "\n" + "EMAIL:" + contact.getEmail() + "\n" + "FAX:"
+ contact.getFax() + "\n" + "STREET1:" + contact.getStreet1() + "\n" + "STREET2:"
+ contact.getStreet2() + "\n" + "CITY:" + contact.getCity() + "\n" + "STATE:"
+ contact.getState() + "\n"
+ "END:VCARD";
fop.write(str.getBytes());
// Now read the content of the vCard after writing data into it
BufferedReader br = null;
String sCurrentLine;
br = new BufferedReader(new FileReader("contact.vcf"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
System.out.println(f.getAbsolutePath());
}
// close the output stream and buffer reader
fop.flush();
fop.close();
System.out.println("The data has been written");
}
else
System.out.println("This file does not exist");
}
private static Contact buildContact() {
Contact contact = new Contact();
contact.setCity("xxxxxx");
contact.setCountry("xxxxxx");
contact.setEmail("test#xxxxxx.com");
contact.setFax("123456789");
contact.setFull_name("Dummy Name");
contact.setOrganization_name("Dummy-technologies");
contact.setPhone("123456789");
contact.setPhone_type("work");
contact.setPhone_2("987456321");
contact.setPhone_2_type("home");
contact.setPhone_3("564789451236");
contact.setPhone_3_type("work2");
contact.setPostal_code("500000");
contact.setState("state");
contact.setStreet1("street1");
contact.setStreet2("stree2");
contact.setTitle("company");
contact.setWebsite_url("www.text.com");
return contact;
}
It is not generating the vcard properly.Can any one help me plz.

Related

How do I output data from a text file to a JTable and have it refresh when the text file is edited?

This is where the data is coming from:
private void btnPlaceOrderActionPerformed(java.awt.event.ActionEvent evt) {
String name = txtClientName.getText();
String address = txtClientAddress.getText();
String date = txtDeliveryDate.getText();
String contact = txtContactInfo.getText();
String small = txtSmall.getText();
String medium = txtMedium.getText();
String large = txtLarge.getText();
BufferedWriter buf;
try{
buf = new BufferedWriter(new FileWriter("orders.txt", true));
buf.write(name + " " + address + " " + small + " " + medium + " " + large + " " + date + " " + contact);
buf.newLine();
buf.close();
JOptionPane.showMessageDialog(this, "Order sent");
} catch (Exception e){
}
}
How would I display the data that gets collected from this into a JTable that updates whenever new data is introduced to the text file?

How to send a string to ffmpeg's input in Java

I am quite new to java, and try as I might, I can't find any examples to help me. I am running ffmpeg as a process and parsing the stderr to get various bits of data - all of which is working fine, but I want to send a "q\n" command to ffmpeg's input from a gui menu item to gracefully quit it whilst it is running when necessary. So all I want to do is send a string programmatically to ffmpeg, the equivalent of sending q return from terminal. Thanks in advance
edit here is the relevant (simplified) section of the code
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { Thread thread = new Thread() {
public void run()
BufferedReader error_reader,input_reader;
InputStreamReader error_isr,input_isr;
String ffmpeg_command = "ffmpeg " + overwrite + " -i " + "\"" + currentfilestring + "\"" + " " + stream + " " + test + " " + findsilence + " " + videocodec + " -b:v " + videoqual + " " + audiocodec + " -ac 2 -ab " + audioqual + " " + res + " " + aspectratio + " " + framerate + " " + "\"" + destdir + destfile + "\"";
System.out.println(ffmpeg_command);
try {
OutputStream stdout;
InputStream stdin;
InputStream stderr;
String errorstr,inputstr;
//Run the ffmpeg
Process ffmpeg = Runtime.getRuntime().exec(ffmpeg_command, null, new File(userDir));
//Get stdin,stderr + stdout
stdin = ffmpeg.getInputStream();
stderr = ffmpeg.getErrorStream();
stdout = ffmpeg.getOutputStream();
stdout.write("\r\n".getBytes());
stdout.flush();
stdout.close();
error_isr = new InputStreamReader(stderr);
error_reader = new BufferedReader(error_isr);
input_reader = new BufferedReader(input_isr);
while (!error_reader.ready()) {
}
while ((errorstr = error_reader.readLine()) != null) {
if(stopconv =="yes"){
//-------------------------------------------------------------------------------------
// TRYING TO INPUT "q\n" TO FFMPEG HERE
//-------------------------------------------------------------------------------------
}
error_isr.close();
error_reader.close();
stdin.close();
stderr.close();
jProgressBar1.setValue(0);
ffmpeg.destroy();
} catch (Exception e) {
e.printStackTrace();
}
};
thread.start();
}

Why my method don't read all of my File? ... Java?

I have to display all of records in my blocks (text files), and do a split to "cover" the Fields Separators, but only the first record of my blocks are diplayed. What am I doing wrong?
enter code here
public static void listAllStudents() throws IOException {
File path = new File(Descriptor.getBlockPath());
for (int i = 0; i < path.listFiles().length; i++) {
try {
FileInputStream file = new FileInputStream(Descriptor.getBlockPath() + "BLK" + i + ".txt");
InputStreamReader entrada = new InputStreamReader(file);
BufferedReader buf= new BufferedReader(entrada);
String piece = " ";
System.out.println("\nBLOCO " + i + " ------------------------------------------------------ +");
do {
if (buf.ready()) {
piece = buf.readLine();
System.out.println("\n¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨");
String string = " ", field[] = piece.split(Descriptor.getFieldSeparator());
string = " ";
System.out.println("CPF: " + field[0]);
System.out.println("Name: " + field[1]);
System.out.println("Course: " + field[2]);
System.out.println("Age: " + field[3]);
System.out.println("Phone: " + field[4]);
System.out.println("Active: " + field[5]);
string = " ";
}
} while (buf.ready());
buf.close();
} catch (IOException e) {
System.out.println();
}
}
}
See the documentation for the BufferedReader.readLine() method:
or null if the end of the stream has been reached
Then change your code to read the file line by line:
while ((piece = buf.readLine()) != null) {
String field[] = piece.split(Descriptor.getFieldSeparator());
if (field.length >= 6) {
System.out.println("CPF: " + field[0]);
System.out.println("Name: " + field[1]);
System.out.println("Course: " + field[2]);
System.out.println("Age: " + field[3]);
System.out.println("Phone: " + field[4]);
System.out.println("Active: " + field[5]);
}
}

Exception in main thread No such element exception

I'm pretty new to Java and I'm having some trouble figuring out where I'm going wrong with my program. I have it so it's doing a mad libs sort of thing where it reads a document with questions(or categories), then prompts for an answer on loop till it has all the answers. It commits these answers to a text file called "answers" then reads the file and prints a message, along with another file containing the full madlib.
I don't actually get an error upon compiling but after I've input all the answers I get
Exception in thread "main" java.util.NoSuchElementException: no line found
-at java.util.Scanner.nextLine(Scanner.java:1540)
-at reader.main(reader.java:68)
Here's the complete code for reference
import java.util.Scanner; // importing scanner object for usage
import java.io.*;
import java.io.PrintWriter;
public class reader{
public static void main(String[] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
System.out.println("enter the name of a file");
String filename = keyboard.nextLine();
File file = new File(filename);
Scanner inputFile = new Scanner(file);
int limit = inputFile.nextInt();
int n;
inputFile.nextLine();
PrintWriter answers = new PrintWriter("answers.txt");
for(n = 0; n < limit; n++)
{
String line = inputFile.nextLine();
System.out.println(line);
String answer = keyboard.nextLine();
answers.println(answer);
}
inputFile.close();
answers.close();
File useanswers = new File("answers.txt");
Scanner inputFile2 = new Scanner(useanswers);
String outputline = inputFile2.nextLine();
String outputline2 = inputFile2.nextLine();
String outputline3 = inputFile2.nextLine();
String outputline4 = inputFile2.nextLine();
String outputline5 = inputFile2.nextLine();
String outputline6 = inputFile2.nextLine();
String outputline7 = inputFile2.nextLine();
String outputline8 = inputFile2.nextLine();
String outputline9 = inputFile2.nextLine();
String outputline10 = inputFile2.nextLine();
String outputline11 = inputFile2.nextLine();
String outputline12 = inputFile2.nextLine();
String outputline13 = inputFile2.nextLine();
String outputline14 = inputFile2.nextLine();
String outputline15 = inputFile2.nextLine();
String outputline16 = inputFile2.nextLine();
String outputline17 = inputFile2.nextLine();
String outputline18 = inputFile2.nextLine();
String outputline19 = inputFile2.nextLine();
PrintWriter result = new PrintWriter("Madlibs_result.txt");
System.out.println("Batman is " + outputline + ". Teenager " + outputline2 +
"was traumatized by " + outputline3 + "his parent's murder and vowed to " + outputline4 +
" their deaths by bringing the " + outputline5 + " to justice. " + outputline6 + " used his " +
outputline7 + "fortune to study criminology, to train his body to " + outputline8 + " perfection, " +
"and to acquire hight tech vehicles and " + outputline9 + " to fight crime in his homw town of " + outputline10 + ". One night " +
outputline11 + "was " + outputline + " by a bat outside his window and decided to dress himself as a \"bat man\" to strike " +
outputline12 + " in the \"" + outputline13 + " and " + outputline14 + "\" hearts of " + outputline15 + ". From that moment forward, " +
outputline16 + " became \"Batman\" in his altered " + outputline17);
result.println("Batman is " + outputline + ". Teenager " + outputline2 +
"was traumatized by " + outputline3 + "his parent's murder and vowed to " + outputline4 +
" their deaths by bringing the " + outputline5 + " to justice. " + outputline6 + " used his " +
outputline7 + "fortune to study criminology, to train his body to " + outputline8 + " perfection, " +
"and to acquire hight tech vehicles and " + outputline9 + " to fight crime in his homw town of " + outputline10 + ". One night " +
outputline11 + "was " + outputline + " by a bat outside his window and decided to dress himself as a \"bat man\" to strike " +
outputline12 + " in the \"" + outputline13 + " and " + outputline14 + "\" hearts of " + outputline15 + ". From that moment forward, " +
outputline16 + " became \"Batman\" in his altered " + outputline17);
result.close();
}
}
Resolved, I had too many String outputline... lines, and I assume when it tried to go to the nonexistent 19th and 20th lines of the text file created, it gave me an error.

An ArrayIndexOutOfBoundsException

I am taking one csv comparing each line with every line of another csv to find matches.
I then need to add some elements from the second csv with some from the first and write it to a new file.
It works for the first lines of the csv then gets the ArrayIndexOutOfBoundsException.
I understand how arrays work and I've checked my csv and as far as I can see I'm not going out of bounds.
The first csv has 8 fields and contains all the customer info. The second has 15 fields and holds sales info on customers. the first 2 fields [0] and [1] are the same in both csv's if there is a record of sales.
If anyone could have a quick look I may be missing something stupid.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at excel.parse.ExcelParse.main(ExcelParse.java:61)
package excel.parse;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.File;
import java.io.IOException;
public class ExcelParse {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
String csvFile2 = "\\\\SBS2011\\RedirectedFolders\\Josh.Hickinbotham\\My Documents\\Customer_Sales_Trends_Summary_by_Sales_Order_114641390.csv";
String csvFile1 = "\\\\SBS2011\\RedirectedFolders\\Josh.Hickinbotham\\My Documents\\All_Customers_Listing_for_Rep_114337469.csv";
BufferedReader br = null;
BufferedReader br2 = null;
BufferedWriter bw = null;
String line = "";
String line2 = "";
String csvSplitBy = ",";
Boolean match = false;
try {
br = new BufferedReader(new FileReader(csvFile1));
bw = new BufferedWriter(new FileWriter("\\\\SBS2011\\RedirectedFolders\\Josh.Hickinbotham\\My Documents\\newcsv.txt"));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] customer = line.split(csvSplitBy);
System.out.println(customer[1]);
br2 = new BufferedReader(new FileReader(csvFile2));
while ((line2 = br2.readLine()) != null) {
String[] file2 = line2.split(csvSplitBy);
if (customer[1].equals(file2[1])) {
match = true;
bw.write(customer[0] + "," + customer[1] + "," + customer[2] + "," + customer[3] + "," + customer[4] + ","
+ customer[5] + "," + customer[6] + "," + customer[7] + ","+ file2[2] +","+ file2[3] + "," + file2[4] + "," + file2[5] + ","
+ file2[6] + "," + file2[7] + "," + file2[8] + "," + file2[9] + "," + file2[10] + "," + file2[11] + "," + file2[12] + ","
+ file2[13] + "," + file2[14]+"\r\n");
System.out.println(":::MATCH " +customer[1]+" : "+file2[1]+" :::");
} else {
match = false;
}
}
if (match == false) {
bw.write(customer[0] + "," + customer[1] + "," + customer[2] + "," + customer[3] + "," + customer[4] + ","
+ customer[5] + "," + customer[6] + "," + customer[7] + "," + "," + "," + "," + "," + "," + "," + "," + "," + "," + ","
+ "," + ","+"\r\n");
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br2 != null) {
try {
br2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("Done");
}
}
Try displaying what customer is before it goes into the if match == false.
May help to see what exactly you are dealing with. :)
What about a simple check on the bounds of the result of "split" ? if you dump your line at the point where the bounds are not OK, you will see the faulty input data....
I think the problem is in your file containing the csv. Maybe debug it and stop at the line where you get the exception to check what data customer contains.
But what I wanted to note:
I'm not sure your program is doing what it is supposed to do. When it finds a match it will print it out, but then keep going in the inner while loop and so at the end match will be false again and you print out the non-match.
So I think you want to put a break:
if (customer[1].equals(file2[1])) {
match = true;
bw.write(customer[0] + "," + customer[1] + "," + customer[2] + "," + customer[3] + "," + customer[4] + ","
+ customer[5] + "," + customer[6] + "," + customer[7] + ","+ file2[2] +","+ file2[3] + "," + file2[4] + "," + file2[5] + ","
+ file2[6] + "," + file2[7] + "," + file2[8] + "," + file2[9] + "," + file2[10] + "," + file2[11] + "," + file2[12] + ","
+ file2[13] + "," + file2[14]+"\r\n");
break;
}

Categories