Results 1 to 13 of 13
- 03-11-2008, 08:43 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 13
- Rep Power
- 0
how can we remove blank lines from a .txt
Copy a file to another file removing all blank lines.
Typical Input
Here is an
input text with just two
blank lines.
Typical Output
Here is an
input text with just two
blank lines.
coding till now (im using acm library, dont pay attention on readLine()):
Java Code:String inputFile = readLine("Insert file: "); BufferedReader inputFileReader = new BufferedReader(new FileReader(inputFile)); String inputFileLine; String NewFile = readLine("Insert file name of the new file: "); PrintWriter outputFile=new PrintWriter (new FileWriter(NewFile)); while((inputFileLine = inputFileReader.readLine()) != null) { char [] characters= inputFileLine.toCharArray(); for (int i=0; i<characters.length; i++){ outputFile.print(characters[i]); } outputFile.println(""); } inputFileReader.close(); outputFile.close();
Last edited by Camden; 03-11-2008 at 08:45 PM.
- 03-12-2008, 06:02 AM #2Java Code:
import java.io.*; import java.util.Scanner; public class IOTest { public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(System.in); System.out.println("Insert file: "); String inputFile = //scanner.nextLine(); "ioTestIn.txt"; BufferedReader inputFileReader = new BufferedReader( new FileReader(inputFile)); String inputFileLine; System.out.println("Insert file name of the new file: "); String NewFile = //scanner.nextLine(); "ioTestOut.txt"; scanner.close(); PrintWriter outputFile = new PrintWriter(new FileWriter(NewFile)); while((inputFileLine = inputFileReader.readLine()) != null) { if(inputFileLine.length() == 0) continue; outputFile.println(inputFileLine); } inputFileReader.close(); outputFile.close(); } }
Java Code:Here is an input text with just two blank lines.
- 07-27-2011, 12:26 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 10
- Rep Power
- 0
How to do this with out writing to a new file, just to read the input from a string variable with empty lines in it and store the result to a new String varaiable, after the empty lines are removed.
Last edited by reachjava; 07-27-2011 at 12:29 PM.
- 07-27-2011, 01:40 PM #4read the input from a string variable with empty lines in it and store the result to a new String varaiable, after the empty lines are removed.
Concatenate the non empty lines to the new String variable.
- 07-28-2011, 09:08 PM #5
One approach would be to do this:
Assumptions:
file 1 exists
file 2 will be created
Pseudo Code:
Java Code:Open file 1 for reading Open file 2 for writing while true if file1 != ready // EOF encountered break String line = readline if readline == blank // do nothing else write to file 2 end while close file1 close file2
- 07-29-2011, 07:17 AM #6
Member
- Join Date
- Jun 2011
- Posts
- 10
- Rep Power
- 0
Hi,
Thank you for the post. My requirement is not to write to a new file, I will be getting file content in a String variable, in that empty lines will be there at the end of the file, I want to remove these empty lines and put it into a new String variable and I have to use that new variable for further processing...
Thanks..
- 07-29-2011, 07:20 AM #7
Member
- Join Date
- Jun 2011
- Posts
- 10
- Rep Power
- 0
I tried this norm.. Want I want to know is by default outputStreamWriter,FileWriter and PrintWriter writes a empty line at the end of the file? which writes stops the cursor exactly at the last content line in the file?
Thanks..
- 07-29-2011, 07:27 AM #8
- 07-29-2011, 07:28 AM #9
- 07-29-2011, 07:30 AM #10
Member
- Join Date
- Jun 2011
- Posts
- 10
- Rep Power
- 0
No, this won't be a large file, it will have not more than 20 lines. I have to use it in another tool, there I have to retrieve the file as a String, there I have to do the remaining process, like removing the empty lines and to use that new variable.
- 07-29-2011, 07:32 AM #11
Member
- Join Date
- Jun 2011
- Posts
- 10
- Rep Power
- 0
hi master, if you are interested in replying, pls reply, other wise leave it.. why you are saying all this.. Is there anything wrong in knowing, what is not clear or not known from the experts!!
- 07-29-2011, 07:39 AM #12
Are you talking to me? You are not making any sense. All I am trying to do is get you to clarify what you are trying to do. If you don't want my help then I don't give a damn. No skin of my nose.
- 07-29-2011, 01:38 PM #13
Similar Threads
-
How to get the count of all the lines in a file
By Java Tip in forum java.ioReplies: 0Last Post: 04-06-2008, 07:45 PM -
Joining lines in Eclipse
By Java Tip in forum Java TipReplies: 0Last Post: 12-04-2007, 11:17 AM -
how to edit lines.
By jason27131 in forum New To JavaReplies: 1Last Post: 08-01-2007, 04:41 AM -
Print a blank space
By susan in forum New To JavaReplies: 2Last Post: 07-30-2007, 01:58 PM -
Blank result for jsp_servlet in Eclipse
By Unni in forum EclipseReplies: 2Last Post: 07-12-2007, 04:30 AM
Bookmarks