cancel
Showing results for 
Search instead for 
Did you mean: 

Removing "LineFeed" character from file

Amey-Mogare
Contributor
0 Kudos

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

Accepted Solutions (0)

Answers (1)

Answers (1)

Amey-Mogare
Contributor
0 Kudos

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());
		}
	}
}