Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

How to see an IO file

Hi,

I am retreiving data from oracle from my java application in an xml format and then im writing it to a file using buffered writer, I would like to show this output file, how can i show this file to users?

import java.sql.*;
import java.math.*;
import oracle.xml.sql.query.*;
import oracle.jdbc.*;
import oracle.jdbc.driver.*;
import java.io.*;


public class xmlretTest {
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
    String tableName = "emp1";
    Connection conn;
   
    try
    {
     DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
                     
     conn = DriverManager.getConnection ("");
                       
     OracleXMLQuery qry = new OracleXMLQuery(conn, "select * from emp1");
     
     String sx = qry.getXMLString();
     
     
                     try
   
                     {
                         
                     File temp = File.createTempFile("pattern", ".suffix");
                     BufferedWriter out = new BufferedWriter(new FileWriter(temp));

                     out.write(sx);
                     out.toString();
                     //System.out.println(sx);
                     }
                     
                     catch (IOException e) {
                                        System.out.println(e);
                                  }
    }
   
  catch (SQLException e)
              {
                 e.printStackTrace();      
              
                     System.out.println(e);
                                          
              }
}
}
0
vihar123
Asked:
vihar123
  • 6
  • 5
  • 5
  • +1
1 Solution
 
EmildaCommented:
As sx is in String format you can print it out using System.out.println(sx).
0
 
vihar123Author Commented:
I want to show it as some txt file
0
 
vihar123Author Commented:
Is my question not clear????
why am i not getting any replies?
please its urgent, all contributions will be appreciatted
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
EmildaCommented:
Yes, question is not clear. What do you mean by showing it as a text file.
0
 
vihar123Author Commented:
to show the retreived data in a .txt file, for eg:- taking all the data into display.txt and showing it to the users
0
 
sciuriwareCommented:
Well create that text file ......

;JOOP!
0
 
EmildaCommented:
In the code you have shown you are already writing it into a file. Do you want to pop up application like notepad and show the contents of display.txt in it.

Is that what you want.

You can pop-up notepad by using

Runtime r = Runtime.getRuntime();
r.exec(path_to_noteopad);
0
 
sciuriwareCommented:
If not, add:

        temp.close();
        temp.renameTo("display.txt");
just after:
                     out.toString();
                     //System.out.println(sx);
;JOOP!
0
 
vihar123Author Commented:
>>> Do you want to pop up application like notepad and show the contents of display.txt in it.

ya i want this, can u send me in detail, im unable to understand where to write what :-)
0
 
EmildaCommented:
You can include these lines below the statement to close the file.

temp.write(..)
temp.close();

Runtime r = Runtime.getRuntime();
r.exec(path_to_noteopad);

But this can only pop-up Notepad and user needs to later open up the text file from the saved location.
0
 
zzynxSoftware engineerCommented:
Try

public class xmlretTest {
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
    String tableName = "emp1";
    Connection conn;
   
    try
    {
     DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
                     
     conn = DriverManager.getConnection ("");
                       
     OracleXMLQuery qry = new OracleXMLQuery(conn, "select * from emp1");
     
     String sx = qry.getXMLString();
     
     
                     try
   
                     {
                         
                     File temp = File.createTempFile("pattern", ".suffix");
                     BufferedWriter out = new BufferedWriter(new FileWriter(temp));

                     out.write(sx);
                     out.close();

                     temp.renameTo("display.txt");
                     Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " + temp.getAbsolutePath());
                   
                     }
                     
                     catch (IOException e) {
                                        System.out.println(e);
                             }
    }
   
  catch (SQLException e)
             {
                e.printStackTrace();      
             
                    System.out.println(e);
                                     
             }
}
}
0
 
zzynxSoftware engineerCommented:
Explanation:

>> temp.renameTo("display.txt");
This renames the file to a *.txt file

>> Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " + temp.getAbsolutePath());
This starts up the program related to a *.txt file (notepad or whatever other program)

0
 
EmildaCommented:
That's cool.
0
 
sciuriwareCommented:
Thanks, zzynx.
;JOOP!
0
 
zzynxSoftware engineerCommented:
:°)

Let's hope vihar feels like that too ;°)
0
 
vihar123Author Commented:
thanks zzynx its working, can u also tell me how to send this file to a database. thanks :-)
0
 
zzynxSoftware engineerCommented:
>> thanks zzynx its working
Good! Question closed then.

>>Can u also tell me how to send this file to a database
Yes. But that's another question.
0
 
vihar123Author Commented:
>>>>Yes. But that's another question.

hey thats cool answer :-)), ok ill put it as a new question.

thanks
0
 
zzynxSoftware engineerCommented:
Thanks

>> hey thats cool answer
;°)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now