Results 1 to 6 of 6

Thread: Import a text file in JTextPane

  1. #1
    Join Date
    Dec 2009
    Posts
    204

    Import a text file in JTextPane

    Hello,
    I want to import a text file into the JTextPane in my application. I did not find anything other than going through a temporary file place it in my application's resources, I execute a query on setPage (url) but it do not affect the visible url. So to summarize my problem, I do not get a say import a text file directly into JTextPane. And more to export the text of the JText Pane into a file I pass by his StringBufferInputStream, it progress but also as its the disadvantage of being disparage. Do I have to use any different input stream? Any help will be highly appreciated. Thanks in advance.

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: Import a text file in JTextPane

    Hi,
    I presume that you're embarrassed when you try to view a file, the method setPage request a url, not a file. To resolve the situation, we must not save your file in resources, but use the toURL method File.
    The syntax looks some thing like this
    Code:
    File file;
    file = new File(somefile);
    textpane.setPage(file.toURL().toString());
    Hope this helps you and you resolve your problem.

  3. #3
    Join Date
    Dec 2009
    Posts
    204

    Re: Import a text file in JTextPane

    Hello
    I just do not go through the BufferedReader because I work with Bytes. It is not the method setPage (url) I have a problem is to find a outputStream allows me to write directly into a JTextPane, which is that for the moment I'm the procedure. Here is a part of my code
    Code:
     inputStream = new FileInputStream(currentPathFile);
                            outptst = new FileOutputStream(temp + extend);
    / / Temp and a string representing the file name "temporary".
                             int c;
                            while ((c = inputStream.read()) != -1)
                            outptst.write(c);
                            outptst.close();
                      } catch (java.io.IOException io) {
                            io.printStackTrace();
                       }
                       showNewDoc(temp + extend);
                       }

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Re: Import a text file in JTextPane

    Hello,
    It is not the method setPage (url) I have a problem is to find a outputStream allows me to write directly into a JTextPane, which is that for the moment I'm the procedure.
    Excuse me, I understand nothing, an InputStream never allowed to write anywhere, just read. If you want to write, through an OutputStream (this will be easier, I guess ) Directly in a JTextPane, the basic principle is simply to redefine a method write in order to return the character written in the text the JTextPane.

  5. #5
    Join Date
    Dec 2009
    Posts
    204

    Re: Import a text file in JTextPane

    Hi,
    I have posted this part of my code, just go through it and see if I am correct or going somewhere wrong.
    Code:
          java.net.URL du = null;
                  try{
                  du = PhaseTest.class.getResource(stg);
                       if (du! = null) {
                            jt.setPage(du);
                            jt.validate();
                       }
    If I am incorrect some where then please guide me.

  6. #6
    Join Date
    May 2008
    Posts
    2,297

    Re: Import a text file in JTextPane

    Hello,
    Just follow the below code, this will help you , how you can use the input stream and the output stream.
    Code:
    class example
    { 
        Public void write(String text) 
        { 
            String add = System.getProperty("user.dir") + "\\myfile.txt "; 
      
            try 
            { 
               
                FileWriter fwri = new FileWriter(add, true); 
                BufferedWriter up = new BufferedWriter(fwri); 
                up.write(text); 
                 up.flush(); 
                up.close(); 
            } 
            catch(IOException ioe){System.out.System.out.println("Error:" + Ioe );} 
      } 
         Public void read() 
        { 
           try 
            { 
             
             String add = System.getProperty("user.dir") + "\\myfile.txt ';
                FileReader fread = new FileReader(add);
                BufferedReader br = new BufferedReader(en);
                 
                String text = ""; 
                int a = 0; 
                    while(a <2) 
                    { 
                        text = text + br.readLine() + "\ n";
                        a + +;         
                    } 
                br.close(); 
                 
                returns null 
                System.out.System.out.println(text); 
           
            } 
            catch(IOException ioe){System.out.System.out.println("Error:" Ioe +);} 
             
        } 
    }

Similar Threads

  1. Replies: 5
    Last Post: 13-01-2012, 05:18 PM
  2. Import AS400 formated text into InDesign?
    By Braasch in forum Windows Software
    Replies: 3
    Last Post: 16-07-2010, 04:38 PM
  3. import text file into ms access
    By Clay Aiken in forum Windows Software
    Replies: 2
    Last Post: 23-06-2009, 08:30 PM
  4. How to import Text into Access through VBA
    By appueye in forum Software Development
    Replies: 2
    Last Post: 17-04-2009, 11:10 PM
  5. Vba import text files
    By Naval in forum Software Development
    Replies: 1
    Last Post: 24-03-2009, 11:35 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,063,465.55615 seconds with 17 queries