• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

New line character not getting written to a file using StringBuffer

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to parse an XML file and populating a string buffer with element values. However, I am appending a new line character to StringBuffer after reading each element as follows.



Please assume all above values are coming from XML file. here COLUMN_SEPARATOR is "|".

Now I want to write the contents of the StringBuffer "constituentContent" to a file like this:


System.out.println statements prints the contents on console with line seperator but when I open the file in Unix all values are in a single line. I have tried all the options for line separator as \n,\r\n but to no avail. Please suggest. Thanks.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Atish Panghate wrote:when I open the file in Unix all values are in a single line.



What does "open" mean in this context? Maybe the thing you're using to "open" the file is discarding those line-ending characters.

And, welcome to the Ranch!
 
Atish Panghate
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.

I am using TextPad to open the file. I have also tried with notepad and wordpad. After writing to the file , I want to bcp the file to a database table. Since, all content in the file is in single line, bcp is failing and nothing gets inserted into table. Please suggest. However when I write a standalone java program like this, data in the file gets written with line separator and when I open the file with text pad data is properly dispalyed and not in single line.

public class Demo {


public static void main(String[] args) throws IOException {
final String seperator = System.getProperty("file.separator");
final String lineSeperator = System.getProperty("line.separator");
System.out.println("File Separator is"+seperator );
System.out.println("Line Separator is"+ " "+lineSeperator);
StringBuffer headerContent=new StringBuffer();
headerContent.append("1026564");
headerContent.append("|");
headerContent.append("1005503");
headerContent.append("|");
headerContent.append("391.6000");
headerContent.append("|");
headerContent.append("INR");
headerContent.append("|");
headerContent.append("|");
headerContent.append("|");
headerContent.append("2013-12-03");
headerContent.append("|");
headerContent.append("|");
headerContent.append("06 Jan 2014");
headerContent.append(lineSeperator);
headerContent.append("1026564");
headerContent.append("|");
headerContent.append("1005503");
headerContent.append("|");
headerContent.append("391.6000");
headerContent.append("|");
headerContent.append("INR");
headerContent.append("|");
headerContent.append("|");
headerContent.append("|");
headerContent.append("2013-12-03");
headerContent.append("|");
headerContent.append("|");
headerContent.append("06 Jan 2014");
headerContent.append(lineSeperator);

System.out.println(headerContent.toString());
File file=new File("/opt/compliance/Atish.dat");
FileOutputStream out= new FileOutputStream(file,true);
BufferedOutputStream bos = new BufferedOutputStream(out);
bos.write(headerContent.toString().getBytes());
bos.flush();
out.close();
bos.close();



}


}
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say the second program is a "standalone" program, that suggests there is something about the first program which you forgot to mention. So it isn't "standalone"? What does that mean?
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you writing the newline on windows and reading it on Unix?
Don't use stream classes for reading/writing character data. Use FileWriter and FileReader classes instead.
 
Atish Panghate
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first program, I am copying the XML file from Windows machine to a particular location on Unix server using winscp and then running the script in unix which call the code mentioned in the first program which is the part of a project I am working on. It parses the XML file and creates .dat file. But data in the .dat file is coming in single line.

I created the second program as a sample program and deployed and ran on Unix. The file gets created with line seperator and data gets displayed properly. So , I dont know why the first program is not able to do this, since file writing code is similar. Please suggest.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write a new line character which the target platform (the platform that will read the data) will understand as a new line.
 
Atish Panghate
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have used System.getProperty("line.separator") as the way to get new line for platform independence. But the result are same.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Atish Panghate wrote:I have used System.getProperty("line.separator") as the way to get new line for platform independence. But the result are same.


Did you read my last reply? System.getProperty("line.separator") gets the new line for the current platform. When you write the file on windows it writes a windows newline. When you read the file on unix it's going to expect a unix new line but you have written a windows new line. Write a new line character which the target platform will understand.
 
Atish Panghate
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am reading XML fiel on Linux and have used \n as line separator. Still it doest work. getBytes method somehow not able to write encoding for \n I guess. Please suggest.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you writing bytes when the data is text? Use FileReader and FileWriter for character files.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Atish Panghate wrote:I am reading XML fiel on Linux and have used \n as line separator. Still it doest work. getBytes method somehow not able to write encoding for \n I guess. Please suggest.


I think you're overthinking this. Reading line-oriented data in Java is very simple providing, as E.Armitage says, you use the right class (ie, one that provides a readLine() or nextLine() method), and it won't care whether the data uses Windows or Unix newlines.

The fact is that Windows is the culprit here. Every other major system on the planet uses '\n' as its line break; but not only does Windows not use it, many Windows programs still don't recognise '\n' as a line break after more than 20 years of this ridiculous difference.

Luckily for you, Java couldn't give a fig. Read files, line by line, with a BufferedReader or a Scanner (java.util.Scanner), and write them with a PrintStream or a PrintWriter. The only thing you might want to do when writing is to use the Windows-style newline sequence, which you can force with:
System.setProperty("line.separator", "\\r\\n");
but even that is overkill in my book.

HIH

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:Read files, line by line, with a BufferedReader or a Scanner...


