US-ASCII LF character

Hello all

I am reading a byte array and making a file out of it. the file is a csv file...which when opened in notepad shows CSV and when opened in excel shows values in their designated columns. The charSet we are using is US-ASCII. Everything is working fine but when we open this file in notepad...we are seeing a 'square' after every line. upon further researching i found out that it is the LF character (\n). So i made a new byte array and now i am passing everything EXCEPT the LF character

for (i = 0; i < csvBytes.length; i++)

{

if ((char)csvBytes[i]!='\n')

{

System.out.println("Putting character: " + (char)csvBytes[i]);

csvResults[i] = csvBytes[i];

}

else

System.out.println("At position " + i +" LF character was found: " + (char)csvBytes[i]);

Now after all this when i put csvResults into a csv file andi open this csv file in excel...everything is showing in columns..there is just one row. i am thinking this is because the LF character has been taken out. But now when i open it in notepad the 'Square' is not there.

Does anyone have any better solutions to get rid of this 'square' (LF) from notepad and make excel still work.

[1612 byte] By [bhaarat_javaa] at [2007-11-26 23:59:45]
# 1
\n is the Linux linebreak. Windows has \n\rYou can ask the system for the linebreak of the current plattform with System.getProperty("line.separator");
Wildcard82a at 2007-7-11 15:49:02 > top of Java-index,Java Essentials,New To Java...
# 2
file is coming from a server (solaris) and is displayed on windows system
bhaarat_javaa at 2007-7-11 15:49:02 > top of Java-index,Java Essentials,New To Java...
# 3

can i replace the bit or character in byte array so that notepad would know that it is a LF and would interpret that rather than showing a 'square'

something like this maybe?

for (int j1 = 0; j1 < csvResults.length; j1++)

{

if ((char)csvResults[j1]!='\n')

csvResults1[j1] = csvResults[j1];

else //put a character here that notepad would understand as LF

{

System.out.println("Value: " + csvResults1[j]);

csvResults1[j]=10;

}

}

bhaarat_javaa at 2007-7-11 15:49:02 > top of Java-index,Java Essentials,New To Java...
# 4

no, you can not replace the character, because you need an aditional character.

If I'd be you, I'd rather substitute that in a String with replaceAll, or add the missing \r when I'm writing the bytes to disk.

btw, in case that's sufficient: a more sophisticated edior than Windows Notepad will do the trick and accept \n as linebreak.

Wildcard82a at 2007-7-11 15:49:02 > top of Java-index,Java Essentials,New To Java...
# 5
> file is coming from a server (solaris) and is displayed on windows systemI recommend not creating a dependency on notepad. This issue is very similar to the "problem" you expressed here: http://forums.devshed.com/java-help-9/character-set-for-csv-435100.html~
yawmarka at 2007-7-11 15:49:02 > top of Java-index,Java Essentials,New To Java...
# 6

moving to a more sophisticated editor than notepad is the last option.

yes thanks yawmark.

if i do this i get an 'invalid character constant' compiling error ...

String toWriteA = new String(csvBytes);

toWriteA.replace('/n','/r/n');

bhaarat_javaa at 2007-7-11 15:49:02 > top of Java-index,Java Essentials,New To Java...
# 7
toWrite = toWrite.replace("\n", "\r\n");
uncle_alicea at 2007-7-11 15:49:02 > top of Java-index,Java Essentials,New To Java...
# 8
> '/r/n'Two char values does not a char literal make. ;o)~
yawmarka at 2007-7-11 15:49:02 > top of Java-index,Java Essentials,New To Java...
# 9
ok screw it...i've convinced people to just open .csv file with wordpad. looks like wordpad adds /r/n by itself and we dont see the 'square' anymore.
bhaarat_javaa at 2007-7-11 15:49:02 > top of Java-index,Java Essentials,New To Java...
# 10
> looks like wordpad adds /r/n by itselfNot quite. WordPad doesn't add any characters; it interprets '\n' as a proper new line, like most text editors do. Notepad doesn't, which is why it prints the "square"; it does so for every character it doesn't
yawmarka at 2007-7-11 15:49:03 > top of Java-index,Java Essentials,New To Java...
# 11
notepad sux
bhaarat_javaa at 2007-7-11 15:49:03 > top of Java-index,Java Essentials,New To Java...
# 12
> notepad suxNow we're getting somewhere!^_^
uncle_alicea at 2007-7-11 15:49:03 > top of Java-index,Java Essentials,New To Java...