Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-11-2008, 09:43 PM
Member
 
Join Date: Nov 2007
Posts: 13
Camden is on a distinguished road
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()):
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 09:45 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-12-2008, 07:02 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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(); } }
ioTestIn.txt
Code:
Here is an input text with just two blank lines.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the count of all the lines in a file Java Tip java.io 0 04-06-2008 08:45 PM
Joining lines in Eclipse Java Tip Java Tips 0 12-04-2007 12:17 PM
how to edit lines. jason27131 New To Java 1 08-01-2007 05:41 AM
Print a blank space susan New To Java 2 07-30-2007 02:58 PM
Blank result for jsp_servlet in Eclipse Unni Eclipse 2 07-12-2007 05:30 AM


All times are GMT +3. The time now is 06:52 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org