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>\nhello\n</body>\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
9 2131 JosAH 11,448
Recognized Expert MVP
The readLine() method returns a String without the terminating end of line character
sequence so you have to append that sequence yourself after you have appended
the line String to your StringBuffer (or StringBuilder). Look at the system properties
for the correct value of the end of line character sequence.
kind regards,
Jos
is there any other "read" method that don't get out '\n' ???
JosAH 11,448
Recognized Expert MVP
is there any other "read" method that don't get out '\n' ???
Sure, you can read parts (or all) of the file directly into a char[] of CharBuffer if
you want. Read the API documentation for details. Note though that an end of
line character sequence need not be a single \n character so don't anticipate
on that.
kind regards,
Jos
to be sincer, for me it's not important to read the file line by line; how can I put the entire file into a String in a hit?
JosAH 11,448
Recognized Expert MVP
to be sincer, for me it's not important to read the file line by line; how can I put the entire file into a String in a hit?
Read the API documentation for the BufferedReader and Reader classes.
You'll find the read() methods you need.
kind regards,
Jos
hello,
read() is ok; but as you can see I have ither problem because I'm a newbie to java; doesn't exist any function like c++ seekg() to seek at the end of my file and retrive the number of characters it has? -
char buf[] = new char[100];
-
int num = istream.read(buf, 0, 100);
-
It's my code but I can't have a fixed dimesion for buf (I mean, 100)....
Can you suggest me more? thanks...
if you use readLine(), you can assume that there is a '\n' character after each call, im not sure how that fits into your program, but if you had a loop like: - while ( (line = istream.readLine()) != null ) {
-
//System.out.println(line);
-
sb.append(line);
-
}
you could use something like
prior to appending it to whatever you are doing.
im not sure if that helps, but i hope it did
good luck,
ken
Nepomuk 3,112
Recognized Expert Specialist
if you use readLine(), you can assume that there is a '\n' character after each call...
That is not 100% correct - there won't be a '\n' character after the last line. So you should use something like this: - read first line
-
while (there is a next line)
-
{
-
add '\n' to your String
-
read the next line and add it to the String
-
}
or alternatively: - while(something)
-
{
-
read next line
-
if there's a next line, add '\n'
-
else quit the loop
-
}
Greetings,
Nepomuk
BigDaddyLH 1,216
Recognized Expert Top Contributor
hello,
read() is ok; but as you can see I have ither problem because I'm a newbie to java; doesn't exist any function like c++ seekg() to seek at the end of my file and retrive the number of characters it has?
Don't forget that the number of bytes is not necessarily the number of chars -- for example, my favorite encoding for HTML, UTF-8.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Bill Cohagan |
last post by:
I'm writing a console app in c# and am encountering a strange problem. I'm
trying to use redirection of the standard input stream to read input from a
(xml) file. The following code snippet is from this app:
===============================
static void Main(string args)
{
if (args.Length > 0) Console.SetIn(new StreamReader(args));
//executes if I don't use the "<", ">" redirection syntax when invoking
XmlTextReader xmlin = new...
|
by: a |
last post by:
I have a struct to write to a file
struct _structA{
long x;
int y;
float z;
}
struct _structA A;
//file open
write(fd,A,sizeof(_structA));
//file close
|
by: Sumana |
last post by:
Hi All,
We developed our project on VC++.Net console application to
create image of disk and to write the image
We are having problem with reading and writing the sector
beyond 6GB Disk or Partition
we are using ReadFile , WriteFile and setFilePointerEx to read
and write the sectors and we are reading/writing 102400 sectors together,
even we have reduced the sectors still it is not able to
read or write,
our program is working fine...
|
by: nicolasg |
last post by:
Hi,
I'm trying to open a file (any file) in binary mode and save it inside
a new text file.
After that I want to read the source from the text file and save it
back to the disk with its original form. The problem is tha the binary
source that I extract from the text file seems to be diferent from the
source I saved. Here is my code:
1)
handle=file('image.gif','rb')
|
by: agphoto |
last post by:
There is big or problem in open file in read and write mode..
$file = "data.txt";
$fp = fopen($file,"w+");
$line = fgets($fp,"120"); // i need only 1st line to read and upto 120 bytes
echo $line;
fclose($fp);
coding is not problem, The problem is file modifier as mention in PHP
| |
by: Frederic Rentsch |
last post by:
Hi all,
Working with read and write operations on a file I stumbled on a
complication when writes fail following a read to the end.
30L
'abcdefg'
Traceback (most recent call last):
File "<pyshell#62>", line 1, in -toplevel-
|
by: =?Utf-8?B?ZGF2aWQ=?= |
last post by:
I try to follow Steve's paper to build a database, and store a small text
file into SQL Server database and retrieve it later. Only difference between
my table and Steve's table is that I use NTEXT datatype for the file instead
of using IMAGE datatype. I can not use SqlDataReader to read the data. I need
your help, Thanks.
-David
(1) I have a table TestFile for testing:
ID int
FileName navrchar(255)
|
by: Bina |
last post by:
hi,
I want to read & write a html file which contain the japanese character. Now how i will do it?I can read & write english character . but when i read & write Japanese character from the html file then the writed html file cannnot show the japanese character. my new created html file show the another character, it cannot show the original japanese character. But when i read & write data from a txt file it works. But only problem is,When...
|
by: vineeth |
last post by:
Hello all,
I have come across a weird problem, I need to determine the amount
of bytes read from a file, but couldn't figure it out ,
My program does this :
__
file = open("somefile")
data = file.read()
print "bytes read ", len(data)
---
|
by: Sachin Garg |
last post by:
I have a program which opens a fstream in binary input+output mode, creating
the file if it doesn't exists. But writing doesn't works after reading, it
must be something obvious that I am not aware of.
f.open(filename,ios::in | ios::out | ios::binary | ios::trunc)
The program flow is
1) write some data
2) read the data
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |