want to get rid of Overwriting in filehandling - file-handling

My code is perfect but still my cursor is on the beginning of text my and keep overwriting previous input whenever I give new input
String username=Cuser.getText();
String password=Cpass.getText();
FileWriter filewriter;
try{
filewriter=new FileWriter("Record.txt");
try (BufferedWriter bufferedWriter = new BufferedWriter(filewriter)) {
bufferedWriter.write("Username"+" :" + username+" Password"+" :" + password+"\n");
// bufferedWriter.write();
JOptionPane.showMessageDialog(null, "Account Created Successfully!");
bufferedWriter.close();
}
} catch (IOException ex){
JOptionPane.showMessageDialog(null, "Error!");
System.out.println("Error! ");
}

So I found out my error
try (BufferedWriter bufferedWriter = new BufferedWriter(filewriter) Visible )
that visible was at end was not in my program which was causing to create new data each timing

Related

SQL Try..Catch..Finally Errors Not Displaying

How can I get the SQLException error messages below to display?
I have a site where users upload a data file. I have intentionally added incorrect data (text to a field that only allows integers) as a test of my error handling. Nothing is rendering in the view. I am sure that an exception is being thrown, as none of the data is in the database. If I remove the incorrect field, everything uploads fine. I have also tried both ex.toString() and ex.message with no success.
In my view, I have:
#ViewBag.ErrorMessage
Controller:
using (SqlCommand command = new SqlCommand(sql2, con))
{
try
{
// long SQL statement
con.Open();
command.ExecuteNonQuery();
TempData["shortMessage"] = "Dataset Uploaded Successfully.";
}
catch (SqlException ex)
{
ViewBag.ErrorMessage = ex.ToString();
}
catch (Exception ex)
{
ViewBag.ErrorMessage = ex.ToString();
}
finally
{
con.Close();
}
}

I am not able to parse IOS driver page source

