I have to read a csv file line by line and change line break (alt+enter that is \n\r) with space and comas out side of fields with Ctrl+A (\001). As I try this with reading Buffered Reader, it takes line from \n and not interpret \n\r as non line break character. How can I handle this. I have to done this in java
Asked
Active
Viewed 3,713 times
2 Answers
1
You can read the entire file into a String variable and then use String.replaceAll()
to replace the characters as you want:-
File file = new File("abc.csv");
FileInputStream fis = null;
fis = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
fis.read(data);
fis.close();
String str = new String(data, "UTF-8");
And then replace characters in the String:-
str = str.replaceAll("\r\n", " ");
str = str.replaceAll("[,]", ";");
System.out.println(str);
Then you can create a new file or overwrite the existing file using the new String

Mustafa sabir
- 4,130
- 1
- 19
- 28
-
Thanks for replying, is there any CSVReader library , which handle this type of things internally. – agarwal_achhnera Dec 09 '14 at 10:16
-
There are many csv reader libraries available like [CSVParser from Apache](https://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVParser.html) , But you will have to google out and find if they have capability to handle out such implementations. I mostly use my own custom java code to parse CSVs. Also I think your requirement more specifically targets String/File manipulation rather than focussing on CSVs. – Mustafa sabir Dec 09 '14 at 11:06
0
I assume you mean "\r\n", not "\n\r". Usually windows-based programs generate "\r\n" while linux-based programs use "\n".
The buffered reader normally uses the global "line.separator" setting that should be set specifically for your system.
On a sidenote: it is usually interesting to use frameworks for parsing instead of doing actual line reads because there are a lot of edge cases (like this one) that a framework usually covers.

nablex
- 4,635
- 4
- 36
- 51
-
Actually few fields contains test data submited by user, which is alt+enter, so what does it As I know is \n\r (but not confirm), Could you please confirm this will be \n\r or r\n. All the things are at linux system – agarwal_achhnera Dec 09 '14 at 07:08