Actually, the two classes may not behave exactly the same. According to the docs, BufferedReader regards anything terminated with a '\n', \r' or '\r\n' as a "line", but the Scanner class docs aren't specific (at least I can't find where it specifies how it determines them). Consequently, I suspect you're better off using BufferedReader for your purposes.

Winston
 
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The next line method of Scanner is the one thing that really causes confusion; many books are miles out about it and I haven't seen a good explanation anywhere. Except what I wrote myself

In the case of BufferedReader, it has a method which says it reads a line. You should check what happens if you have a file like this (I am using ↩ for enter key/line end thoughout):-

Campbell Ritchie is a right nuisance.↩

…and you read it like this:-so I tried it, and got this output:-

java ReaderDemo file
67
97
mpbell Ritchie is a right nuisance.

The read method reads the character and returns it as an integer. (Remember a char is an integer, not a letter.) It is rather like System.in.read, and I don't like it. You see the ASCII values of the first two letters appear, followed not by the line but by the remainder of the line; I think you would get an empty String as the result if you had a two‑character line there. Try it and see what happens.

The corresponding method in Scanner is described as returning:-

This method returns the rest of the current line, excluding any line separator at the end.

That is quite clear, but it isn't what the methods is called and lots of people misunderstand it. Even books like Horstmann misunderstand it.Assume you are using the default delimiter for inScan, and you feed this input:-

123 456 789 Campbell Ritchie↩

… then you will have "Campbell Ritchie" as name. You do not get the line, but the rest of the line, after 789 .
What follows is also legal input for a Scanner, but you don't get the next line; you get the remainder of the line after 789:-

123 456 789↩
Campbell Ritchie↩

It reads the remainder of the line, which now equals "" into name: a zero‑length String. "Campbell Ritchie" remains unused, and will cause Exceptions if you try to read it with nextInt() or similar.

Warning: what follows is suitable for reading from the keyboard because the user can respond to the error messages. It is not suitable for text files, unless you want to edit the text file whilst reading it

Suggestion: search my posts for utility class, and work out how to write a utility class. Search them for Scanner next Int hasNextInt. You will find a loop which can absolutely obviate the risk of Exceptions from nextInt() or similar, by printing out an error message and asking for new input. Rob Spoor showed me how to do that.
I suspect that class will not be thread‑safe. You may be able to make it thread‑safe by synchronising every method. Not sure.
Write yourself a utility class called KeyboardInputs or similar. You can write things likeNow: more difficult. Consider what to do about nextLine. Maybe a loop which tests whether the input is empty:-I think that will work. Winston and others, please check whether I have made some daft mistake.
And it would appear that the readLine method and nextLine are more similar in their pitfalls than I thought at first.
Agree: it says line separator in the Scanner documentation but does not define it.
Check carefully what the String#trim() defines as whitespace. This Unicode page will probably help.
For the strange syntax after while, look here.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Atish Panghate wrote:I am reading XML fiel on Linux and have used \n as line separator. Still it doest work. getBytes method somehow not able to write encoding for \n I guess. Please suggest.



As I have said before, you haven't told us enough about the code which actually has the problem, namely the Linux code which is reading the text. You are still focusing on the code which writes the text.

You mentioned getBytes(), which as E Armitage already said is a peculiar thing to do when reading XML data. But you didn't say anything about how you used that method, or whether you are using a standard XML parser to read the code, or anything really. There are situations when an XML parser will discard new-line characters, for example, but we have no idea whether those situations might be occurring in your case.

Edit: Looking back at your original post, it looks to me like you are parsing XML and producing something which is not XML. Then you apparently read that non-XML file in Linux. But now you're saying that you are trying to read an XML file in Linux. I think you need to tell us more so that we aren't confused.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: . . . more difficult. . . . please check whether I have made some daft mistake. . . .

And Prasanna Raman found the daft mistake. The loop would read alternate lines. Maybe a do loop would be better
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Suggestion: search my posts for utility class, and work out how to write a utility class. Search them for Scanner next Int hasNextInt. You will find a loop which can absolutely obviate the risk of Exceptions from nextInt() or similar, by printing out an error message and asking for new input. Rob Spoor showed me how to do that.



I'm probably overlooking something but I can't find the option of searching through a user's post for a String. I could only see the option of refining Campbell's responses by the forums. But he has written several responses.

I tried the general search ( the one we have for each of the forums ) also, but I got many results and I couldn't find that post in the first four pages of the search results. Is there an easier way to find that post?

I have a feeling that I've probably read that post but I don't remember Rob's recommendation.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was searching in the wrong forum. I should have searched in Beginning Java. I was searching in JIG.

Anyway I think I've found the relevant posts.

https://coderanch.com/t/626418/java/java/entering-integers#2865803
https://coderanch.com/t/470912/java/java/#2108910
https://coderanch.com/t/623579/java/java/java-io-IOException-Stream-closed#2850896




 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you click search, you put the String into the top empty box. It says “search for” on the left.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell.

I had never tried that search option before. It's definitely better than the one I use often.

Also thanks everyone ( Rob, you too ) for your responses. I think I would have also done these mistakes in my programs ( to be written ), had I not chanced upon this topic.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chan Ag wrote: . . . I should have searched in Beginning Java. I was searching in JIG. . . .

I usually search both BJ and JiG if I am looking for that sort of post.
And well done finding the answers in the end
 
reply
    Bookmark Topic Watch Topic
  • New Topic