View Single Post
  #6 (permalink)  
Old 01-02-2008, 11:12 PM
CaptainMorgan's Avatar
CaptainMorgan CaptainMorgan is offline
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Is there something wrong with both of your codes or am I doing something wrong? They compile, but when ran, end up outputing the text file verbatim and then crashing with a NullPointerException.

Tampa, try this one, albeit a bit naive, and I've left out the substring for you to figure out.
Code:
import java.io.*; import java.util.*; public class demo { public static void main(String args[]) { String data = null; int recCount = 0; try { FileReader fr = new FileReader("C:\\Development\\demo.txt"); BufferedReader br = new BufferedReader(fr); data = new String(); while ((data = br.readLine()) != null) { int length = data.length(); for (int i = 0; i < length; i++) { System.out.print(data.charAt(i) + " "); i++; } System.out.println(""); data = br.readLine(); // skips a line, which is // naive, seek something better, but // you get the idea recCount++; } } catch (IOException e) { System.out.println("IOException error!"); e.printStackTrace(); } } }
Reply With Quote