Reading Data From .dat File in Java

  1. #1

    Assistance: Reading a *.DAT file in Java

    hullo everyone, I promise someone can assistance me.
    I have to read some information from a *.dat file and the problem is that it doesn't read it.
    I don't know weather I have used the correct code or the location of the file is wrong.

    The location of the file is in the projection main folder and i call back it reads the file as if ,instead of using waypoints.dat i use waypoint.dat (withouth the s) it catches an exception maxim no file was read.

    here is the code

    Code:

    import coffee.io.*; import java.util.*; public class readWaypoint {  	/** 	 * @param args 	 */ 	public static void principal(String[] args) { 		// TODO Automobile-generated method stub 		 	String waypoints=""; 	Boolean endOfFile=false; 	try 	{ 	 		FileInputStream file = new FileInputStream("waypoints.dat"); 		DataInputStream fileStream= new DataInputStream(file); 		 		//waypoints=fileStream.readUTF(); 		while(endOfFile==false) 			 		{ 			 			endeavour 			{ 			waypoints=waypoints + fileStream.readUTF(); 			} 		 		catch(EOFException e) 		   { 			endOfFile=true; 		   } 	 		fileStream.close(); 		 		} 		Organization.out.println(" This is the content of the file:"); 		System.out.println(waypoints);	 			 	}  	catch(FileNotFoundException due east) 	{ 		 	Organization.out.println("No file was read");	 	}  	take hold of(IOException e) 		{ 			System.out.println("There was a problem reading the file" + east); 		} 				 	} }
    when I debugged information technology gives me source not found on this signal >
    waypoints=waypoints + fileStream.readUTF();

    thanks in accelerate for the assistance

    Last edited by cpu2007; December 28th, 2010 at 01:fourteen PM.

  2. #2

    Re: Assistance: Reading a *.DAT file in Java

    Was the .dat file written using the DataOutputStream.writeUTF() method?

  3. #3

    Re: Aid: Reading a *.DAT file in Java

    Quote Originally Posted by keang View Post

    Was the .dat file written using the DataOutputStream.writeUTF() method?

    the file has normal text, not binary or any other type merely the extension is .dat.
    I remember the trouble was with the syntax and using the wrong reading system.
    I have managed to make it work
    hither is the code if someone else might need some helpe

    Code:

    import java.io.*;   public class readFile { 	 	 	 	public Cord readIn(String fileIn){ 	String fileContent=""; 	 	try 	{ 		//make filereader object to read the file 		FileReader file = new FileReader(fileIn); 		//create bufferreader to wrap the file  		BufferedReader fileStream = new BufferedReader(file); 		 		 		Cord temp=fileStream.readLine(); 		while(temp!=null) 		{ 		fileContent=fileContent + " " +temp;	 		temp=fileStream.readLine(); 		 		 		 	 		}	 		fileStream.close(); 				 	} 	catch(FileNotFoundException e) 	{ 		 	System.out.println("No file was read");	 	} 		catch(IOException e) 		{ 			Organization.out.println("There was a problem reading the file"); 		} 		return fileContent; 	} 	 	 	}
    This is a course that does become equally input the name of the file and and then reads the content of information technology.

    Cheers


  4. #4

    Re: Assist: Reading a *.DAT file in Java

    I call up the problem was with the syntax and using the wrong reading system.

    The problem was you used a method without reading the API docs which conspicuously state "Reads in a cord that has been encoded using a modified UTF-eight format.". Such as a string that has been written by the DataOutputStream.writeUTF() method, hence my before question.

    This is a class that does go equally input the name of the file and then reads the content of it.

    I wouldn't recommend using this class for the following reasons:
    1. If an IOException is thrown whilst reading the file, the file will not be closed. Y'all demand to close the file in a finally clause.
    2. Concatenating strings in loops become increasingly inefficient as the length of the file increases. You should use a StringBuilder.
    3. The terminal result isn't a true representation of the file as a ' ' char is added at the stop of each line rather than a line terminator.

  5. #5

    Re: Help: Reading a *.DAT file in Java

    Quote Originally Posted by keang View Post

    The problem was you used a method without reading the API docs which conspicuously state "Reads in a string that has been encoded using a modified UTF-viii format.". Such every bit a string that has been written by the DataOutputStream.writeUTF() method, hence my earlier question.

    I wouldn't recommend using this course for the following reasons:

    1. If an IOException is thrown whilst reading the file, the file volition not exist closed. You demand to shut the file in a finally clause.
    2. Concatenating strings in loops become increasingly inefficient every bit the length of the file increases. You lot should use a StringBuilder.
    3. The final result isn't a true representation of the file as a ' ' char is added at the stop of each line rather than a line terminator.
    yeah i do sympathise that, the reason why I used the readUTF was considering the working code that I posted at the cease wasn't working and I thought that the reason might exist with the extention which clearly wasn't was it'southward about the encoding.

    -what exercise y'all advise to close the file? to put information technology at the terminate of the code or how would i implement a finally clause to my lawmaking ?

    -Thanks for that, I will read how the string architect works and if it doesn't require much modification to the lawmaking I might implement that

    -how would a line terminator piece of work ? like this fileContent=fileContent +temp + /n;
    is that how you put a new line terminator?


  6. #6

    Re: Help: Reading a *.DAT file in Java

    I take written a few guides on this:

    Using the finally clause to close a stream
    Why you should employ StringBuilder

    Equally for the line terminator, it depends on what Bone you are on. Luckily there is a system property which supplies the appropriate ane.


  7. #7

    Re: Help: Reading a *.DAT file in Java

    Quote Originally Posted by keang View Post

    Thank you lot very much

hewettswitted1985.blogspot.com

Source: https://forums.codeguru.com/showthread.php?506923-Help-Reading-a-%2A-DAT-file-in-Java

0 Response to "Reading Data From .dat File in Java"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel