I want every data to be retrieved once looping will occur in the CSV writer, how do I make the loop occur?
thanks!
Data from Firebase
Output writer
*sorry, i can't display images 'cause it must have 10 reputations :(
try {
File root = new File(Environment.getExternalStorageDirectory() + "/StockCount/");
if (!root.exists()) {
root.mkdirs();
}
File myCSV = new File(root, currentDate + " DataStockCount.csv");
myCSV.createNewFile();
FileWriter writer = new FileWriter(myCSV);
writer.append("Date : " + getDate + "\n");
writer.append("Inspector : " + getInspector + "\n");
writer.append("Location : " + getLocation + "\n");
writer.append("Product Name : " + getProductName + "\n");
writer.append("Price : " + getPrice + "\n");
writer.append("Quantity : " + getAmount + "\n");
writer.append("Barcode : " + getCode + "\n");
writer.flush();
writer.close();
Toast.makeText(this, "File Saved Successfully!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Error : " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
Why not using an existing library like FastCSV?
Related
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?
I am trying to save a file as *.xlsx without overwriting the existing file with the same name. I thought to add number suffixes to new file names, like file(1).xlsx, file(2).xlsx in case only if the specified file exists. Here what I have tried so far:
do {
s = JOptionPane.showInputDialog("Enter the name of the file name");
File tmpDir = new File(System.getProperty("user.home"), "Documents\\Challan_Reports\\" + s + ".xlsx");
boolean exists = tmpDir.exists();
if (exists) {
JOptionPane.showMessageDialog(this, "File Name Already exists try again!");
}
else {
break;
}
} while (true);
if (s.equals("") || s.equals(null)) {
//.........................................................................
File tmpDir = new File(System.getProperty("user.home"), "Documents\\Challan_Reports\\" + gname + " " + date + ".xlsx");
boolean exists = tmpDir.exists();
if (exists) {
JOptionPane.showMessageDialog(this, "exists 1");
for (int m= 1; true;m++)
{
JOptionPane.showMessageDialog(this, "exists m " + m);
File tmpDir1 = new File(System.getProperty("user.home") + "\\Documents\\Challan_Reports\\" + gname + " " + date + " (" + m + ").xlsx");
System.out.println(System.getProperty("user.home") + "\\Documents\\Challan_Reports\\" + gname + " " + date + " (" + m + ").xlsx");
boolean exists1 = tmpDir1.exists();
if (exists) {
System.out.println("exists file");
continue;
}
else {
System.out.println(" not exists");
filename = System.getProperty("user.home") + "\\Documents\\Challan_Reports\\" + gname + " " + date + " (" + m + ").xlsx";
break;
}
}
}
// FileOutputStream out = new FileOutputStream(new File(System.getProperty("user.home"),"Documents\\Challan_Reports\\"+gname+" "+date+".xlsx"));
// workbook.write(out);
// out.close();
}
FileOutputStream out = new FileOutputStream(new File(filename));
workbook.write(out);
out.close();
System.out.println(
"Writesheet.xlsx written successfully");
JOptionPane.showMessageDialog(this, filename + ".xlsx \n\\Generated at path" + System.getProperty("user.home") + "\\Documents\\Challan_Reports");
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}
The problem is that it gives the output as the file(1) or file(2)..... exists
when it doesn't. If condition in the loop works only the first time if I put the condition as if(!exists). Please help me out.
You are doing check mistake, instead checking for variable exists1 , you are checking exists and that is true from before for loop part.
try changing as below.
boolean exists1 = tmpDir1.exists();
if(exists1)// change here exists to exists1
{
System.out.println("exists file");
continue;
}
else{
System.out.println(" not exists");
filename=System.getProperty("user.home")+"\\Documents\\Challan_Reports\\"+gname+" "+date+" ("+m+").xlsx";
break;
}
In the condition where you are checking the file existence you are not doing anything right? Not appending anything in name hence if file exists, it will go ahead and try to save only the same name. Please try to append once you found the file existence
here's my problem in java, my button is set on public because it is on different window and now i put a function to this button but when I always open the window that the button is included the button is always set to false even the button is clicked it is not functioning.
BTW
veiwTable is a new window:(maybe somebody will laugh to my spelling but I intentionally set it to wrong due to my other variables :) )
convertToTxt is a button
I input else to check if the function is set to false when opening the window
here is my code:
if(veiwTable.convertToTxt.isSelected()) {
try{
File file = new File("e:\\Data Logs\\ " + sn + "_" + status + ".txt");
if(!file.exists()){
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Board Name: " + boardName);
bw.newLine();
bw.write("Part Number: " + pn);
bw.newLine();
bw.write("Serial Number: " + sn);
bw.newLine();
bw.write("Board Revision: " + bRev);
bw.newLine();
bw.write("Failing Test Parameter: " + failingTest);
bw.newLine();
bw.write("Failing Checker: " + checker);
bw.newLine();
bw.write("Verified By: " + verifiedBy);
bw.newLine();
bw.write("Remakrs: " + remarks);
bw.newLine();
bw.write("Tester Number: " + testerNumber);
bw.newLine();
bw.write("Datalog:");
bw.newLine();
bw.write(Datalogs );
bw.close();
String note = boardName.concat(" with ").concat(sn).concat(" is located on 'ETS88-spare'\'E:'\'Data Logs'"); //" with " + sn " is located on 'EData Logs'"
JOptionPane.showMessageDialog(null, note);
}catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}
} else JOptionPane.showMessageDialog(null, "none");
update: my program is now running properly after converting to string all the data on the text box and call it afterwards.
private void convertToTxtActionPerformed(java.awt.event.ActionEvent evt) {
//////not included on the generated datalogs///
String lastDevice = jLastDevice.getText();
String progname = jProgramName.getText();
String progRev = jProgramRevision.getText();
/////////////////////////////////////////////
String boardname = jBoardname.getText();
String pn = jPN.getText();
String sn = jSN.getText();
String boardrev = jBoardRev.getText();
String verifStatus = jVerificationStatus.getText();
String failedTNum = jFailedTNum.getText();
String checker = jFailingChecker.getText();
String tester = jTesterNumber.getText();
String remarks = jRemarks.getText();
String verifiedBy = jVerifiedBy.getText();
String dLogs = jDatalog.getText();
try{
File file = new File("\\\\192.168.1.100\\e\\Data Logs\\ " + sn + "_" + verifStatus + ".txt");
if(!file.exists()){
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Board Name : " + boardname);
bw.newLine();
bw.write("Part Number : " + pn);
bw.newLine();
bw.write("Serial Number : " + sn);
bw.newLine();
bw.write("Board Revision : " + boardrev);
bw.newLine();
bw.write("Failing Test Parameter : " + failedTNum);
bw.newLine();
bw.write("Failing Checker : " + checker);
bw.newLine();
bw.write("Verified By : " + verifiedBy);
bw.newLine();
bw.write("Remakrs : " + remarks);
bw.newLine();
bw.write("Tester Number : " + tester);
bw.newLine();
bw.write("Datalog");
bw.newLine();
bw.newLine();
bw.write(dLogs );
bw.close();
String note = boardname.concat(" with ").concat(sn).concat(" is located on 'ETS88-spare'\'E:'\'Data Logs'"); //" with " + sn " is located on 'EData Logs'"
JOptionPane.showMessageDialog(null, note);
//System.out.println(note);
}catch(IOException | HeadlessException e) {
JOptionPane.showMessageDialog(null, e);
}
}
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.
brightonStores = BrightonUtil.getStoreInfo(file.getName(), dir.getAbsolutePath(),mailInfoST);
String status = "604";
if (!brightonStores.isEmpty()) {
status = brightonStoreService.saveDeleteBrightonStore(brightonStores,mailInfoST);
storeLog.info("Adding Store records Status:: "+status);
if (status.equals("600")) {
storeLog.info("File: " + file.getName() + " No of records: " + brightonStores.size() + " :: Added");
mailInfoST.setToSend(false);
//mailTimerTaskST.cancel();
} else {
storeLog.info("File: " + file.getName() + " :: Error in adding records");
writer.append("File: " + file.getName() + " :: Error in adding records"+"\n" + mailInfoST.getResult());
writer.close();
BrightonUtil.setMailInfo(mailInfoST, true, false, "File: " + file.getName() + " :: Error in adding records");
mailTimerST.schedule(mailTimerTaskST, new java.util.Date(), 1000*configurationManager.getFileUploadErrorMailTimer());
}
} else {
storeLog.info("File: " + file.getName() + " :: Error in adding records");
storeLog.info("File: " + file.getName() + " :: No record found ");
writer.append("File: " + file.getName() + " :: Error in adding records");
writer.append("\nFile: " + file.getName() + " :: No record found ");
if(null != mailInfoST.getResult()){
storeLog.info(mailInfoST.getResult());
writer.append("\n"+mailInfoST.getResult());
}
writer.close();
BrightonUtil.setMailInfo(mailInfoST, true, false, "File: " + file.getName() + " :: Error in adding records");
mailTimerST.schedule(mailTimerTaskST, new java.util.Date(), 1000*configurationManager.getFileUploadErrorMailTimer());
}
} else if(validationStatus.equals("604")){
storeLog.error("Error in Brighton Store data file :: Invalid structure or value :: "+ file.getName());
writer.append("Error in Brighton Store data file :: Invalid structure or value :: "+ file.getName() + "\n" + mailInfoST.getResult());
writer.close();
BrightonUtil.setMailInfo(mailInfoST, true, false, "Error in Brighton Store data file :: Invalid structure or value :: "+ file.getName());
mailTimerST.schedule(mailTimerTaskST, new java.util.Date(), 1000*configurationManager.getFileUploadErrorMailTimer());
} else {
storeLog.error("Error in reading data from Brighton Store file :: " + file.getName());
writer.append("Error in reading data from Brighton Store file :: " + file.getName()+ "\n" + mailInfoST.getResult());
writer.close();
BrightonUtil.setMailInfo(mailInfoST, true, false, "Error in reading data from Brighton Store file :: " + file.getName());
mailTimerST.schedule(mailTimerTaskST, new java.util.Date(), 1000*configurationManager.getFileUploadErrorMailTimer());
}
brightonStores = null;
java.lang.Runtime.getRuntime().gc();
Is it possible reshedule TimerTask in a Timer..
You can't reschedule a TimerTask within a Timer. If you want to change the schedule that the task is run on, you need to cancel the old timer and create a new one.