on 05-17-2010 5:45 AM
Hi,
I have a scenario where I need to develop an EJB session bean business method which will take a file (inputstream) as input.
The methods should remove all "LineFeed" characters in this file and return new file (inputstream).
How do I achieve this? How do I remove line feeds from inputstream?
Thanks and regards,
Amey
This is how its done:-
import java.io.*;
public class RemoveLineFeed{
private static final String BLANK = "";
private static final String NEWLINE = "\n";
private static final String CARR_RET_NEWLINE = "\\r|\\n";
private static final String LINE_SEPARATOR = "line.separator";
public static void main(String[] args){
InputStream l_returnStream = null;
String l_inputStreamAsString = null;
byte[] byteArray = null;
try{
//1. InputStream to String
FileInputStream fileInputStream = new FileInputStream ("997AND824-TESTFILE_WithLF");
DataInputStream dis = new DataInputStream(fileInputStream);
int arraySize = fileInputStream.available();
byteArray = new byte[arraySize];
dis.readFully(byteArray);
l_inputStreamAsString = new String(byteArray);
//2. Carry out replace
//l_inputStreamAsString = l_inputStreamAsString.replaceAll(NEWLINE, BLANK);
//l_inputStreamAsString = l_inputStreamAsString.replaceAll(System.getProperty(LINE_SEPARATOR), BLANK);
l_inputStreamAsString = l_inputStreamAsString.replaceAll(CARR_RET_NEWLINE, BLANK);
//3. Convert String back into InputStream
//l_returnStream = new ByteArrayInputStream(l_inputStreamAsString.getBytes());
//4. Create new file
File l_outFile = new File("997AND824-TESTFILE_WithOutLF");
FileWriter fileWriter = new FileWriter(l_outFile, true);
fileWriter.write(l_inputStreamAsString);
fileWriter.close();
}
catch (IOException ioe) {
System.out.println(ioe.toString());
}
catch (Exception e) {
System.out.println(e.toString());
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
76 | |
11 | |
10 | |
7 | |
7 | |
6 | |
6 | |
6 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.