Results 1 to 4 of 4

Thread: [Java] read and write a file

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default [Java] read and write a file

    Hi,
    I have one problem; I must read a html like this;
    Qt Code:
    1. <html>
    2. <body>
    3. Hello
    4. </body>
    5. </html>
    To copy to clipboard, switch view to plain text mode 
    I read this in a string (correct?) and after I have to do some changes to it and write it on disk but in this way:
    Qt Code:
    1. <html>\n<body> ........
    To copy to clipboard, switch view to plain text mode 
    There, there's the '\n' because in the original file <body> was in another line of <html>
    How can I do this? This my code for read and write:
    Qt Code:
    1. File fin = new File(FILE_NAME);
    2. FileReader in = new FileReader(fin);
    3. istream = new BufferedReader( in );
    4. String line = null;
    5.  
    6. while ( (line = istream.readLine()) != null ) {
    7. //System.out.println(line);
    8. sb.append(line);
    9. }
    10. // here do changes on the sb
    11. String text = sb.toString();
    12. FileWriter fw = new FileWriter(new File ("out.txt") );
    13. out = new PrintWriter(fw);
    14. out.println(text);
    To copy to clipboard, switch view to plain text mode 

    Thanks,
    Last edited by mickey; 22nd June 2008 at 03:13.
    Regards

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [Java] read and write a file

    The best way is Tidy the html or xml code

    http://fop-miniscribus.googlecode.co...dy/bestqtidy.h
    class QTidy
    qt4 compatible lib...
    http://fop-miniscribus.googlecode.co.../modules/tidy/

    after you can parse as xml and read/write all your own chunk html...

    or if you search only image on html code to load.....


    Qt Code:
    1. /* read remote image from html code */
    2. void Load_Image_Connector()
    3. {
    4. ///////////////////qDebug() << "### Load_Image_Connector";
    5.  
    6. QRegExp expression( "src=[\"\'](.*)[\"\']", Qt::CaseInsensitive );
    7. expression.setMinimal(true);
    8. int iPosition = 0;
    9. int canna = 0;
    10. while( (iPosition = expression.indexIn( html , iPosition )) != -1 ) {
    11. QString semi1 = expression.cap( 1 );
    12. canna++;
    13. dimage.append(semi1); /* image lista 1 */
    14. AppendImage(semi1); /* register qmap images & name & replace qurl remove css file */
    15. iPosition += expression.matchedLength();
    16. ////////////////////qDebug() << "### iPosition " << iPosition;
    17. }
    18. QTimer::singleShot(1, this, SLOT(GetRemoteFile()));
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [Java] read and write a file

    IMO: here a symply BufferedReader to read & write file or buffer ... on QThread

    I use it to send Image frame movie over QEvent at just time

    http://fop-miniscribus.googlecode.co...t/image_event/


    Qt Code:
    1. /* device to write or read from QThread or not */
    2.  
    3. class StreamFile
    4. {
    5. public:
    6. StreamFile()
    7. :d(new QBuffer())
    8. {
    9. d->open(QIODevice::ReadWrite);
    10. start();
    11. }
    12. ~StreamFile()
    13. {
    14. d->close();
    15. }
    16. bool clear()
    17. {
    18. d->write(QByteArray());
    19. return d->bytesAvailable() == 0 ? true : false;
    20. }
    21. void start() {
    22. d->seek(0);
    23. }
    24. bool LoadFile( const QString file ) {
    25. if (clear()) {
    26. QFile f(file);
    27. if (f.exists()) {
    28. if (f.open(QFile::ReadOnly)) {
    29. d->write(f.readAll());
    30. f.close();
    31. start();
    32. return true;
    33. }
    34. }
    35. }
    36. return false;
    37. }
    38. bool PutOnFile( const QString file ) {
    39. QFile f(file);
    40. if (f.open(QFile::WriteOnly)) {
    41. uint bi = f.write(d->readAll());
    42. start();
    43. return bi > 0 ? true : false;
    44. }
    45. return false;
    46. }
    47. QBuffer *device() { return d; }
    48. bool isValid() { return img.loadFromData(stream()); }
    49. QByteArray stream() { return d->data(); }
    50. QImage img;
    51. QBuffer *d;
    52. };
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: [Java] read and write a file

    sorry, but I need to use Java with standard library (not QT)....My question was focused on '\n' problem....

    thanks.
    Regards

Similar Threads

  1. How can I Read Write Excel File
    By xingshaoyong in forum Qt Programming
    Replies: 6
    Last Post: 13th July 2011, 20:16
  2. structures to a file (read and write)
    By baray98 in forum Qt Programming
    Replies: 5
    Last Post: 10th February 2008, 20:12
  3. How to open a file in Read Write mode
    By merry in forum Qt Programming
    Replies: 13
    Last Post: 16th November 2007, 14:40
  4. Read \Write Excel File using Qt
    By xingshaoyong in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 22:07
  5. FSWriteFork in MAC OS to write data to a file.
    By vishal.chauhan in forum General Programming
    Replies: 5
    Last Post: 2nd July 2007, 06:48

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.