I got Page source using
String pageSource = driver.getPageSource();
Now i need to save this xml file to local in cache. So i need to get element attributes like x and y attribute value rather than every time get using element.getAttribute("x");. But I am not able to parse pageSource xml file to some special character. I cannot remove this character because at if i need element value/text it shows different text if i will remove special character. Appium is use same way to do this.
I was also facing same issue and i got resolution using below code which i have written and it works fine
public static void removeEscapeCharacter(File xmlFile) {
String pattern = "(\\\"([^=])*\\\")";
String contentBuilder = null;
try {
contentBuilder = Files.toString(xmlFile, Charsets.UTF_8);
} catch (IOException e1) {
e1.printStackTrace();
}
if (contentBuilder == null)
return;
Pattern pattern2 = Pattern.compile(pattern);
Matcher matcher = pattern2.matcher(contentBuilder);
StrBuilder sb = new StrBuilder(contentBuilder);
while (matcher.find()) {
String str = matcher.group(1).substring(1, matcher.group(1).length() - 1);
try {
sb = sb.replaceFirst(StrMatcher.stringMatcher(str),
StringEscapeUtils.escapeXml(str));
} catch (Exception e) {
e.printStackTrace();
}
}
try {
Writer output = null;
output = new BufferedWriter(new FileWriter(xmlFile, false));
output.write(sb.toString());
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if you will get that kind of problem then catch it with remove special character and parse again.
try {
doc = db.parse(fileContent);
} catch (Exception e) {
removeEscapeCharacter(file);
doc = db.parse(file);
}
It might works for you.
I can able to do same using SAXParser and add handler to do for this.
Refer SAX Parser

Is it possible to subcribe to a sentence of an epl module?

I have deployed an epl module with the code:
InputStream inputFile = this.getClass().getClassLoader().getResourceAsStream("Temperature.epl");
if (inputFile == null) {
inputFile = this.getClass().getClassLoader().getResourceAsStream("etc/Temperature.epl");
}
if (inputFile == null) {
throw new RuntimeException("Failed to find file 'Temperature.epl' in classpath or relative to classpath");
}
try {
epService.getEPAdministrator().getDeploymentAdmin().readDeploy(inputFile, null, null, null);
// subscribers Ok, tested before whith epService.getEPAdministrator().createEPL ()
// sentences ok, printed
EPStatement statement;
statement = epService.getEPAdministrator().getStatement("Monitor");
System.out.println(statement.getText() + ";");
statement.setSubscriber(new MonitorEventSubscriber());
statement = epService.getEPAdministrator().getStatement("Warning");
System.out.println(statement.getText() + ";");
statement.setSubscriber(new WarningEventSubscriber());
statement = epService.getEPAdministrator().getStatement("Error");
System.out.println(statement.getText() + ";");
statement.setSubscriber(new ErrorEventSubscriber());
}
catch (Exception e) {
throw new RuntimeException("Error deploying EPL from 'Temperature.epl': " + e.getMessage(), e);
}
I can get the sentences by statement.getText(), but the subscribers are not activated. What it's wrong?
I'm working with Esper 5.0.0
Seeing that your code uses the current classloader, you'd want to make sure the classloader is the same else you can get different engine instances.
Also have your code actually send an event to see if it matches since this code doesn't send events.

Why my file don't write text inserted?

I' doing an API for Black Berry but now I have a problem, the file don't save text inserted , for example when I write on application running : Hello , I guess that "Hello" is saving in EditField pt_nombre,
name = new LabelField("Nombre");
pt_nombre = new FixedWidthEditField();
String a = pt_nombre.getText();
String nom = name.getText();
String fullPath = "file:///SDCard/xxx.txt";
try {
FileConnection fconn = (FileConnection) Connector.open(fullPath, Connector.READ_WRITE);
if (fconn.exists()) {
fconn.delete();
}
fconn.create();
OutputStream os = fconn.openOutputStream();
os.write(nombre.getBytes());
os.write("\r\n".getBytes());
os.write(a.getBytes());
fconn.close();
} catch (IOException e) {
System.out.println("Oh noes!!1! " + e.toString());
}
When I check the xxx.txt only are write the Label, Nombre: but the written text in EditField isn't !! help please
Are you sure this code even compiles? I don't see where nombre is declared (even to the extend that name and pt_nombre are 'declared'); nor is there anywhere that you set a value into pt_nombre, so I don't know what you rexpect to see in the file.
you should check FixedWidthEditField getText function.
will it return the text that user input

How to print a JTable header in two lines?

Program is to print a JTabel and used function is
JTabel jt=new JTable();
MessageFormat headerFormat= new MessageFormat("My World Tomorrow");
MessageFormat footerFormat = new MessageFormat("Page {0}");
jt.Print(JTabel.Format,headerFormat,footerFormat);
Query is: How to print the header in two lines that is
My World
Tomorrow
Tired following solutions:
new MessageFormat("My world \n Tomorrow");
new MessageFormat("My world \r\n Tomorrow");
new MessageFormat("My world" System.getProperty("line.separator")+"Tomorrow"
);
Nothing works.
try this.
PrinterJob job = PrinterJob.getPrinterJob();
MessageFormat[] header = new MessageFormat[3];
header[0] = new MessageFormat("");
header[1] = new MessageFormat("line 1");
header[2] = new MessageFormat("line 2");
MessageFormat[] footer = new MessageFormat[2];
footer[0] = new MessageFormat("footer 1");
footer[1] = new MessageFormat("footer 2");
job.setPrintable(new MyTablePrintable(tblmunim, PrintMode.FIT_WIDTH, header, footer));
if (job.printDialog())
try {
System.out.println("Calling PrintJob.print()");
job.print();
System.out.println("End PrintJob.print()");
}
catch (PrinterException pe) {
System.out.println("Error printing: " + pe);
}
originally posted here http://sandeepsharma11.blogspot.com/2010/12/java-how-to-print-table-with-multi-line.html

Resources