[Java] read and write a file
Hi,
I have one problem; I must read a html like this;
Code:
<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:
Code:
<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:
Code:
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,
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.....
Code:
/* 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
()));
}
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/
Code:
/* device to write or read from QThread or not */
class StreamFile
{
public:
StreamFile()
{
start();
}
~StreamFile()
{
d->close();
}
bool clear()
{
return d->bytesAvailable() == 0 ? true : false;
}
void start() {
d->seek(0);
}
bool LoadFile
( const QString file ) { if (clear()) {
if (f.exists()) {
if (f.
open(QFile::ReadOnly)) { d->write(f.readAll());
f.close();
start();
return true;
}
}
}
return false;
}
bool PutOnFile
( const QString file ) { if (f.
open(QFile::WriteOnly)) { uint bi = f.write(d->readAll());
start();
return bi > 0 ? true : false;
}
return false;
}
bool isValid() { return img.loadFromData(stream()); }
};
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.