Results 1 to 4 of 4
Thread: Java File I/O
- 01-17-2011, 07:29 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
Java File I/O
I have the following file ...
INPUT.TXT
I want to read the numbers...Java Code:5 0 0 0 1 0.5 0.5 1 0 1 1
I am getting the following output. Please help me out!Java Code:import java.io.*; class ConvexHull { public static void main(String args[]) throws IOException { DataInputStream dataIn; try { dataIn = new DataInputStream(new FileInputStream("INPUT.TXT")); } catch(FileNotFoundException exc) { System.out.println("File Not Found"); return; } BufferedReader inputFileReader = new BufferedReader(new InputStreamReader(dataIn)); String line; line = inputFileReader.readLine(); System.out.println(line); while ( (line = inputFileReader.readLine()) != null ) { line = inputFileReader.readLine(); System.out.println(line); } dataIn.close(); System.out.println("\nProgram terminated successfully!!"); } }
Java Code:5 0 1 1 0 null Program terminated successfully!!
- 01-17-2011, 08:02 PM #2
This is where you are messing up the things. You are reading the file contents, twice here in while loop, but printing them only once. This is resulting into skipping the alternate lines of the text file.
Even you are not supposed to read the text file and print the contents before the while loop.
Can you now figure out, what needs to be removed in your code?
GoldestLast edited by goldest; 01-17-2011 at 08:05 PM.
Java Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-18-2011, 01:53 AM #3
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
you can use the Scanner to read your file as well. here's an example
Java Code:Scanner sc = new Scanner( new File("file") ); String line ; while( sc.hasNext() ){ line = sc.nextLine(); System.out.println(line); } sc.close()
- 01-18-2011, 02:42 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
how to split large xml file into small xml file in java
By enggvijaysingh@gmail.com in forum XMLReplies: 2Last Post: 02-07-2011, 09:34 AM -
To open an image file such as Jpeg file using JAva Program
By itmani2020 in forum Advanced JavaReplies: 10Last Post: 07-11-2008, 09:57 AM -
How to parse the CSV(Comma separation values)file and validate the file using java
By padmajap13 in forum Advanced JavaReplies: 7Last Post: 05-23-2008, 03:46 AM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM -
Converting text file(.txt) to JPG file(.jpg) in java
By javadeveloper in forum Advanced JavaReplies: 0Last Post: 11-09-2007, 04:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks