The Artima Developer Community
Sponsored Link

Java Answers Forum
file handling errors

9 replies on 1 page. Most recent reply: Nov 18, 2003 4:41 AM by sridevi

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 9 replies on 1 page
maxima

Posts: 15
Nickname: maxima
Registered: Nov, 2003

file handling errors Posted: Nov 16, 2003 6:46 AM
Reply to this message Reply
Advertisement
getting error in this program... what should i do?


import java.io.*;
class straight

{

public static void main(String args[])


{

File file=new File("data.txt");


FileInputStream fin =new FileInputStream(file);

DataInputStream data= new DataInputStream(fin);

try

{


while(true)
{


System.out.println( data.readInt());



}


}

catch(EOFException e)

{ System.out.println(e);}



data.close();


}

}



[abc@sankya abc]$ javac straight.java
straight.java:21: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
FileInputStream fin =new FileInputStream(file);
^
straight.java:39: unreported exception java.io.IOException; must be caught or declared to be thrown
System.out.println( data.readInt());
^
straight.java:54: unreported exception java.io.IOException; must be caught or declared to be thrown
data.close();
^
3 errors
[abc@sankya abc]$

how can i fix these errors?????


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: file handling errors Posted: Nov 16, 2003 7:30 AM
Reply to this message Reply
You should catch the right exception, in this case IOException. Also close the streams inside the finally clause.

import java.io.*;
 
class straight {
 
  public static void main(String args[]){
    File file = new File("data.txt");
 
    DataInputStream data = null;
    FileInputStream fin = null;
    try {
      fin = new FileInputStream(file);
      data = new DataInputStream(fin);
      while (true) {
        System.out.println(data.readInt());
      }
    }
    catch (IOException e) {
      System.out.println(e);
    }
    finally {
      try {
        data.close();
        fin.close();
      }
      catch (IOException ex) {
      }
    }
  }
}

maxima

Posts: 15
Nickname: maxima
Registered: Nov, 2003

Re: file handling errors Posted: Nov 16, 2003 8:45 AM
Reply to this message Reply
yes it is running...but getting different output. my original "data.txt" contains

12 14 16 17 18 20

17 18 20 21 22 90


but ouput gives different numbers...

[ssroy@sankya ssroy]$ javac straight.java
[ssroy@sankya ssroy]$ java straight
540095008
540095520
540096032
540096288
540096544
540160010
169881911
538976305
941629490
807411762
824188978
840966201
java.io.EOFException
[ssroy@sa nkya ssroy]$

maxima

Posts: 15
Nickname: maxima
Registered: Nov, 2003

Re: file handling errors Posted: Nov 16, 2003 8:47 AM
Reply to this message Reply
yes it is running...but getting different output. my original "data.txt" contains

12 14 16 17 18 20

17 18 20 21 22 90


but ouput gives different numbers...

[abc@sankya abc]$ javac straight.java
[abc@sankya abc]$ java straight
540095008
540095520
540096032
540096288
540096544
540160010
169881911
538976305
941629490
807411762
824188978
840966201
java.io.EOFException
[abc@sank ya abc]$

Birendar S Waldiya

Posts: 21
Nickname: ebiren
Registered: Nov, 2003

Re: file handling errors Posted: Nov 16, 2003 11:40 PM
Reply to this message Reply
I also tried the following modified code .. and tried to get out put ..
i get the data that is in my data.txt ie 10,20,..89.. but along with this i get null values

import java.io.*;
class readFile
 
{
 
public static void main(String args[]) throws Exception
{
 
File file=new File("data.txt");
System.out.println("Length of File"+file.length());
FileInputStream fin =new FileInputStream(file);
DataInputStream data= new DataInputStream(fin);
 
try{
	for(int k =0;k< file.length();k++){
	System.out.println( data.readLine());
				}
}
 
catch(EOFException e){
	System.out.println("Exception:==>"+e);
	}
	finally{
data.close();
fin.close();
	}
}
 
}
 
 
 

maxima

Posts: 15
Nickname: maxima
Registered: Nov, 2003

Re: file handling errors Posted: Nov 17, 2003 12:10 AM
Reply to this message Reply
why the code is not giving the correct output from the file? where is the mistake?? what should be the correct code?

J

Posts: 2
Nickname: se92923
Registered: Nov, 2003

Re: file handling errors Posted: Nov 17, 2003 3:06 AM
Reply to this message Reply
If the files you use contain numeric data represented as ASCII/Unicode characters (rather than "binary" data in the format expected by DataInputStream) then I guess you will get results similar to what you describe...

Check out the class DataInputStream:
http://java.sun.com/j2se/1.3/docs/api/

/J

maxima

Posts: 15
Nickname: maxima
Registered: Nov, 2003

Re: file handling errors Posted: Nov 17, 2003 4:42 AM
Reply to this message Reply
i have a simple file: "data.txt"

the content of this file is

12 14 16 17 18 20

17 18 20 21 22 90

now i want to read and print elements on the screen.this is my purpose. can anybody help plz. i wrote the code but that is not working...... any suggestion??

J

Posts: 2
Nickname: se92923
Registered: Nov, 2003

Re: file handling errors Posted: Nov 17, 2003 6:44 AM
Reply to this message Reply
Please read my previous post. There is a difference (in this context) between text files and binary files.

If (as it seems) you wish to have data as text file, use something other than java.io.DataInputStream to read it, try java.ui.BufferedReader (via java.io.FileReader) and then parse the numbers using e.g. java.util.StringTokenizer ...

/Jens

sridevi

Posts: 1
Nickname: sridevi
Registered: Nov, 2003

Re: file handling errors Posted: Nov 18, 2003 4:41 AM
Reply to this message Reply
Try this code

class readFile

{

public static void main(String args[]) throws Exception
{

File file=new File("data.txt");
System.out.println("Length of File"+file.length());
FileInputStream fin =new FileInputStream(file);
BufferedReader d = new BufferedReader(new InputStreamReader(fin));
try{
String str = null;
while ((str = d.readLine()) != null) {
System.out.println(str );
}
}
catch(EOFException e){
System.out.println("Exception:==>"+e);
}
finally{
d.close();
fin.close() ;
}
}

}

Flat View: This topic has 9 replies on 1 page
Topic: Weight and Height Problem??? Previous Topic   Next Topic Topic: reading from MS Project mpp into servlet

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use