简体   繁体   中英

How to retrieve data from textarea or text field and store in a text file with the same data format, java

Hi I have created a text field in java and i am able to get the data from text field and store in a .txt file. But My problem is that let the my data in textfield is in bold and italic format and the font colour is red. Now I want to store the data exactly in the same format in .txt file. I donot know how to write code for that.

Anyone please help me to solve this type of problem

Thanks Sunil Kumar Sahoo

It would be easier to see the code you've got so far. Also it would be a bit more motivating to complete your snippet.

Anyway:

Using bold or italic in a textfield is implies that you use html in swing

How to read/write files plese check Java I/O tutorial

Here is my simple suggestion..

Make an .html file with the help of any editor software

note that its not for further use,just for your easiness.

After that copy contents of html file to java file..carefully,,

Here is a simple example

Suppose a Text box named Text1 that holds a name,then

FileOutputStream fos = null;
    try {
        File fout = new File("Drive:/Folder/name_of_file.html");
        fos = new FileOutputStream(fout);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
        bw.newLine();
        bw.newLine();
        bw.write("<html xmlns='http://www.w3.org/1999/xhtml'>\n" +
         "<head>\n" +
         "<body>\n" +
         "<p id='p1'>My Name is <font color='red';>"+ Text1.getText() + "</font><p>\n" +
         "</body>\n" +
         "</head>\n" +
         "</html>");
         bw.close();
    }

        catch(Exception d){
         d.getmessage();
        }    

    try {
                Runtime rt = Runtime.getRuntime();
                String fil = "Drive:/Folder/name_of_file.html";
                String path = "C:\\Program Files\\Google\\Chrome\\Application\\chrome ";//path of your browser ..(There should be a single space required after path file. in my case  after \\chrome ")                        
                Process p = rt.exec(path + fil);
            } catch (IOException ex) {
                System.out.print(ex);
            }

You can add styles what ever you need,In This case I just add only font color property, In this line

 <p id='p1'>My Name is <font color='red';>"+ Text1.getText() + "</font><p>\n" 

I penetrate my java textbox value.with red color while running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM