View Full Version : [Java] read and write a file
mickey
22nd June 2008, 03:03
Hi,
I have one problem; I must read a html like this;
<html>
<body>
Hello
</body>
</html>
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:
<html>\n<body> ........
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:
File fin = new File(FILE_NAME);
FileReader in = new FileReader(fin);
istream = new BufferedReader( in );
String line = null;
while ( (line = istream.readLine()) != null ) {
//System.out.println(line);
sb.append(line);
}
// here do changes on the sb
String text = sb.toString();
FileWriter fw = new FileWriter(new File ("out.txt") );
out = new PrintWriter(fw);
out.println(text);
Thanks,
patrik08
22nd June 2008, 09:20
The best way is Tidy the html or xml code
http://fop-miniscribus.googlecode.com/svn/trunk/modules/tidy/bestqtidy.h
class QTidy
qt4 compatible lib...
http://fop-miniscribus.googlecode.com/svn/trunk/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.....
/* read remote image from html code */
void Load_Image_Connector()
{
///////////////////qDebug() << "### Load_Image_Connector";
QRegExp expression( "src=[\"\'](.*)[\"\']", Qt::CaseInsensitive );
expression.setMinimal(true);
int iPosition = 0;
int canna = 0;
while( (iPosition = expression.indexIn( html , iPosition )) != -1 ) {
QString semi1 = expression.cap( 1 );
canna++;
dimage.append(semi1); /* image lista 1 */
AppendImage(semi1); /* register qmap images & name & replace qurl remove css file */
iPosition += expression.matchedLength();
////////////////////qDebug() << "### iPosition " << iPosition;
}
QTimer::singleShot(1, this, SLOT(GetRemoteFile()));
}
patrik08
22nd June 2008, 09:27
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.com/svn/trunk/apng_label/Image_On_QEvent/image_event/
/* device to write or read from QThread or not */
class StreamFile
{
public:
StreamFile()
:d(new QBuffer())
{
d->open(QIODevice::ReadWrite);
start();
}
~StreamFile()
{
d->close();
}
bool clear()
{
d->write(QByteArray());
return d->bytesAvailable() == 0 ? true : false;
}
void start() {
d->seek(0);
}
bool LoadFile( const QString file ) {
if (clear()) {
QFile f(file);
if (f.exists()) {
if (f.open(QFile::ReadOnly)) {
d->write(f.readAll());
f.close();
start();
return true;
}
}
}
return false;
}
bool PutOnFile( const QString file ) {
QFile f(file);
if (f.open(QFile::WriteOnly)) {
uint bi = f.write(d->readAll());
start();
return bi > 0 ? true : false;
}
return false;
}
QBuffer *device() { return d; }
bool isValid() { return img.loadFromData(stream()); }
QByteArray stream() { return d->data(); }
QImage img;
QBuffer *d;
};
mickey
22nd June 2008, 10:43
sorry, but I need to use Java with standard library (not QT)....My question was focused on '\n' problem....
thanks.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.