473,799 Members | 2,741 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

read/write a file and '\n' problem

mickey0
142 New Member
Hi,
I have one problem; I must read a html like this;
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. Hello
  4. </body>
  5. </html>
  6.  
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:
Expand|Select|Wrap|Line Numbers
  1. <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:
Expand|Select|Wrap|Line Numbers
  1.       File fin = new File(FILE_NAME);    
  2.                   FileReader in = new FileReader(fin);           
  3.                   istream =  new BufferedReader( in );                                               
  4.                   String line = null;                 
  5.                   while ( (line = istream.readLine()) != null  ) {
  6.                       //System.out.println(line);
  7.                       sb.append(line);
  8.                   }
  9.                               // here do changes on the sb
  10.                               String text = sb.toString();
  11.                   FileWriter fw = new FileWriter(new File ("out.txt") ); 
  12.                   out = new PrintWriter(fw); 
  13.                   out.println(text); 
  14.  
Thanks
Jun 22 '08 #1
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
Jun 22 '08 #2
mickey0
142 New Member
is there any other "read" method that don't get out '\n' ???
Jun 22 '08 #3
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
Jun 22 '08 #4
mickey0
142 New Member
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?
Jun 22 '08 #5
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
Jun 22 '08 #6
mickey0
142 New Member
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?
Expand|Select|Wrap|Line Numbers
  1. char buf[] = new char[100];
  2. int num = istream.read(buf, 0, 100);
  3.  
It's my code but I can't have a fixed dimesion for buf (I mean, 100)....
Can you suggest me more? thanks...
Jun 22 '08 #7
drsmooth
112 New Member
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:

Expand|Select|Wrap|Line Numbers
  1.                   while ( (line = istream.readLine()) != null  ) {
  2.                       //System.out.println(line);
  3.                       sb.append(line);
  4.                   }
you could use something like
Expand|Select|Wrap|Line Numbers
  1.  line+= '\n' ;
prior to appending it to whatever you are doing.

im not sure if that helps, but i hope it did

good luck,
ken
Jun 22 '08 #8
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:
Expand|Select|Wrap|Line Numbers
  1. read first line
  2. while (there is a next line)
  3. {
  4.    add '\n' to your String
  5.    read the next line and add it to the String
  6. }
or alternatively:
Expand|Select|Wrap|Line Numbers
  1. while(something)
  2. {
  3.    read next line
  4.    if there's a next line, add '\n'
  5.    else quit the loop
  6. }
Greetings,
Nepomuk
Jun 22 '08 #9
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.
Jun 23 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2433
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...
8
23908
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
5
5120
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...
3
18966
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')
2
12608
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
0
1055
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-
3
2962
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)
2
7702
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...
9
3852
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) ---
1
3930
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
0
9688
marktang
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...
0
9544
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,...
0
10490
Oralloy
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...
1
10238
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,...
0
10030
tracyyun
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...
1
7570
isladogs
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...
0
5467
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...
0
5589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2941
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.