Saving code in java

Java save data in file java code example

In your case before you use executUpdate method you will have to supply the values using your string fields, refer Supplying Values of PreparedStatement parameters. Solution 1: Creating a PreparedStatement Object The following creates a PreparedStatement object that takes two input parameters: Supplying Values for PreparedStatement Parameters You must supply values in place of the question mark placeholders (if there are any) before you can execute a PreparedStatement object.

How to save the data into File in java?

Use a timestamp in the filename so you can be sure it is unique. Below example uses a timestamp in milliseconds which should be enough in most cases.

If you expect you can have multiple files within 1 millisecond then you could do something with a GUID/UUID. Note that GUID/UUID could result in duplicates too, however this chance is extremely rare.

import java.io.*; class FileWrite < public static void main(String args[]) < try< // Create file FileWriter fstream = new FileWriter(System.currentTimeMillis() + "out.txt"); BufferedWriter out = new BufferedWriter(fstream); out.write("Hello Java"); //Close the output stream out.close(); >catch (Exception e) > > 

You don’t need to compute the filename by yourself, have a look at File.createTempFile.

  1. The file denoted by the returned abstract pathname did not exist before this method was invoked, and
  2. Neither this method nor any of its variants will return the same abstract pathname again in the current invocation of the virtual machine.

A one liner. Using base 36 for the ids will make the file names shorter.

IOUtils.write(text, new FileWriter(Long.toString(System.currentTimeMillis(), 36)+».txt»)));

How to save a file in java, I am trying to create a file from a log report. To save the file I’ve created a button. When the button is pushed, the following code is executed: public void SAVE_REPORT(KmaxWidget widget)Save data in an XML file

There is very good framework JAXB for this also there is Simple

But I have used this XStream

Person joe = new Person("Joe", "Walnes"); joe.setPhone(new PhoneNumber(123, "1234-456")); joe.setFax(new PhoneNumber(123, "9999-999")); 

Now, to convert it to XML, all you have to do is make a simple call to XStream:

String xml = xstream.toXML(joe); 

The resulting XML looks like this:

 Joe Walnes 123 1234-456  123 9999-999   

There are many open source libraries, but I would simply use JAXB, the standard. Although I have to say the XStream library suggested by other answerers looks very promising, too!

Читайте также:  Подключить css файл странице

Consider using xstream (http://x-stream.github.io/). XStream’s API is very simple:

YourObjectGraph yourData=buildYourData(); XStream xstream=new XStream(); String yourXML=xstream.toXml(yourData); // do something with your shiny XML 

Importing is just as easy:

YourObjectGraph yourData=(YourObjectGraph)xstream.fromXml(yourXml); 

How to save the data into File in java?, One solution can be, use a random number generator to generate a random number. Use this random number with some text as a filename. Maintain a list of already used names and each time you are saving the file, check through this list if the file name is unique. Share.

Making a save button work in java

Alright for starters, you didn’t add an ActionListener to your save button, so you need to add it like this.

 btnSave = new JButton ("Save"); btnSave.setBounds(130, 350, 100, 20); btnSave.setForeground(Color.blue); btnSave.addActionListener(new SaveHandler()); panel.add (btnSave); btnSave.setVisible(true); 

Also, in your SaveHandler , your PrintWriter isn’t actually printing anything.

Your Code: out.println( ); would not write anything.

Let’s say you wanted to write the contents of txtFirstName, you would need to do

Edit: You also need another closing braces at the end of your ExitHandler class

class ExitHandler implements ActionListener < public void actionPerformed(ActionEvent e) < int n = JOptionPane.showConfirmDialog(frame1, "Are you sure you want to exit?", "Exit?",JOptionPane.YES_NO_OPTION); if(n == JOptionPane.YES_OPTION)< System.exit(0); System.out.println("EXIT SUCCESSFUL"); >> > 

You will also then need to remove a closing brace at the end of the program.

Class — Java- Save object data to a file, Add a comment. 1. You can use the XMLDecoder/XMLEncoder to serialize JavaBean as xml. Here are the examples from oracle’s Javadocs on the two classes: (XMLDecoder) XMLDecoder d = new XMLDecoder ( new BufferedInputStream ( new FileInputStream («Test.xml»))); Object result = …

Code that could save data by entering through jframe to database

Creating a PreparedStatement Object

The following creates a PreparedStatement object that takes two input parameters:

String updateString = "update " + dbName + ".COFFEES " + "set SALES = ? where COF_NAME = ?"; updateSales = con.prepareStatement(updateString); 

Supplying Values for PreparedStatement Parameters

You must supply values in place of the question mark placeholders (if there are any) before you can execute a PreparedStatement object. Do this by calling one of the setter methods defined in the PreparedStatement class. The following statements supply the two question mark placeholders in the PreparedStatement named updateSales:

updateSales.setInt(1, e.getValue().intValue()); updateSales.setString(2, e.getKey()); 

The first argument for each of these setter methods specifies the question mark placeholder. In this example, setInt specifies the first placeholder and setString specifies the second placeholder. Link: http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

Читайте также:  Ajax POST request with JQuery and PHP - Tutsmake

In your case before you use executUpdate method you will have to supply the values using your string fields, refer Supplying Values of PreparedStatement parameters.

1) Bind all the values on the JFrame to Java Pojo or class. See the answer : Binding of JText fields value to Info Class

2) Now bind an action method to the save button, here you can use the class of step 1 to get the values entered through form. Now all you have to do is to create the connection with the DB and insert the data.

Read about Variable binding in swing to get a better idea.

Swing — How to «Open» and «Save» using java, I want to make an «Open» and «Save» dialog in java. An example of what I want is in the images below: Open: Save: How would I go about doing this? java swing jfilechooser. Share. Improve this question. Follow edited Oct 25, 2013 at 18:51. notquiteamonad. 1,012 1 1 gold badge 11 11 silver badges 27 27 bronze …

Источник

How to write code data into a text file

I just did a simple code which takes user name and phone number and save those into an arraylist by creating object. I want to save those information (name and phonenumber) into a text file so that all old information I can get again. How do I do it? Here is my code .

import java.util.ArrayList; import java.util.Scanner; public class manager < Scanner input = new Scanner(System.in); ArrayList Test = new ArrayList (); public void mainloop() < for (int i = 0; i < 100; i++) < String x; System.out.println("Please Select your option"); System.out.println(". "); System.out.println("1 ADD NAME AND NUMBER\n2 SEARCH NAME AND NUMBER \n0 EXIT"); System.out.println(". "); x = input.nextLine(); if (x.equalsIgnoreCase("0")) < System.out.println("Thank you!"); break; >if (x.equalsIgnoreCase("1")) < String Name; String Number; System.out.println("Please Enter your Name below"); Name = input.nextLine(); System.out.println("Please Enter your Number below"); Number = input.nextLine(); objectclass objectclassObject = new objectclass(Name, Number); Test.add(objectclassObject); >if (x.equalsIgnoreCase("2")) < String y; System.out.println("*** Enter your Name below for search ***"); y = input.nextLine(); for (objectclass p : Test) < String z = p.getName(); if (z.equalsIgnoreCase(y)) < System.out.println("Your Name is: " + p.getName() + "\nYour Number is: " + p.getNumber()); System.out.println(""); >else < System.out.println("Contact not Found!\n"); >> > > > > 

I want to save all name and number that I store in arraylist into a text file . how can I do it? I tried this so far but don’t know what to do next . import java.io.; import java.lang.; import java.util.*;

public class creatfile < private Formatter x; public void openFile()< try< x = new Formatter("testkyo"); >catch (Exception e) < System.out.println("you have an error"); >> public void addRecord() < x.format(); >public void closeFile()

Источник

Читайте также:  Php get utf8 code

How to save java source code in an xml file, and edit/compile it in an IDE?

We have an java application in which the user can write/execute their own java code and use imports from compiled jars — i.e. they write it, and it is compiled and run by the application. They can also save this code (along with various other information that they are using) — currently this is saved to a human-readable xml file. I want to be able to use those save xml files in an IDE (principally, Intellij), so that if the user changes things in their compiled jar in the IDE, these changes can also be picked up in the save xml file. For example, if a save file used a class from the compiled jar, it may have the following import:

import com.company.project.package.subpackage.MyClass; 
import com.company.project.package.subpackage.subsub.MyClass; 
  • Does this seem like a reasonable approach?
  • Are there alternative approaches that anyone could suggest?

Because, as my question states, it’s being saved along with various other information. I considered the possibility of two save files (i.e. one for code, one for other stuff. ) but I determined that the danger of those getting mismatched/corrupted etc. was too great.

You could use custom annotations within the javadoc of the source file’s header, to keep everything in one place. Depends how complicated your metadata is.

«I’m confident that saving as a .java file is not the solution I’m looking for.» Save it as a Zip with 1 (or more) XML files as well as any source (in paths according to package) that is required. This has the twin advantage of allowing source to be a different encoding to the XML, as well as consolidating all the necessary parts of the project. You could even include other files easily, a manifest, help files etc.

@AndrewThompson — I’ve given your suggestion some further thought and and am going to have a go at implementing it. Can you post it as an answer, so I can up-vote it (and, possibly, accept it)?

Источник

Оцените статью